繁体   English   中英

nginx反向代理背后的Nginx配置

[英]Nginx configuration behind nginx reverse proxy

我的开发环境有一个带 Docker 的 Nginx,使用 HTTP 和 HTTPS,这是配置:

listen 80;
listen 443 ssl;

set_real_ip_from 10.0.0.0/8;
real_ip_header X-Real-IP;
real_ip_recursive on;

location / {
    try_files $uri @rewriteapp;
}

location @rewriteapp {
    rewrite ^(.*)$ /app.php/$1 last;
}

location ~ ^/(app|app_dev|app_test|config)\.php(/|$) {
    fastcgi_pass php-upstream;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS $https;
}

我想在本地环境中测试 HTTP 和 HTTPS,但在生产中,我在前面有一个 Nginx 反向代理:

upstream app_upstream {
  server app:80;
}

server {
server_name $APP_DOMAIN;

listen 443 ssl;
ssl_certificate /run/secrets/app_cert.pem;
ssl_certificate_key /run/secrets/app_key.pem;

proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

location / {
    proxy_pass http://app_upstream;
}
}

我希望反向代理接受唯一的 HTTPS 并转发到应用程序 nginx 但我背后的 PHP 应用程序正在接收 $_SERVER['HTTPS'] = ""

我也想只在反向代理上保留 SSL 证书,如何将 HTTPS 从反向代理传递到 Nginx 到 PHP?

HTTPS变量设置为$https (根据与后端服务器的连接设置,始终为 HTTP),但您希望根据转发的连接设置它。

您可以使用X-Forwarded-Proto标头通过map设置HTTPS变量。 例如:

map $http_x_forwarded_proto $https_flag {
    default off;
    https on;
}
server {
    ...
    location ~ ^/(app|app_dev|app_test|config)\.php(/|$) {
        ...
        fastcgi_param  HTTPS  $https_flag;
        ...
    }
}

有关更多信息,请参阅此文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM