2017-03-22 115 views
0

我有一個碼頭工人一Nginx的我與HTTP和HTTPS的開發環境,這裏的配置:背後nginx的Nginx的配置反向代理

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?

回答

0

HTTPS變量設置爲$https(根據與後端服務器的連接設置,它始終爲HTTP),但您希望根據轉發的連接來設置它。

您可以使用X-Forwarded-Proto表頭設置HTTPS變量使用map。例如:

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

請參閱this document瞭解更多信息。