在 rails 项目中使用 grape 写 api,使用 rack-cors 在 development 环境下没问题,但到了 production 环境下,所有 PUT、DELETE 请求都是 405,而 PUT、DELETE 请求之前的 OPTIONS 都是 200
在 application.rb 中
config.middleware.insert_before ActionDispatch::Static, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :delete, :put, :options]
end
end
nginx 配置:
upstream my_unicorn {
server unix:/srv/www/myapp/shared/sockets/unicorn.sock;
}
server {
listen 80;
server_name www.mysite.com;
access_log /var/log/nginx/myapp.access.log;
root /srv/www/myapp/current/public/;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://my_unicorn;
}
}