为什么SSL重定向不能与force_ssl和Nginx一起使用?

时间:2013-06-17 22:38:16

标签: ruby-on-rails nginx unicorn

我有一个Rails 3.2.13应用程序,我正在尝试使用Nginx和Unicorn配置SSL。我希望能够将一些控制器和一些控制器操作告诉'force_ssl'并正确地重定向。我已经能够使这个工作,所以我可以用“https://foo.com”手动点击应用程序并且工作正常。当我将'force_ssl'放入控制器动作时,让我们说用户#index:

class UsersController < ApplicationController
  force_ssl

  def index
    # do some stuff
  end

end

我希望如果我导航到'http://foo.com/users'它会重定向到'https://foo.com/users'. 它没有。

相反,它会重定向到:'https://unicorn_foo/users'。我错过了什么?

nginx.conf:

upstream unicorn_foo {
  server unix:/tmp/unicorn.foo.sock fail_timeout=0;
}

server {
    listen 80 default;
    server_name foo.com;
    root /home/webuser/apps/foo/current/public;

    location ^~ /assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
    }

    try_files $uri/index.html $uri @unicorn_foo;

    location @unicorn_foo {
        proxy_set_header X-Forwarded-Proto http;
        proxy_pass http://unicorn_foo;
    }
    error_page 500 502 503 504 /500.html;
    client_max_body_size 5G;
    keepalive_timeout 10;
    send_timeout 240;
    sendfile_max_chunk 5m;
}

server {
    listen 443;
    server_name foo.com;
    root /home/webuser/apps/foo/current/public;

    location ^~ /assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
    }

    try_files $uri/index.html $uri @unicorn_foo;

    location @unicorn_foo {
        proxy_set_header  X-Real-IP       $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto https;
        proxy_set_header  Host $http_host;
        proxy_redirect    off;
        proxy_pass http://unicorn_foo;
    }
    error_page 500 502 503 504 /500.html;
    client_max_body_size 5G;
    keepalive_timeout 10;

    ssl on;
    ssl_certificate         /etc/nginx/ssl/server.crt;
    ssl_certificate_key     /etc/nginx/ssl/server.key;
    ssl_protocols           SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers             ALL:-ADH:+HIGH:+MEDIUM:-LOW:-SSLv2:-EXP;
    ssl_session_cache       shared:SSL:10m;

    send_timeout 240;
    sendfile_max_chunk 5m;
}

1 个答案:

答案 0 :(得分:4)

首先猜测......端口80服务器块没有通过主机,可能就是这样吗?

proxy_set_header  Host $http_host;

SSL块可以,但是如果您在非SSL端启动并且Rails选择了它,那么它可能没有完整的头?