生产环境Nginx上的Rails 5 ActionController :: InvalidAuthenticityToken

时间:2019-05-07 14:47:52

标签: ruby-on-rails ssl nginx ruby-on-rails-5

在Rails 5.2上,SSL环境在Nginx上以reverse_proxy运行,每当我提交表单时都会收到错误消息:

  

HTTP Origin标头(https://agro2business.com.br)不匹配   request.base_url(https://agro2business.com.br,agro2business.com.br)

     

在1毫秒内完成422个不可处理的实体(ActiveRecord:0.0毫秒)

     

ActionController :: InvalidAuthenticityToken   (ActionController :: InvalidAuthenticityToken)

我读过有关配置参数和Nginx以便在另一个StackOverflow问题中传递标头的信息,但到目前为止还算不上成功。我的Nginx配置文件:

  

proxy_pass http://localhost:4000;           }

     

位置/ {

            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为什么要尝试将标头与两个值进行比较?

  

request.base_url(https://agro2business.com.br,agro2business.com.br)

1 个答案:

答案 0 :(得分:0)

我的问题是,在我的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主机$ http_host; 中的一个