近期做一個手機端靜態網站,在wcm上網站預覽的時候顯示正常,網站數據發布到nginx網站服務上后,發現頁面有部分不顯示:
正常頁面:
錯誤頁面:
進入谷歌瀏覽器的Developer Tools(F12鍵),進入Network探測
查到錯誤信息:Request Method:POST Status Code:405 Not Allowed
網上傳抄的添加以下配置的解決辦法不可用:
error_page 405 =200 @405; location @405 { root /data/jcyweb/hnjcy; }
upstream static_backend { server localhost:80; } server { listen 80; # ... error_page 405 =200 @405; location @405 { root /data/jcyweb/hnjcy; proxy_method GET; proxy_pass http://static_backend; } }
即轉換靜態文件接收的POST請求到GET方式
最后的配置文件:

#user nobody; worker_processes auto; ##允許Nginx進程生成的worker process數 根據cpu內核數自動生成 #error_log logs/error.log #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { use epoll; ##配置事件驅動模型 worker_connections 2048; ##配置最大連接數 } http { include mime.types; ##定義MIME-Type default_type application/octet-stream; log_format main '$remote_addr - [$time_local] - "$request"' ##定義日志類型,遠程地址、時間、請求內容、請求狀態 ' - $status'; types_hash_max_size 2048; ##多servername需設置 server_names_hash_bucket_size 512; sendfile on; server_tokens off; ##關閉默認版本號 keepalive_timeout 20; ##定義鏈接超時時間,防止慢速dos攻擊 client_header_timeout 15; ##請求頭超時時間 client_body_timeout 15; ##請求體超時時間 reset_timedout_connection on; ##關閉不響應的客戶端連接,釋放客戶端占有內存 send_timeout 10; ##客戶端兩次讀取時間,如果這段時間里沒有讀取任何數據,nginx就會關閉連接 upstream static_backend { server localhost:80; } server { ##直接訪問IP 反饋500錯誤 listen 80; location / { root /data/jcyweb/hnjcy; index index.html; ssi on; ssi_silent_errors on; ssi_types text/shtml; } error_page 404 /404.html; location = /404.html { root html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } error_page 405 =200 @405; location @405{ root /data/jcyweb/hnjcy; proxy_method GET; proxy_pass http://static_backend; } } }
還有一更好的方法:修改源代碼,重新編譯安裝nginx
編輯nginx源代碼
[root@localhost ~]# vim src/http/modules/ngx_http_static_module.c
修改: 找到下面一段注釋掉
/* if (r->method & NGX_HTTP_POST) { return NGX_HTTP_NOT_ALLOWED; } */
然后按照原來的編譯參數,重新編譯安裝nginx,即可