如何解决nginx反向代理混合内容(http、https)

ane*_*bie 11 nginx reverse-proxy

nginx 的新手。玩了2天,不知道如何解决这个问题:

我有一堆虚拟机在一个盒子里运行,这个盒子显然在我的路由器后面,其中一个虚拟机正在运行 nginx(我的反向代理)并设置为 DMZ。

我已经在该 VM 上正确安装了 SSL 证书,现在我希望所有传入流量都根据其路径进行定向,例如:

domain.com/service1->192.168.1.101

domain.com/service2->192.168.1.102

等等。想法是让 nginx 作为 SSL 层工作,而 nginx 通过 HTTP 或任何未加密的协议与其他 VM 对话。然后当然当 nginx 与客户端对话时,消息应该被加密。

我已经部分工作了。如果我通过 HTTP 访问,除了未加密之外,一切都很好,但是如果我通过 HTTPS 访问,则网页会损坏,并且出现此类错误:Mixed Content: The page at 'https://domain.com/service1' was loaded over HTTPS, but requested an insecure stylesheet 'http://domain.com/service1/blahblah.css'. This request has been blocked; the content must be served over HTTPS.

我也收到了这样的警告: The page at 'https://domain.com/service1/' was loaded over HTTPS, but is submitting data to an insecure location at 'http://domain.com/service1/': this content should also be submitted over HTTPS.

现在,对于某些服务,我可以破解服务本身,以便修复它……但我不想这样做,因为那样我必须破解每个耗时且可能会破坏某些内容的服务。我想尽可能少地接触服务。

我当前的配置适用于 hack,但没有 hack 就无法工作。它涵盖了整个服务1:

location /service1/ {
    proxy_pass              http://192.168.1.101/;

    proxy_read_timeout      3500;
    proxy_connect_timeout   3250;

    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        Host $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Proto https;

    proxy_set_header        SSL_PROTOCOL $ssl_protocol;
}
Run Code Online (Sandbox Code Playgroud)

我在互联网上寻找通用解决方案,但只找到了一种适用于一项服务的黑客。其他 nginx 示例/howtos 没有太大帮助(仍然收到错误和警告)。

提前谢谢了!

小智 22

使用 nginx,我需要:

        add_header 'Content-Security-Policy' 'upgrade-insecure-requests';
Run Code Online (Sandbox Code Playgroud)

  • 这应该是正确的解决方案! (2认同)
  • 如果使用 Nginx 作为反向代理,则应将其放在 server 块内(而不是放置 proxy_pass 的 location 块内)。通过使用此标头,人们将跳过编辑应用程序源代码。 (2认同)

小智 13

这适用于 nginx https 代理 >> nginx http 服务 Django 后端

在位置指令中:

proxy_set_header X-Forwarded-Proto $scheme;
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,值得阅读这篇出色的文章:https : //www.metaltoad.com/blog/running-drupal-secure-pages-behind-proxy

  • 这也适用于使用 docker-compose 的 Wordpress + Nginx。 (2认同)

Ter*_*nen 7

你必须要经过网站的代码,并替换出现的所有http://domain.com/resource用两种/resource//domain.com/resource

这确保所有依赖的网页资源都使用与加载网站本身相同的协议加载。

  • 这是否意味着 nginx 无法重写传出数据包?我讨厌修改它背后的每个网站...... (3认同)

小智 5

添加到此 apache.conf 文件解决了我的问题

 <IfModule mod_setenvif.c>
  SetEnvIf X-Forwarded-Proto "^https$" HTTPS
</IfModule>
Run Code Online (Sandbox Code Playgroud)