3

我有一个 Rails 3.2.13 应用程序,我正在尝试使用 Nginx 和 Unicorn 配置 SSL。我希望能够将一些控制器和一些控制器操作告诉“force_ssl”并正确重定向。我已经能够让它工作,这样我就可以使用“ https://foo.com ”手动点击应用程序并且一切正常。当我将“force_ssl”放入控制器操作时,假设 users#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;
}
4

1 回答 1

4

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

proxy_set_header  Host $http_host;

SSL 块可以,但是如果您非 SSL 端开始并且 Rails 选择它,它可能没有完整的标题?

于 2013-06-18T01:22:02.480 回答