首页 文章

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

提问于
浏览
3

我有一个Rails 3.2.13应用程序,我试图为Nginx和Unicorn配置SSL . 我希望能够将一些控制器和一些控制器操作告诉'force_ssl'并正确重定向 . 我已经能够使这个工作,所以我可以手动点击应用程序'https://foo.com ' and things work. When I put ' force_ssl ' into a controller action, let' s说用户#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 回答

  • 4

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

    proxy_set_header  Host $http_host;
    

    SSL块可以,但是如果你从非SSL端开始并且Rails选择它,那么它可能没有完整的头?

相关问题