Nginx无法正确地将请求协议转发到上游

时间:2013-04-25 19:47:18

标签: ruby-on-rails nginx forward

我在rails 4 beta中有一个网站。它在Nginx + Unicorn上运行。我希望nginx将请求协议('http'或'https')转发给unicorn,以便我可以使用它们。但是我无法使其发挥作用。

我将<%= request.ssl? %><%= request.protocol %>放在视图文件中进行测试。我的nginx服务器配置文件如下:

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

server {
  listen 80;
  listen 443;
  server_name example.com;
  root /home/example;

  ssl on;
  ssl_certificate /etc/nginx/ssl/server.crt;
  ssl_certificate_key /etc/nginx/ssl/server.key;

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

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

  location @unicorn {
    proxy_set_header X-Forwarded-Proto https;  # <--- Line 1
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Ssl on;       # <--- Line 2
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

我发现我标记的2行不正常。这是我的测试结果:

=================

第1行注释掉,第2行也注释掉了:

访问http://the.url

<%= request.ssl? %>     : false
<%= request.protocol %> : http

访问https://the.url

<%= request.ssl? %>     : false
<%= request.protocol %> : http

=================

第1行注释掉,第2行不是OR 第2行注释掉,第1行不是OR 也没有评论

访问http://the.url

<%= request.ssl? %>     : true
<%= request.protocol %> : https

访问https://the.url

<%= request.ssl? %>     : true
<%= request.protocol %> : https

=================

也就是说,如果出现这两行中的一行,则无论实际协议是什么,nginx都会向上游转发“https”。但是如果这两行都没有出现,那么无论实际的协议是什么,nginx都会将“http”转发给上游。

有人可以告诉我如何编写nginx配置文件,以便它可以正确转发协议吗?非常感谢你。

2 个答案:

答案 0 :(得分:37)

尝试:

proxy_set_header X-Forwarded-Proto $scheme;

OR

server {
    Listen 80
    ...
}
server {
    Listen 443
    ...
    location @unicorn {
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-Ssl on;
    }
}

答案 1 :(得分:1)

您需要配置两个单独的服务器块。一个用于HTTP,一个用于HTTPS。它将使两者的配置变得更加容易。您可以从单独的共享配置文件中包含相同的部分。