Nginx踩坑指南

1. 官方操作手册

参考链接:http://nginx.org/en/linux_packages.html

2.nginx常用命令


# 快速关闭Nginx,可能不保存相关信息,并迅速终止web服务
nginx -s stop
# 平稳关闭Nginx,保存相关信息,有安排的结束web服务
nginx -s quit
# 因改变了Nginx相关配置,需要重新加载配置而重载
nginx -s reload
# 重新打开日志文件
nginx -s reopen
# 为 Nginx 指定一个配置文件,来代替缺省的
nginx -c filename
# 不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件
nginx -t
#  显示 nginx 的版本
nginx -v
# 显示 nginx 的版本,编译器版本和配置参数
nginx -V
# 格式换显示 nginx 配置参数
2>&1 nginx -V | xargs -n1
2>&1 nginx -V | xargs -n1 | grep lua

3. 相关问题

3.1 问题1

nginx: [emerg] "server" directive is not allowed here in /etc/nginx/nginx.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed

答案:
这不是一个nginx配置文件,而是一个vhost配置文件,应该放在/etc/nginx/conf.d(或者/etc/nginx/sites-enabled,如果您喜欢这种类型的vhost配置文件组织;请查看sites-available与sites-enabled与conf.d目录thread在ServerFault中的差异,以找出差异)。您可以在nginx GitHub镜像中检查整个nginx配置文件示例。

这些vhost配置文件通常包含在http配置级别的主配置文件中:

http {
    ...
    include /etc/nginx/conf.d/*.conf;
}

Debian-based包(或使用这些sites-enabled/sites-available目录的任何其他包)在那里还有一行:

http {
    ...
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

要查找特定Ubuntu发行版的nginx.conf原始文件,请下载相应的nginx-common包并检查其内容。

3.2 问题2

在使用nginx反向代理POST请求的时候,错误配置情况

location /sms/sendSmsLocal/ {
  proxy_pass http://localhost:10086/sms/sendSmsLocal/;
}

这样配置会出现的问题

原本发出的请求是POST请求,经过nginx转发以后就会变成GET请求
原本POST请求携带的请求体经过转发以后会丢失,正确配置代理POST请求

location /sms/sendSmsLocal {
  proxy_pass http://localhost:10086/sms/sendSmsLocal;
  proxy_redirect off;
}

location 后边跟的路径不要以 / 结尾
由于nginx代理会将我们的请求拦截后在做一次重定向,在这个过程中我们的请求携带的请求体就会丢失,解决办法就是屏蔽掉转发 通过 proxy_redirect off; 可以解决

3.3 Nginx中proxy_pass末尾带斜杠/和不带的区别

1、proxy_pass末尾无斜杠
nginx配置proxy_pass时url末尾带/不带/的区别如下:

注意:当location为正则表达式匹配模式时,proxy_pass中的url末尾是不允许有/的,因此正则表达式匹配模式不在讨论范围内。

proxy_pass配置中url末尾带/时,nginx转发时,会将原uri去除location匹配表达式后的内容拼接在proxy_pass中url之后。

测试地址:http://192.168.171.129/test/tes.jsp

场景一:

location ^~ /test/ {
 proxy_pass http://192.168.171.129:8080/server/;
}

代理后实际访问地址:http://192.168.171.129:8080/server/tes.jsp

场景二:

location ^~ /test {
 proxy_pass http://192.168.171.129:8080/server/;
}

代理后实际访问地址:http://192.168.171.129:8080/server//tes.jsp

场景三:

location ^~ /test/ {
 proxy_pass http://192.168.171.129:8080/;
}

代理后实际访问地址:http://192.168.171.129:8080/tes.jsp

场景四:

location ^~ /test {
 proxy_pass http://192.168.171.129:8080/;
}

代理后实际访问地址:http://192.168.171.129:8080//tes.jsp

proxy_pass配置中url末尾不带/时,如url中不包含path,则直接将原uri拼接在proxy_pass中url之后;如url中包含path,则将原uri去除location匹配表达式后的内容拼接在proxy_pass中的url之后。

测试地址:http://192.168.171.129/test/tes.jsp

场景一:

 location ^~ /test/{
 proxy_pass http://192.168.171.129:8080/server;
 }

代理后实际访问地址:http://192.168.171.129:8080/servertes.jsp

场景二:

location ^~ /test {
 proxy_pass http://192.168.171.129:8080/server;
}

代理后实际访问地址:http://192.168.171.129:8080/server/tes.jsp

场景三:

location ^~ /test/ {
 proxy_pass http://192.168.171.129:8080;
}

代理后实际访问地址:http://192.168.171.129:8080/test/tes.jsp

场景四:

location ^~ /test {
 proxy_pass http://192.168.171.129:8080;
}

代理后实际访问地址:http://192.168.171.129:8080/test/tes.jsp

总体流程如下:

名词解释
url:proxy_pass中地址
uri: 原始请求地址

proxy_pass配置中
url末尾是否带/时
nginx转发时,会将原uri
去除location匹配表达式后
的内容拼接在proxy_pass中
url之后
url中是否包含path
将原uri去除location
匹配表达式后的内容拼接
在proxy_pass中的url之后
直接将原uri拼接在
proxy_pass中url之后

3.4 问题4

问题描述:

安装完nginx后,执行/usr/sbin/nginx -s reload 报错: nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
原因:
由于没有nginx.pid 文件,当每一次安装或每次当我们停止nginx时(nginx -s stop) ,nginx 会把 /usr/local/var/run/ 路径下名为nginx.pid 的文件删掉,所以重启会报没有这个文件导致起不来。

解决方案

# 执行nginx -t确认nginx配置文件路径。
[root@node-03 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 指定conf文件重新生成。
[root@node-03 conf.d]# nginx -c /etc/nginx/nginx.conf
# 重新执行正常
[root@node-03 conf.d]# nginx -s reload

4.正则匹配

= :进行普通字符精确匹配,也就是完全匹配。
^~ :表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其它 location。
~ :区分大小写的匹配。
~* :不区分大小写的匹配。
!~ :区分大小写的匹配取非。
!~* :不区分大小写的匹配取非。

4.1 优先级

在这里插入图片描述

4.2 示例

(1)location = / {}
=为精确匹配 / ,主机名后面不能带任何字符串,比如访问 / 和 /data,则 / 匹配,/data 不匹配
再比如 location = /abc,则只匹配/abc ,/abc/或 /abcd不匹配。若 location  /abc,则即匹配/abc 、/abcd/ 同时也匹配 /abc/。
 
(2)location / {}
因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求 比如访问 / 和 /data, 则 / 匹配, /data 也匹配,
但若后面是正则表达式会和最长字符串优先匹配(最长匹配)
 
(3)location /documents/ {}
匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索其它 location
只有其它 location后面的正则表达式没有匹配到时,才会采用这一条
 
(4)location /documents/abc {}
匹配任何以 /documents/abc 开头的地址,匹配符合以后,还要继续往下搜索其它 location
只有其它 location后面的正则表达式没有匹配到时,才会采用这一条
 
(5)location ^~ /images/ {}
匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条
 
(6)location ~* \.(gif|jpg|jpeg)$ {}
匹配所有以 gif、jpg或jpeg 结尾的请求
然而,所有请求 /images/ 下的图片会被 location ^~ /images/ 处理,因为 ^~ 的优先级更高,所以到达不了这一条正则
 
(7)location /images/abc {}
最长字符匹配到 /images/abc,优先级最低,继续往下搜索其它 location,会发现 ^~ 和 ~ 存在
 
(8)location ~ /images/abc {}
匹配以/images/abc 开头的,优先级次之,只有去掉 location ^~ /images/ 才会采用这一条
 
(9)location /images/abc/1.html {}
匹配/images/abc/1.html 文件,如果和正则 ~ /images/abc/1.html 相比,正则优先级更高

4.3 常用正则表达式

^ :匹配输入字符串的起始位置
$ :匹配输入字符串的结束位置
* :匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll”
+ :匹配前面的字符一次或多次。如“ol+”能匹配“ol”及“oll”、“olll”,但不能匹配“o”
? :匹配前面的字符零次或一次,例如“do(es)?”能匹配“do”或者“does”,”?”等效于”{0,1}”
. :匹配除“\n”之外的任何单个字符,若要匹配包括“\n”在内的任意字符,请使用诸如“[.\n]”之类的模式
\ :将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n”匹配一个换行符,而“\$”则匹配“$”
\d :匹配纯数字
{n} :重复 n 次
{n,} :重复 n 次或更多次
{n,m} :重复 n 到 m 次
[] :定义匹配的字符范围
[c] :匹配单个字符 c
[a-z] :匹配 a-z 小写字母的任意一个
[a-zA-Z0-9] :匹配所有大小写字母或数字
() :表达式的开始和结束位置
| :或运算符

参考资料
https://blog.csdn.net/qq_20147683/article/details/125501184
https://mp.weixin.qq.com/s/XoqGvYBabW8YBl9xEeNYZw
https://www.5axxw.com/questions/content/ndyexm
http://www.huazhaox.com/article/908
https://blog.csdn.net/lihefei_coder/article/details/121033283
https://www.yingsoo.com/news/servers/45036.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值