Nginx 到底发生了什么事情…
在进行调试时的方法
因为有时候一个设置文件的改变可能会导致行为差异较大,有可能让人感到困惑。所以如果能够查看nginx的运行情况就太好了。
希望设定环境
操作系统:CentOS7
安装nginx
yum install -y nginx
服务的自动启动设置和启动
在这里,不是使用nginx而是使用一个名为nginx-debug的服务是最初的难点。
在安装nginx时会默认带有它。它的运行方式与nginx服务相同。
systemctl enable nginx-debug && systemctl start nginx-debug
日志导出设置
要做的事情有两点:
修改日志的激活状态(没有这个的话,会导致调试困难)
调整日志级别
只有下面这两行应该是从原文件中进行更改的部分。
error_log /var/log/nginx/example-error.log debug;
rewrite_log on;
请同时备注修改后的文件。
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/example-error.log debug;
rewrite_log on;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
查看终端上的日志
能够确认日志如鬼魅般地流动。
tail -f /var/log/nginx/example-error.log
查看日志
一开始看到这个,甚至一点兴趣都丧失了,但每次的HTTP请求都会出现下面这样的响应头,所以可以将其视作一个分割点。
Server: nginx/1.15.2
Date: Sun, 02 Sep 2018 19:00:38 GMT
Content-Type: text/html
Content-Length: 185
Connection: close
Location: https://localhost/