ruby-on-rails - 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;
}
解决方案
原因很简单。当您使用带有 ssl 的 Nginx 服务器时,它已经为您整理好 ssl。如果您config.force_ssl = true
从 production.rb 中删除,它将被排序。
推荐阅读
- mongodb - 在相同的键上使用带有 $set 和 $setOnInsert 的 bulkWrite
- multithreading - Perl 线程:为什么输出是按顺序而不是混淆的?
- html - 无法使用 justify-content: space-between 在两个 div 之间创建空间
- typescript - 赛普拉斯检查密码保护的 pdf 文件内容
- middleware - 使用中间件提供 html 表单文件时出现问题
- f# - 如何防止基于模型的测试中的收缩?
- java - 在 JPA 存储库中使用本机查询时出现无效列错误
- javascript - 检查浏览器是否可以呈现繁重的 HTML
- html - 谷歌地图从昨天开始没有显示在我的网站上
- c# - 一段时间后结束协程函数内循环的最佳方法是什么?