【问题标题】:SSL on Nginx causes redirect loopNginx 上的 SSL 导致重定向循环
【发布时间】:2015-04-19 01:10:23
【问题描述】:

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

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

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

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

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

谢谢!

【问题讨论】:

  • 看起来您有两个服务器块正在监听 443,第一个正在重写 URL?删除第一个 443 服务器块。

标签: ruby-on-rails redirect ssl nginx https


【解决方案1】:

试试这个:

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,则可能需要修改此设置。

【讨论】:

  • 我错过了proxy_set_header X-Forwarded-Proto $scheme;。谢谢!
猜你喜欢
  • 2012-03-05
  • 2016-02-02
  • 2015-02-26
  • 2015-02-15
  • 1970-01-01
  • 2014-06-01
  • 2011-06-04
  • 2018-10-19
  • 1970-01-01
相关资源
最近更新 更多