防火牆內的內網服務器,因為網關傳過來的remot_addr都一樣,不得不對Nginx的日志格式做了配置
配置語法如下:
- log_format myformat '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
- '"$status" $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for" '
- '"$gzip_ratio" $request_time $bytes_sent $request_length';
上面這只是定義一個格式,要使用這個格式,很多人采用server里面加access_log,其實不用,直接http里面的access_log指令出改配置,可以改全局的日志格式:
- access_log /var/log/nginx/access.log myformat;
如果你的nginx服務器內部有其他容器,可以直接把容器傳參的params設置中的
#$remote_addr
$remote_addr也直接改掉,這樣在容器里面少折騰一點。
順便談談Nginx的配置調試,你使用reload或者restart裝載新配置,如果新配置格式有誤,它不會報錯,可以啟動成功,所以改了配置以后,最好用stop和start指令來,這樣,啟動失敗,就可以確定為配置格式問題,否則,新配置不會生效,你也不知道是配置格式問題。
參考資料:
http://www.ttlsa.com/Linux/the-nginx-log-configuration/
http://www.nginx.cn/273.html