【发布时间】:2018-09-16 09:32:19
【问题描述】:
我有一个带有 nginx 的 docker 映像。
我想记录传入的请求。
我的 docker-compose 文件:
nginx:
container_name: my-nginx
image: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf
- ./access.log:/var/log/nginx/test.com.access.log
我的 nginx 配置:
server {
listen 80;
listen 443 default_server ssl;
access_log /var/log/nginx/test.com.access.log;
location / {
proxy_pass http://myapp:8080;
proxy_buffering off;
}
}
当我尝试用以下配置替换 access_log 指令时:
log_format testlog '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" "$request_body"';
access_log /var/log/nginx/test.com.access.log testlog;
我明白了:
nginx-reverse-proxy | 2018/04/06 11:50:00 [emerg] 1#1: "log_format" directive is not allowed here in /etc/nginx/conf.d/default.conf:10
nginx-reverse-proxy | nginx: [emerg] "log_format" directive is not allowed here in /etc/nginx/conf.d/default.conf:10
【问题讨论】:
-
您不应该在同一个服务器块中拥有 HTTP 和 HTTPS 侦听器,而是为 HTTPS 创建一个单独的侦听器,包括所需的 SSL 密钥。