大网数据广告活动
服务器运维问答

DEBIAN - Nginx and Apache reverse proxy loses HTTPS state?

· 52CE 我爱测

I've got a problem configuring nginx and apache on a Debian 8 machine. I use nginx s reverse proxy, which is setup to run on HTTPS (and it works like a charm). But, when requests are sent to Apache, the $_SERVER['HTTPS'] is empty…

问题原文

原帖链接 · Olivier · CC BY-SA 3.0

I've got a problem configuring nginx and apache on a Debian 8 machine. I use nginx s reverse proxy, which is setup to run on HTTPS (and it works like a charm). But, when requests are sent to Apache, the $_SERVER['HTTPS'] is empty, although it should be "ON" (anyway, that's what i'd like to achieve) ? Here is my apache config :

<VirtualHost 127.0.0.1:8080>
    ServerName shop.mywebsite.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www_shop

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    LogLevel debug
  <Directory "/">
    Require all granted
  </Directory>
</VirtualHost>

And here is my nginx congif, part on proxy pass :

    location ~ \.php {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header        X-Forwarded-Proto $scheme;
    proxy_set_header X-Original-Request $request_uri;
    add_header              Front-End-Https on;
    proxy_pass http://127.0.0.1:8080;
}

Any help, any clue, appreciated ;) Thanks, Olivier.

已采纳解答

原帖链接 · Jens Bradler · CC BY-SA 3.0

The HTTPS state you are looking for is set if the connection to the web server is over HTTPS. As your are looking for this value in Apache but only Nginx is handling the request via HTTPS and Apache getting requested via HTTP by Nginx, the status must be no-HTTPS.

In your proxy configuration of Nginx, you will set already some additional header. What you have to do is, check for these header in Apache and overwrite HTTPS status flag

Therefore you have to install and activate mod_setenvif in Apache and add following to your Apache config:

<IfModule mod_setenvif.c>
    SetEnvIf X-Forwarded-Proto "https" HTTPS=on
</IfModule>

In your Nginx config one setting is missing. As long as your Apache does not have multiple virtual hosts, it doesn't matter but I recommend to add following to your Nginx config:

proxy_http_version 1.1;

By setting this Nginx speaks to Apache in HTTP/1.1. The default is HTTP/1.0.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-09-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。