Open oakland opened 6 years ago
看 这个
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
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
重启 nginxnginx -s stop
关闭 nginxnginx -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
这篇博客就是说了如何解决这个问题的过程。 其他相关的文章还有:
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 等等。
MIME types 的作用
"Nginx from beginner to pro" 这本书中,chapter 3,understanding the default configuration 里面有这样一段话:
之前社区的帖子就有这个问题,右键点击选择新页面打开图片的时候,会直接下载图片而不是在新页面打开照片。
配置跳转
最近有个需求,就是要求把原来 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 命令来进行配置。 最后的配置效果如下
这里面有几个点说一下,第一个是 ^~ 表示以 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 这样的运维工具,但是我之前对这些完全没有研究,目前的需求感觉也没有必要用这些工具,所以就写了个简单的脚本来操作。过程有这么几步:nginx -s reload
命令。 上面的 3 和 4 应该是可以合成同一步的,但是我没有去研究。先这么记录。 scpToMultiServer.sh 的内容:这里面需要用到一个 destfile.txt 的文件,文件内容如下:
注意,这里的 ip1, ip2... 需要换成你 nginx 机器的 ip,同时你要在跳板机上配置了这四台 nginx 机器的免密登陆。 然后是在四台 nginx 机器上同时执行
nginx -s reload
命令的脚本for vhost in $vhosts; do echo $vhost ssh $vhost "nginx -s reload" echo "---------------------" done;