oakland / tecblog

My tech blogs
4 stars 0 forks source link

nginx related✨ #53

Open oakland opened 6 years ago

oakland commented 6 years ago

https://github.com/jaywcjlove/nginx-tutorial

本文和 #64 #125 两篇 issue 都是讲 nginx 的。现在比较分散,未来的主要内容都整合到本文中。 入门先读 Beginner's Guide 这篇文章。

安装 nginx

centos 机器,通过 yum install nginx 安装的

确认 nginx 是否安装成功

nginx -v 显示版本号

启动 nginx

可以通过命令 service nginx start 来启动。如果启动不了的话,还有另外一种启动方式。 首次安装之后,需要启动 nginx。 通过 [nginx安装目录地址] -c [nginx配置文件地址] 来启动 首先需要找到 nginx安装目录地址,find / -name 'nginx',找到很多相关内容,应该是 /usr/sbin/nginx 这个可执行文件。所以命令就是 /usr/sbin/nginx -c /etc/nginx/nginx.conf,执行完毕之后,访问 linux 机器 ip + 80 端口,可以看到 nginx welcome 的页面就表示启动成功了。或者执行 ps -ef | grep nginx 看到很多 nginx 相关的进程也表示启动成功了。

nginx 的配置文件路径

根据安装方式的不同,nginx 的配置文件通常可能会在下面这几个路径中:


官方的 beginner's guide 也不错,入门先看这个。 淘宝团队对于 nginx 的文章

ps -ef|grep nginx 显示当前 nginx 的进程 sudo kill -QUIT 7398 关闭进程 sudo kill -HUP 7398 重启进程

nginx -s reload 重启 nginx nginx -s stop 关闭 nginx nginx -t 测试 nginx 配置文件是否正确 -s 的 s 参数表示 signal

升级 openssl 之后依然显示 F 的打分,这里有篇文章 https://community.letsencrypt.org/t/f-rating-for-helloworld-letsencrypt-org-on-qualys-ssl-labs-fixed/16679/8 可能是要升级一下 nginx 甚至 linux 才行

发现一个好用的小工具网站 https://pastebin.com/

这篇博客教了怎么升级到 a + https://scotthelme.co.uk/a-plus-rating-qualys-ssl-test/

DNS CAA 显示 no

这篇博客就是说了如何解决这个问题的过程。 其他相关的文章还有:

  1. https://community.qualys.com/thread/16967-configure-dns-caa
  2. https://support.globalsign.com/customer/portal/articles/2851274-how-to-add-dns-caa-record-to-a-dns-zone-file
  3. https://blog.qualys.com/ssllabs/2017/03/13/caa-mandated-by-cabrowser-forum

Reverse proxy VS Load banlancing

reverse-proxy-vs-load-balancer,这篇文章讲的很好。基本上吧负载均衡和反向代理之间的区别都讲清楚了,同时还讲解了很多相关的知识。

防火墙配置

'Nginx: from beginner to pro' 这本书里 chapter2 / verify web server installation 这个部分说了,有些 linux 的机器,比如 CentOS 在防火墙配置里是默认关闭 80 端口的。在 Ubuntu 和 centOS 上打开 80 端口的命令是不同的——centOS 是通过 firewall-cmd 这个命令来打开,但是 Ubuntu 是通过 iptables 这个命令来打开的。具体可以参考这本书里这个章节的部分。

nginx 中 context 的概念

这篇文章 对于什么是 context 的概念讲得很清楚。从表面看就是用大括号包裹的内容,即是 context。比如 events context,http context 等等。

In Nginx parlance, the areas that these brackets define are called "contexts" because they contain configuration details that are separated according to their area of concern. Basically, these divisions provide an organizational structure along with some conditional logic to decide whether to apply the configurations within.

MIME types 的作用

"Nginx from beginner to pro" 这本书中,chapter 3,understanding the default configuration 里面有这样一段话:

MIME types describe the media type of content and guides the browser so that it renders the content appropriately in the browser instead of downloading the file.

之前社区的帖子就有这个问题,右键点击选择新页面打开图片的时候,会直接下载图片而不是在新页面打开照片。

配置跳转

最近有个需求,就是要求把原来 vb3 的路由都切到 vb4 上。比如 www.example.com/fruites/apple 需要跳转到 www.example.com/vb4/fruites/apple,然后 www.example.com/vegetables/cabbage 需要跳转到 www.example.com/vb4/vegetables/cabbage。最后决定在 nginx 里进行配置来解决这个问题,那么如何来配置呢。首先可以确定的是,肯定是在 server block 的 location 里进行配置。我最开始想的是用 proxy_pass 命令来进行配置,但是发现这样其实不行,因为希望做到的是用户访问的 url 也发生变化,所以就用 rewrite 命令来进行配置。 最后的配置效果如下

  location ^~ /fruites/ {
    rewrite ^/(.*) https://www.example.com/vb4/$1;
  }
  location ^~ /vegetables/ {
    rewrite ^/(.*) https://www.example.com/vb4/$1;
  }
  location ^~ /furniture/ {
    rewrite ^/furniture/(.*) https://www.example2.com/search?query=$1;
  }

这里面有几个点说一下,第一个是 ^~ 表示以 xx 开头的路径,然后 rewrite 接受两个参数,第一个参数是 正则,第二个参数是要重写的路由,而 $1 表示正则中第一个括号匹配的部分,那么类似 $2 就表示第二个括号匹配的部分。所以可以看到第三条的 furniture 的配置和前两条略有不同,因为第三条只需要 furniture 后面的内容,比如 https://www.exmaple.com/furniture/desk 需要跳转到 https://www.exmaple2.com/search?query=dest,只需要用到路由中的 desk,所以就只匹配 desk 然后跳转。 还有个问题就是说,线上有 4 台 nginx 机器,那么如何同时做到四台机器同步更新 nginx 配置呢? 之前有分享说过用一些工具,好像提到到类似 ansible 这样的运维工具,但是我之前对这些完全没有研究,目前的需求感觉也没有必要用这些工具,所以就写了个简单的脚本来操作。过程有这么几步:

  1. 通过 scp 命令,将任意一台 nginx 机器的配置下载到跳板机(注意,四台机器的配置是相同的,所以任意一台机器就可以)。
  2. 在跳板机上通过 vim 编辑当前 nginx 配置并保存。
  3. 执行一个脚本用来同时将这个更新后的配置发布到四台 nginx 机器上。脚本内容详见下面的 scpToMultiServer.sh
  4. 执行另一个脚本用来同时让四台 nginx 机器都执行 nginx -s reload 命令。 上面的 3 和 4 应该是可以合成同一步的,但是我没有去研究。先这么记录。 scpToMultiServer.sh 的内容:
    for dest in $(<destfile.txt); do
    scp ./nginx.conf "$dest"
    done

    这里面需要用到一个 destfile.txt 的文件,文件内容如下:

    root@ip1:/etc/nginx/conf.d/
    root@ip2:/etc/nginx/conf.d/
    root@ip3:/etc/nginx/conf.d/
    root@ip4:/etc/nginx/conf.d/

    注意,这里的 ip1, ip2... 需要换成你 nginx 机器的 ip,同时你要在跳板机上配置了这四台 nginx 机器的免密登陆。 然后是在四台 nginx 机器上同时执行 nginx -s reload 命令的脚本

    
    vhosts="
    root@ip1
    root@ip2
    root@ip3
    root@ip4
    "

for vhost in $vhosts; do echo $vhost ssh $vhost "nginx -s reload" echo "---------------------" done;


以上就是这次 nginx 配置的记录

### rewrite 指令
[rewrite rules](https://www.nginx.com/blog/creating-nginx-rewrite-rules/)

### nginx 结合 curl 来配置跳转
昨天 xiaolong 给我一个地址,http://www.virusbook.cn/domain/baidu.com,这个路径目前没有跳转到新的 vb4 页面。而是跳转到这个页面的 https 页面,就是 https://www.virusbook.cn/domain/baidu.com,页面内容是老的 vb3 页面。需求是要跳转到  https://x.threatbook.cn/nodev4/domain/baidu.com。最开始我定位问题的时候一直在考虑 http 跳转 https 的问题,定位到 nginx 的 80 端口配置。好长时间都没有获得解决。后来我想起用 curl 可以完全获取跳转过程还有详情的。所以就用 `curl --location -v http://www.virusbook.cn/domain/baidu.com` 这种方式去操作,果然返回了各个跳转的详细内容。一定位,觉得应该通过支持  https://www.virusbook.cn/domain/baidu.com 来跳转到 https://x.threatbook.cn/nodev4/domain/baidu.com 这个规则来支持跳转。所以 curl 很有用哦。
oakland commented 1 year ago

location regex

这个

Search-Order Modifier Description Match-Type Stops-search-on-match

 1st               =           The URI must match the specified pattern exactly                  Simple-string              Yes
 2nd               ^~          The URI must begin with the specified pattern                     Simple-string              Yes
 3rd             (None)        The URI must begin with the specified pattern                     Simple-string               No
 4th               ~           The URI must be a case-sensitive match to the specified Rx      Perl-Compatible-Rx      Yes (first match)                 
 4th               ~*          The URI must be a case-insensitive match to the specified Rx    Perl-Compatible-Rx      Yes (first match)
 N/A               @           Defines a named location block.                                   Simple-string              Yes