一般來說nginx是不支持靜態文件的post請求。會出現如下的錯誤:
curl -d 1=1 http://127.0.0.1:8081/test.html
<html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/0.5.35</center>
</body>
</html>
下面我們就來解決一下這個問題:
1首先需要重新編譯nginx 修改/nginx-1.8.0/src/http/modules/ngx_http_static_module.c文件
找到如下字段:
#endif
if (r->method & NGX_HTTP_POST) {
return NGX_HTTP_NOT_ALLOWED;
}
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
我們需要做的是將
if (r->method & NGX_HTTP_POST) {
return NGX_HTTP_NOT_ALLOWED;
}
註釋掉。。。。
重新編譯即可 做到這說明了nginx可以支持靜態文件的post的請求了
2.需要在nginx的主配置文件nginx.conf中 log_format 處配置成:
log_format access '$remote_addr\t$time_local\t`$request_uri`\t$content_length\t$status\t`$http_referer`\t`$http_user_agent`\t$http_x_forwarded_for\t$request_body';說明在日誌文件中顯示了post-body
3.nginx只有在使用了反向代理端口,它才能在日誌文件中記錄post-body 。可能只說你們不信。來咱們測試一下:
nginx的子配置文件我的配置如下:
server{
listen 8081;
location /{
proxy_pass http://192.168.2.180:82 ;
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
set $minutes $5;
# set $seconds $6;
} //此處是配置nginx的日誌文件按照分鐘切割日誌的。只有1.8.0以後的版本才支持在配置文件中配置切割日誌
access_log /tmp/logs/access-$year-$month-$day-$hour-$minutes.log access;
}
}
server{
listen 82;
root /tmp/bo ;
access_log /tmp/logs/test.log access ;
}
關於nginx支持靜態文件的post請求。需要做一個代理端口,例如80端口代理的是後臺服務81端口真正提供服務的端口是81,nginx只能在代理端口處能夠記錄post-body,使用代理端口的好處是能夠更好地提高nginx的性能。不會佔用太多的資源!!!
測試如下:我們真正服務的端口是82,代理端口8081,
記錄82端口的日誌是test.log 記錄8081端口的日誌是bo.log
我們來訪問以下代理端口和服務端口:
[root@server logs]# curl -d 1=1 http://127.0.0.1:82
this is a test page
[root@server logs]# tail -f test.log
127.0.0.117/Sep/2015:09:39:59 +0800`/`3200`-``curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2` - //此處不記錄post-body
^C
[root@server logs]# vim /opt/nginx/conf/vhost/bo.conf
[root@server logs]# curl -d 1=1 http://127.0.0.1:8081
this is a test page
[root@server logs]# tail -f bo.log
127.0.0.117/Sep/2015:09:52:04 +0800`/`3200`-``curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2` 1=1 //此處記錄post-body