我有一个大致看起来像这样的设置(我试图尽可能简化这个):
端口443 – nginx监听并转发到端口80,configuration看起来像这样
server { listen 443 ssl; location / { proxy_pass http://127.0.0.1:80; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; proxy_set_header X-Forwarded-Port 443; proxy_set_header Host $host; }
端口80 – 清漆(反向代理),将stream量转发到端口8080,除非有caching命中。
端口8080 nginx设置为PHP与fastcgi。 我有一些共同的configuration:
location ~ \.php$ { ... include fastcgi_params; }
和看起来像这样的fastcgi_params文件
... fastcgi_param HTTPS $https if_not_empty;
我一直在试图做的是创build一个安装程序,其中nginx在侦听端口8080时能够检测是否向http或https提供内容。 它需要告诉这个PHP,所以它可以创build正确的scheme的URL。
我在想,在某个地方,我应该听X-Forwarded-Proto
,然后告诉nginx https是打开的。 我不知道如何/如何做某种如果这样的事情甚至可能或如何告诉nginx HTTPS应该。
您可以通过两种方式解决这个问题:在应用程序级别或在Nginx级别。
因为它是一个PHP应用程序,您可以在代码/应用程序的顶部使用以下内容…
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') $_SERVER['HTTPS']='on';
步骤1 :将X-Forwarded-Proto映射到其值取决于X-Forwarded-Proto的variables。
map $http_x_forwarded_proto $fastcgi_param_https_variable { default ''; https 'on'; }
第2步 :用以下代码行replace现有的fastcgi_param HTTPS $https $if_not_empty
行,该行根据上述variables设置fastcgi_param HTTPS
fastcgi_param HTTPS $fastcgi_param_https_variable;
就个人而言,我更喜欢第一种方法,因为它只需要一行就可以正确设置。 第二种方法也应该工作,但没有testing。