gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:08:20 25 4
gpt4 key购买 nike

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

我把 <%= request.ssl? %><%= request.protocol %>在 View 文件中进行测试。我的 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

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

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

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

最佳答案

尝试:

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;
}
}

关于ruby-on-rails - Nginx 无法将请求协议(protocol)正确转发到上游,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16223470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com