WARNING: Can't verify CSRF token authenticity rails

后端 未结 17 1200
生来不讨喜
生来不讨喜 2020-11-22 06:05

I am sending data from view to controller with AJAXand I got this error:

WARNING: Can\'t verify CSRF token authenticity

I think

17条回答
  •  醉梦人生
    2020-11-22 06:13

    I struggled with this issue for days. Any GET call was working correctly, but all PUTs would generate a "Can't verify CSRF token authenticity" error. My website was working fine until I had added a SSL cert to nginx.

    I finally stumbled on this missing line in my nginx settings:

    location @puma { 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        proxy_set_header Host $http_host; 
        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto https;   # Needed to avoid 'WARNING: Can't verify CSRF token authenticity'
        proxy_pass http://puma; 
    }
    

    After adding the missing line "proxy_set_header X-Forwarded-Proto https;", all my CSRF token errors quit.

    Hopefully this helps someone else who also is beating their head against a wall. haha

提交回复
热议问题