![](/img/trans.png)
[英]Nginx and Unicorn - (Connection refused) while connecting to upstream
[英]Nginx can not forward the request protocol correctly to upstream
我有一个rails 4 beta的网站。 它在Nginx + Unicorn上运行。 我希望nginx将请求协议('http'或'https')转发给unicorn,以便我可以使用它们。 但是我无法使其发挥作用。
我把<%= request.ssl? %>
用于测试的视图文件中的<%= 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行也注释掉了:
<%= request.ssl? %> : false
<%= request.protocol %> : http
<%= request.ssl? %> : false
<%= request.protocol %> : http
=================
第1行注释掉,第2行不是OR行2注释掉,第1行不是OR也没有注释掉
<%= request.ssl? %> : true
<%= request.protocol %> : https
<%= 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;
}
}
您将需要配置两个单独的服务器块。 一个用于HTTP,一个用于HTTPS。 它将使两者的配置变得更加容易。 您可以从单独的共享配置文件中包含相同的部件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.