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

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

[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_filesize4.访问 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/html5.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<

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;

}

}

EOF6.启动 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

相关推荐

卖香烟的网站有没有?这些正规渠道你一定要知道
beat365正版唯一官网

卖香烟的网站有没有?这些正规渠道你一定要知道

🕒 08-23 👁️ 4826
亩产2641公斤!世界玉米高产纪录带给我们的思考→
0365cc彩票APP官方版下载

亩产2641公斤!世界玉米高产纪录带给我们的思考→

🕒 02-07 👁️ 1755