前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >5.Nginx常遇问题入坑出坑整理

5.Nginx常遇问题入坑出坑整理

作者头像
全栈工程师修炼指南
发布2022-09-29 18:48:39
2.2K0
发布2022-09-29 18:48:39
举报

[TOC]

0x00 Nginx 安装与编译

1.在Ubuntu系统上进行Nginx源码编译安装时指定了–with-stream_geoip_module模块报error: the GeoIP module requires the GeoIP library.错误信息,解决办法。

错误信息: 安装的nginx的geo的模块在编译nginx的时候遇到报错,报错信息如下:

代码语言:javascript
复制
./configure: error: the GeoIP module requires the GeoIP library.
You can either do not enable the module or install the library.

解决办法:apt-get install libgeoip-dev


0x01 Nginx 启动与使用

1.启动Nginx后报nginx: [emerg] unknown log format "main" in错误提示,解决办法。

错误信息: 在执行 nginx -s reload 后续出现了 nginx: [emerg] unknown log format "proxy_log" in /usr/local/macports/etc/nginx/nginx.conf:147 错误问题。

问题原因: 默认的 nginx 配置中的 log_format 选项被注释。

解决办法: 打开 nginx.conf 将 log_format 选项前面的#去掉。

代码语言:javascript
复制
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

2.使用Nginx代理转发php后端应用时报 Nginx 502 Bad Gateway错误问题解决。

  • 错误信息: Nginx 502 Bad Gateway_
  • 问题原因: php-cgi进程数不够用、php执行时间长(mysql慢)、或者是php-cgi进程死掉,都会出现502错误与php-fpm.conf的设置有关;而Nginx 504 Gateway Time-out则是与nginx.conf的设置有关。
  • 检查流程:

查看当前的PHP FastCGI进程数是否够用: netstat -anpo | grep "php-cgi" | wc -l

部分PHP程序的执行时间超过了Nginx的等待时间,可以适当增加nginx.conf配置文件中FastCGI的timeout时间:

代码语言:javascript
复制
http {
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
}

3.Nginx 启动时报413 Request Entity Too Large错误问题解决。

错误信息:413 Request Entity Too Large

错误原因: client_max_body_size:指令指定允许客户端连接的最大请求实体大小,它出现在请求头部的Content-Length字段. 如果请求大于指定的值,客户端将收到一个”Request Entity Too Large” (413)错误

解决办法:

代码语言:javascript
复制
#conf增大
client_max_body_size
#php.ini中增大
post_max_size 和upload_max_filesize

4.访问 Nginx 提供的网页时报403 forbidden错误问题解决。

问题描述: directory index of “/xx/xx/xx/“ is forbidden,网页访问403; 解决思路:

代码语言:javascript
复制
# nginx.conf
autoindex on;  #进行列目录查看是否可以列目录为排错准备;

# 需要排查的思路
1.selinux
2.目录下有没有index.html 文件(如果有就需要配套有 index  index.html index.php)
3.权限问题 chown -R nginx:www-data /var/www/html

5.Nginx 成功配置虚拟主机并且启动nginx可以看见有nginx线程存在但是无监听端口

问题原因: 由于在设置多个虚拟主机的时候在nginx.conf主配置文件中去掉了server {…} 添加的 include domains/*,其中domains目录不是在于conf/子目录中;

解决办法:

代码语言:javascript
复制
mkdir -vp /usr/local/nginx/conf/domains
cat>/usr/local/nginx/conf/domains/v1.weiyigeek.top.conf<<EOF
server {
    listen       80;
    server_name  $NGX_VHOSTS;

    location / {
        root   html/$NGX_VHOSTS;
        index  index.html index.htm;
    }
    #Nginx 监控模块启用
    location /nginxStatus {
      stub_status;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
EOF

6.启动 nginx 时报 [error] invalid PID number "" in "/usr/local/var/run/nginx.pid错误解决办法

错误原因: nginx根本就没有启动过,所以pid文件的值为空没法平滑启动,先启动了才能平滑启动。

解决方法:

代码语言:javascript
复制
解决方案1:sudo nginx -s reload -c  /usr/local/etc/nginx/nginx.conf
解决方案2:先启动nginx然后测试配置文件语法无误后再进行重载。

7.使用Nginx判断无效$host变量时返回JSON文本字符串便直接下载而非在页面显示。

解决办法:

代码语言:javascript
复制
location ^~ / {
  # JSON 返回
  if ( $host !~* weiyigeek\.top ) {
  add_header Content-Type 'application/json; charset=utf-8';
  return 200 '{"status":"error","Author":"WeiyiGeek","Site":"https://www.weiyigeek.top","Chinese":"大佬, 请不要把你的域名解析到我的服务器上","English":"Friend, Please do not resolve your domain name to my server"}';
  # return 301 https://space.bilibili.com/385802642;
  }

  # html 返回
  if ($host !~* weiyigeek\.top) {
    add_header Content-Type 'text/html;charset=utf-8'
    return 200 'Warnning, This domain not is www.weiyigeek.top!';  
  }
}

温馨提示: 在百度中搜索的方法中说使用default_type text/html;关键字来默认指定显示文档类型,但在最新的1.21.6版本中会报错,例如。

代码语言:javascript
复制
location ~ ^/weiyigeek/(.*)_(\d+).html$ { 
  default_type text/html; 
  set $s $1; 
  set $d $2; 
  return 200 str:$s$d; 
}

8.在动态生成Nginx日志路径配置后报[emerg] buffered logs cannot have variables in name错误问题解决。

问题原因: 如果设置缓存写入日志,则不支持路径中存在变量。 解决办法:

代码语言:javascript
复制
# 不正确写法
access_log /var/log/nginx/access-${logdate}.log main buffer=128k gzip flush=1m;;  

# 正确写法
access_log /var/log/nginx/access-${logdate}.log main;

补充说明: 通常设置以日期分隔日志, 如果执行 nginx 的用户权非root用户则可能包如下错误,此时我们需要赋予其可以修改日志(不建议使用root,此处假设使用nginx用户), 然后重启nginx。

代码语言:javascript
复制
# 错误信息
2022/04/12 14:22:00 [crit] 594763#594763: *55883 open() "/var/log/nginx/ip-2022-04-12.log" failed (13: Permission denied) while logging request, client: 71.6.232.7, server: 82.15.1.23, request: "GET / HTTP/1.1", host: "82.15.1.23"

# 解决办法
$ chown -R nginx:root /var/log/nginx
$ nginx -s reload
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0x00 Nginx 安装与编译
    • 1.在Ubuntu系统上进行Nginx源码编译安装时指定了–with-stream_geoip_module模块报error: the GeoIP module requires the GeoIP library.错误信息,解决办法。
    • 0x01 Nginx 启动与使用
      • 1.启动Nginx后报nginx: [emerg] unknown log format "main" in错误提示,解决办法。
        • 2.使用Nginx代理转发php后端应用时报 Nginx 502 Bad Gateway错误问题解决。
          • 3.Nginx 启动时报413 Request Entity Too Large错误问题解决。
            • 4.访问 Nginx 提供的网页时报403 forbidden错误问题解决。
              • 5.Nginx 成功配置虚拟主机并且启动nginx可以看见有nginx线程存在但是无监听端口
                • 6.启动 nginx 时报 [error] invalid PID number "" in "/usr/local/var/run/nginx.pid错误解决办法
                  • 7.使用Nginx判断无效$host变量时返回JSON文本字符串便直接下载而非在页面显示。
                    • 8.在动态生成Nginx日志路径配置后报[emerg] buffered logs cannot have variables in name错误问题解决。
                    相关产品与服务
                    轻量应用服务器
                    轻量应用服务器(TencentCloud Lighthouse)是新一代开箱即用、面向轻量应用场景的云服务器产品,助力中小企业和开发者便捷高效的在云端构建网站、Web应用、小程序/小游戏、游戏服、电商应用、云盘/图床和开发测试环境,相比普通云服务器更加简单易用且更贴近应用,以套餐形式整体售卖云资源并提供高带宽流量包,将热门软件打包实现一键构建应用,提供极简上云体验。
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档