在 Rails 5.2、SSL 环境中,使用 Nginx 作为反向代理运行时,每当我提交表单时都会收到错误:
HTTP Origin header (https://agro2business.com.br) didn't match request.base_url (https://agro2business.com.br, agro2business.com.br)
Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken)
我在另一个 StackOverflow 问题中读到过有关配置参数和 Nginx 以传递 header 的信息,但到目前为止还没有运气。我的 nginx 配置文件:
proxy_pass http://localhost:4000; }
location / {
proxy_pass http://localhost:4000; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Real-Port $server_port; proxy_set_header X-Real-Scheme $scheme; proxy_set_header X-NginX-Proxy true; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Ssl on; }
为什么 Rails 尝试将 header 与两个值进行比较?
request.base_url (https://agro2business.com.br, agro2business.com.br)
最佳答案
我的问题是,在我的 nginx 配置中,我设置了 header Host 两次,这导致了 url 生成误导,进而导致表单提交无效。
proxy_pass http://localhost:4000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
**proxy_set_header Host $http_host;**
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
**proxy_set_header Host $http_host;**
proxy_set_header X-Real-Port $server_port;
proxy_set_header X-Real-Scheme $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Ssl on;
只需删除两个 proxy_set_header Host $http_host; 中的一个就可以了
关于ruby-on-rails - 生产环境 Nginx 中的 Rails 5 ActionController::InvalidAuthenticityToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56025237/