Nginx的405错误
Nginx将静态文件响应POST请求,提示405错误问题:
绝大多数服务器,都不允许静态文件响应POST请求(GET请求静态文件是天经地义的),否则会返回HTTP/1.1 405 Method not allowed错误。然而在前端开发中,前端开发工程师经常模拟后端请求,返回静态数据来查看页面效果,怎么办?
其实很简单,知道404和50x的错误定义吧,那么就是把405重新定义一下即可,如下:
error_page 405 =200 $uri;
测试环境局部配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| server {
listen 8443 ssl;
server_name test-rebuild.baidu.com;
ssl_certificate ca.crt;
ssl_certificate_key baidu.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/test-rebuild.aceess.log main;
root /usr/share/nginx/html;
index index.html index.htm;
error_page 405 =200 $uri; _
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
[https://gist.github.com/baskaran-md/e46cc25ccfac83f153bb](https://gist.github.com/baskaran-md/e46cc25ccfac83f153bb)
**server** {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 404 /404.html;
error_page 403 /403.html;
_
error_page 405 =200 $uri;
_
}
location ~ \.(action|jsp) {
root $testDataFold;
error_page 405 =200 $request_uri;
}
|