Nginx 无法将请求协议正确转发到上游 [英] Nginx can not forward the request protocol correctly to upstream

查看:42
本文介绍了Nginx 无法将请求协议正确转发到上游的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a website in rails 4 beta. It is running on Nginx + Unicorn. I want nginx to forward the request protocol ( 'http' or 'https' ) to unicorn so that I can work with them. However I am not able to make it work.

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

I put <%= request.ssl? %> and <%= request.protocol %> in the view file for testing. My nginx server config file is as following:

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 行操作不正确.这是我的测试结果:

I found that the 2 lines I marked are not acting right. Here is my test result:

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

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

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

Line 1 commented out, Line 2 commented out too :

访问http://the.url

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

访问https://the.url

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

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

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

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

Line 1 commented out, Line 2 is not OR Line 2 commented out, Line 1 is not OR Neither is comments out

访问http://the.url

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

访问https://the.url

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

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

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

也就是说,如果出现这两行之一,无论实际协议是什么,nginx都会将https"转发到上游.但是如果这两行都没有出现,那么无论实际协议是什么,nginx 都会将http"转发到上游.

That is to say, if one of those two lines appears, nginx forward "https" to upstream no matter what the actual protocol is. But if none of those two lines appears, nginx forward "http" to upstream no matter what the actual protocol is.

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

Please can someone tell me how to write the nginx config file so that it can forward the protocol correctly? Thank you very much.

推荐答案

尝试:

proxy_set_header X-Forwarded-Proto $scheme;

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

这篇关于Nginx 无法将请求协议正确转发到上游的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆