nginx等绝大多数web服务器,都不允许静态文件响应POST请求,否则会返回“HTTP/1.1 405 Method not allowed”错误,但业务需要,使用post请求静态文件(主要是app代码改麻烦,还需要发布)
百度上方法大多不好用
如:
upstream static_backend {
server localhost:80;
}
server {
listen 80;
# ...
error_page 405 =200 @405;
location @405 {
root /srv/http;
proxy_method GET;
proxy_pass http://static_backend;
}
}
本次使用重定向方法,把post转为get,因为改代理特殊性,都可以用get,所以可以使用本方法
nginx部分配置如下:
location / {
root /www/wwwroot;
index index.html index.htm;
proxy_method POST;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.html?s=$1 last;
}
if ($request_method = POST) {
rewrite ^/(.*)$ /$1 permanent;
}
}
访问成功