nginx 常见问题

1,030 阅读7分钟

1.nginx无法root和alias设置错误

1.问题描述

location / { 
    alias   html/xxx/index/dev;
}
location /admin {
    alias  html/xxxx/admin/dev;
}
location /admin2 {
    alias  html/xxxx/admin2/dev;
}

别名alias 定义在 location / 导致无法响应。

2.概念

1. root 指的是 完整的路径

location /c/ {
     root /a/
}

对应路径是绝对的 /a/c/

2. alias 指的是虚拟路径

location /c/ {
     alias /a/
}

对应路径是 /c/ 是虚拟的,只访问/a/ 下面的文件

3.location匹配

  1. location / 匹配所有
  2. location = / 单独匹配 /
  3. location ~ / 匹配所有,可以和 location = / 搭配使用

3.总结

  1. 所以配置的时候 根路径需要使用 root 其他使用alias .
  2. root结尾/可有可无 , alias后面必须加 /
location / { 
    root   html/xxx/index/dev;
}
location /admin {
    alias  html/xxxx/admin/dev;
}
location /admin2 {
    alias  html/xxxx/admin2/dev;
}

2.nginx无法stop或reload

使用/usr/local/sbin/nginx -s reload 重新读取配置文件出错

[root@localhost nginx]/usr/local/nginx/sbin/nginx -s reload\
提示 nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"

[root@localhost nginx]# cd logs\
[root@localhost logs]# ls\
access.log  error.log  nginx-access.log  nginx_error.log\
#果然没有/usr/local/nginx/logs/nginx.pid 文件  
# nginx.pid 这是nginx启动的时候生成,用来记录master进程 的id文件

解决方法1:通过-c命令重新链接配置文件

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

解决方法2:在conf文件 添加pid指定

#在头部去掉注释,使用默认路径
pid        logs/nginx.pid;

3.nginx一直在启动中

1../nginx -t 检查配置文件

./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

显示配置正常

2.tail -100f log/error.log 查看启动信息

2022/05/19 11:13:41 [notice] 11001#0: signal 17 (SIGCHLD) received from 41125
2022/05/19 11:13:41 [alert] 11001#0: worker process 41125 exited on signal 11 (core dumped)
2022/05/19 11:13:41 [notice] 11001#0: start worker process 41150
ngx_http_fastdfs_process_init pid=41150
2022/05/19 11:13:41 [notice] 11001#0: signal 17 (SIGCHLD) received from 41126
2022/05/19 11:13:41 [alert] 11001#0: worker process 41126 exited on signal 11 (core dumped)
2022/05/19 11:13:41 [notice] 11001#0: start worker process 41151
ngx_http_fastdfs_process_init pid=41151
2022/05/19 11:13:41 [notice] 11001#0: signal 29 (SIGIO) received
2022/05/19 11:13:41 [notice] 11001#0: signal 17 (SIGCHLD) received from 41123
2022/05/19 11:13:41 [alert] 11001#0: worker process 41123 exited on signal 11 (core dumped)
2022/05/19 11:13:41 [notice] 11001#0: start worker process 41152
2022/05/19 11:13:41 [notice] 11001#0: signal 29 (SIGIO) received
ngx_http_fastdfs_process_init pid=41152
2022/05/19 11:13:41 [notice] 11001#0: signal 17 (SIGCHLD) received from 41129
2022/05/19 11:13:41 [alert] 11001#0: worker process 41129 exited on signal 11 (core dumped)
2022/05/19 11:13:41 [notice] 11001#0: signal 29 (SIGIO) received
2022/05/19 11:13:41 [notice] 11001#0: start worker process 41157
ngx_http_fastdfs_process_init pid=41157
2022/0

显示nginx一直在启动,一直打印日志,有ngx_http_fastdfs_process_init 初始化信息

3.通过命令ps -ef | grep nginx 查看nginx启动状态

ps -ef | grep nginx
root      11001      1  0 May18 ?        00:00:00 nginx: master process ./nginx
root      42871  11001  0 11:16 ?        00:00:00 nginx: master process ./nginx
root      42872  11001  0 11:16 ?        00:00:00 nginx: master process ./nginx
root      42873  11001  0 11:16 ?        00:00:00 nginx: master process ./nginx
root      42883  11001  0 11:16 ?        00:00:00 nginx: master process ./nginx
root      42885  36886  0 11:16 pts/0    00:00:00 grep --color=auto nginx

居然显示多个master进程

4.curl 本地127.0.0.1 也无响应

curl http://127.0.0.1:8080

5.浏览器访问-一直出于等待中

image.png

6.firewall-cmd --state检查防火墙是否开启

firewall-cmd --state
not running

没开启

7.通过./nginx -V发现fastdfs模块

./nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
configure arguments: --prefix=/usr/local/nginx --with-http_gzip_static_module --with-http_image_filter_module --add-module=/usr/local/fastdfs/fastdfs-nginx-module-1.22/src

8.确认了问题

在启动的时候nginx需要加载fastdfs模块,fdfs_trackerd模块被关闭了。

fastdfs需要启动两个模块。

  • 一个是fdfs_trackerd
  • 一个是fdfs_storaged
sudo service fdfs_trackerd start # 启动fdfs_trackerd服务,停止用stop
service fdfs_storaged start  # 启动fdfs_storaged服务,停止用stop

9.解决办法service fdfs_trackerd start

通过 service fdfs_trackerd start 启动fdfs服务,然后

service fdfs_trackerd start  # 启动fdfs_trackerd服务,停止用stop
Starting fdfs_trackerd (via systemctl):                    [  OK  ] 

10.重启nginx

./nginx 

启动正常

11.验证检查进程

ps -ef | grep nginx
root      54033      1  0 11:40 ?        00:00:00 nginx: master process ./nginx
root      54758  54033  0 11:41 ?        00:00:00 nginx: worker process
root      54759  54033  0 11:41 ?        00:00:00 nginx: worker process
root      54760  54033  0 11:41 ?        00:00:00 nginx: worker process
root      54761  54033  0 11:41 ?        00:00:00 nginx: worker process
root      54995  36886  0 11:42 pts/0    00:00:00 grep --color=auto nginx

一个master 多个worker 正常。

12验证网页

打开谷歌浏览器愉快的正常访问了

4.问题,接口返回net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK)

1.问题描述

  • nginx 代理服务器接口后,调用上传接口,服务器返回。单独使用postman请求接口能正常回访信息。
  • 控制台输出: net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK) image.png

image.png

2.解决方法

nginx 代理时添加

proxy_http_version 1.1;
proxy_set_header Connection "";

5.nginx转发tomcat服务到二级目录

已知 项目根目录已经被其他内容使用,tomcat的服务只能放在二级目录 直接用nginx转发报错

 server {
        listen       9022;
        server_name localhost;
        location / {
            root   html/project;
             proxy_pass http://127.0.0.1:17280; 
        }
    }

不同同时设置root 和 proxy_pass ,最后只会以最后执行代码为主。proxy_pass覆盖了 root

解决办法,另外定义一个路径专门给tomcat


 server {
        listen       9022;
        server_name localhost;
        location / {
            root   html/project; 
        }
        location /tomcat {
              proxy_pass http://127.0.0.1:17280; //这里代理的是tomocat的服务
              rewrite ^/tomcat/(.*) /$1 break; #这里重定向是关键  
              proxy_set_header Host $host;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        } 
    }

因为tomcat解析 是根据当前url路径执行的,所以需要把tomcat这个路径在重定向的时候去除。

6.The 'Access-Control-Allow-Origin' header contains multiple values'*, *', but only one is allowed.

image.png multiple values "*" 意思就是设置了2次跨域,但是只有一个是允许的,移除其中的任意一个就好了。如果服务器设置了允许跨域,使用Nginx代理里面就不需要了(或者就不用使用Nginx了)

#add_header Access-Control-Allow-Origin *; 注释配置的跨域设置即可

7.nginx上传大文件提示 413 (too large)

问题

上传大文件30m左右提示 413 (too large)

原因

nginx默认上传大小限制1m

解决办法

新增配置头 client_max_body_size 100m;

8.nginx 读取webpack压缩好的xx.js.gz文件无效

问题

nginx服务上已经上传好 app.js 和 app.js.gz ,但是服务器就是不读取app.js.gz,每次都动态生成gz文件返回。

原因

nginx 没有打开giz on; 和 gzip_static on;

解决办法

在nginx配置

9.nginx 提示 unknown directive "gzip_static" in xxx

问题

nginx启动提示gzip_static错误

原因

没有安装http_gzip_static_module 模块

解决办法

在nginx添加配置

./configure --prefix=/usr/local/nginx --with-http_gzip_static_module