NGINX 如何记录 localhost 的访问日志

2022-10-12 12:33:52 +08:00
 shanghai1943

我在 /etc/nginx/conf.d 目录下新建了 一个 localhost.conf 配置文件,内容如下

server {

   listen 12345;

   server_name localhost 127.0.0.1;

   access_log /var/log/nginx/localhost.log main;

}

nginx -t 和 nginx -s reload 之后,在同台机器用 curl 发起了请求

curl http://localhost:12345/abc

以及

curl http://127.0.0.1:12345/abc

在 /var/log/nginx 目录下能看到 localhost.log 文件,但是没有任何内容。

有大佬能指点一下的么?谢谢。

611 次点击
所在节点    问与答
5 条回复
iPc666
2022-10-12 19:31:24 +08:00
估计是访问不存在,走到了 errorlog 里。去 /var/log/nginx/下的 error.log 里找
shanghai1943
2022-10-13 10:13:16 +08:00
@iPc666 #1 在 /var/log/nginx/ 下 grep abc * 啥都没有。。
iPc666
2022-10-13 11:16:10 +08:00
在 access_log 后面加上这句话

# Logging
error_log /var/log/nginx/error.log warn;
log_format main '$remote_addr - [$time_local] "$request" "$request_time" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$request_length" "$http_host"';
shanghai1943
2022-10-13 11:39:25 +08:00
@iPc666 #3 貌似还是不行。error.log 里没有相应的错误信息。 另:log_format 这个不支持在 server directive 里添加,所以就没加。

nginx: [emerg] "log_format" directive is not allowed here in /etc/nginx/conf.d/localhost.conf:9
iPc666
2022-10-13 13:05:22 +08:00
加在 nginx.conf 的 http 里试试

nginx.conf 文件

http {
其他配置....

# Logging
error_log /var/log/nginx/error.log warn;
log_format main '$remote_addr - [$time_local] "$request" "$request_time" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$request_length" "$http_host"';
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/886339

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX