首页 > 解决方案 > Rails 5、Nginx、Puma、Rails 管理员 - ERR_TOO_MANY_REDIRECTS

问题描述

我想知道如何在 rails 5 应用程序中强制实施 SSL。该应用程序在开发中运行良好。在生产中,通过 rails admin 通过 SSL 发出的一些 POST 请求不起作用。

如果 SSL 通过 production.rb 实施,浏览器会返回:

"ERR_TOO_MANY_REDIRECTS"

如果“force_ssl”设置为 false,则通过 rails admin 的一些“POST”请求会返回:

HTTP Origin header (https://www.example.com) didn't match request.base_url (http://www.example.com)

提前致谢。

这些是应用程序设置:

生产.rb

  # ...
  config.force_ssl = true
  # ...

nginx.conf

upstream puma {
  server unix:///home/bgc/apps/domain/shared/tmp/sockets/domain-puma.sock;
}

server {
  listen 80;
  listen 443 ssl;
  server_name domain.com;

  ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot 
  ssl_protocols TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;


  root /home/bgc/apps/domain/current/public;
  access_log /home/bgc/apps/domain/current/log/nginx.access.log;
  error_log /home/bgc/apps/domain/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-Proto https;
    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;
}

标签: ruby-on-railsnginxpumarails-admin

解决方案


原因很简单。当您使用带有 ssl 的 Nginx 服务器时,它已经为您整理好 ssl。如果您config.force_ssl = true从 production.rb 中删除,它将被排序。


推荐阅读