gpt4 book ai didi

ruby-on-rails - 生产环境 Nginx 中的 Rails 5 ActionController::InvalidAuthenticityToken

转载 作者:太空宇宙 更新时间:2023-11-03 14:27:15 27 4
gpt4 key购买 nike

在 Rails 5.2 上,SSL 环境,使用 Nginx 作为 reverse_proxy 运行,每当我提交表单时我都会收到错误:

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 配置中,我设置了标题 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com