设计:无法验证服务器上启用的 HTTPS 的 CSRF 令牌真实性(否 JSON/API)

Devise: Can't verify CSRF token authenticity the HTTPS enabled on server (No JSON/API)

我正在建立一个 Rails 博客。我正在使用 Rails 5 和 Devise 4.2.0。该应用程序在带有 Nginx 和 Puma 的 Ubuntu 服务器上的生产环境中运行,并使用 Capistrano 进行部署。在 Nginx 上启用 HTTPS(具有有效的 SSL 证书)并将 301 重定向从 HTTP 添加到 HTTPS 之前,一切都在生产中运行良好。

我检查了生产日志,日志中的真实性密钥与我在浏览器中看到的不匹配。

这是我正在使用的 nginx.conf 文件:

upstream puma {
  server unix:///home/deploy/apps/example-blog/shared/tmp/sockets/example-blog-puma.sock;
}

server { # Redirect HTTP to HTTPS
  # Bind port(s)
  listen   80;
  listen   [::]:80;

  # Bind domain(s)
  server_name blog.example.com;

  # 301 redirect to HTTPS
  return 301 https://$server_name$request_uri;
}

server { # Primary server block
  # Bind port(s)
  listen   443 default_server ssl;

  # Bind domain(s)
  server_name blog.example.com;

  # Bind certificate(s)
  ssl_certificate       /etc/nginx/ssl/blog.example.com/ssl-bundle.crt;
  ssl_certificate_key   /etc/nginx/ssl/blog.example.com/blog.example.com.key;

  root /home/deploy/apps/example-blog/current/public;
  access_log /home/deploy/apps/example-blog/current/log/nginx.access.log;
  error_log /home/deploy/apps/example-blog/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}

有人知道这里会发生什么吗? 如果您需要更多信息,请告诉我。

谢谢

已将 proxy_set_header X-Forwarded-Proto $scheme; 添加到 location @puma 中,这使它得以运行。