我使用Nginx来提供静态页面,但将请求传递给API,我想处理GET和DELETE请求。
GET工作正常,但DELETE请求被拒绝;405:"不允许的方法";
server {
listen 80;
server_name test.com;
root /var/test/cache/;
index index.php;
add_header 'Access-Control-Allow-Methods' 'GET, DELETE';
add_header 'Access-Control-Allow-Headers' 'Content-Type, Origin';
add_header 'Access-Control-Allow-Origin' '*';
location ~* /questions {
...
try_files $url /index.php;
}
}
任何建议都会有帮助,谢谢。
解决方案:
修复fastcgi_params文件上的一个问题,并改进位置规则条目:
server {
listen 80;
server_name test.com;
root /var/test/cache/;
index index.php;
location ~* /questions {
set_by_lua_block $url_format {
request_type = ngx.var.request_method
if request_type == 'DELETE' then
...
return url_format;
end
...
}
try_files $url_format /index.php;
}
}