微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – Nginx无法将请求协议正确转发到上游

我有一个网站在rails 4 beta.它正在Nginx Unicorn上运行.我希望Nginx将请求协议(‘http’或’https’)转发到独角兽,以便我可以与他们一起工作.但是我无法使其工作.

我把<%= request.ssl? %GT;和<%= 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配置文件,以便正确转发协议?非常感谢你.

解决方法

尝试:
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;
    }
}

原文地址:https://www.jb51.cc/ruby/272669.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐