Nginx上的SSL导致重定向循环

时间:2015-02-18 05:40:12

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

我正在尝试使用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证书配置的问题?

谢谢!

1 个答案:

答案 0 :(得分: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,则可能必须修改此项。