ruby-on-rails - Nginx 上的 SSL 导致重定向循环

标签 ruby-on-rails redirect ssl nginx https

我正在尝试使用 Nginx 在 Digital Ocean VPS 上设置 SSL。我的服务器 conf 是这样的:

upstream unicorn {


server unix:/home/ubuntu/apps/example/shared/sock/unicorn.example.sock fail_timeout=0;
}

server {
  listen 80;
  server_name  www.example.com example.com;
  rewrite ^/(.*) https://example.com/$1 permanent;
}

server {
  listen 443;

  include example_ssl;

  server_name www.example.com;
  rewrite ^/(.*) https://example.com/$1 permanent;
}

server {
  listen 443;
  include example_ssl;

  server_name example.com;

  root /home/ubuntu/apps/example/current/public;

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  location ~ ^/(assets)/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
    #add_header Last-Modified "";
    #add_header ETag "";
  }
  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 60;
}

ssl 信息在 example_ssl 中:

ssl on;

ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;

ssl_session_timeout  10m;

ssl_ciphers "AES256+EECDH:AES256+EDH";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;

这会导致无休止的重定向循环。 Http 请求按预期重定向到 HTTPS,但所有请求都尝试 https://example.com正在被重定向回 HTTP。

我似乎无法弄清楚为什么要重定向 HTTPS 请求。我通过访问 www.digicert.com 检查了我的 SSL 证书,一切都返回说它已成功安装。

当我从我的终端尝试时:

openssl s_client -connect example.com:443

我收到以下错误:

verify error:num=20:unable to get local issuer certificate

我从客户那里获得的证书不包含中间证书,但据我所知,在尝试从浏览器访问网站时,这应该仍然有效。

如果我将服务器 block 更改为仅监听 80 且不使用 SSL,则该站点能够成功加载,但我只需要使用 SSL。

另外,这是使用 Unicorn 网络服务器托管 Rails 应用程序。除了启动网络服务器之外,我的 unicorn.log 是空的,所以我是否更正了这根本没有触及我的 Rails 配置,只是 nginx/ssl 证书配置的问题?

谢谢!

最佳答案

试试这个:

upstream unicorn {
server unix:/home/ubuntu/apps/example/shared/sock/unicorn.example.sock fail_timeout=0;
}

server {
  listen 80;
  server_name example.com www.example.com;
  return 301 https://$server_name$request_uri;
}

server {
  listen 443 ssl;
  include example_ssl;
  server_name example.com;

  root /home/ubuntu/apps/example/current/public;

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  location ~ ^/(assets)/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
    #add_header Last-Modified "";
    #add_header ETag "";
  }
  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 60;
}

您需要删除“ssl on;”从您的 ssl 包含文件,即适用于旧版本的 nginx。如果您在单个 IP 上使用多个 SSL,您可能需要修改它。

关于ruby-on-rails - Nginx 上的 SSL 导致重定向循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28576748/

相关文章:

javascript - 检测页面是否被重定向或直接加载(Javascript)

redirect - 非 Web 应用程序的 OAuth 重定向 URI 替代方案?

javascript - 删除用户时如何清除本地存储

ssl - 这个 erlang 代码抛出异常,我不知道为什么

javascript - 如何在nodejs中将数字格式化为指定长度

ruby-on-rails - Rails 4 需要和允许多个

ruby-on-rails - EC2 中的 SSL 缓慢

Azure 负载均衡器问题?

c# - 如何在 System.Net.Mail SMTP 中使用显式 SSL

ruby-on-rails - OSX 上的 Ruby on Rails - 我是否搞砸了安装?