首页 > 解决方案 > nginx 前端通过 https 和后端通过 http

问题描述

我让 nginx 在运行 Wordpress 的网络服务器前充当反向代理。我不确定我的问题是 nginx、php 还是 WordPress,所以我正在寻求帮助。

我有一个在 Docker 中运行的 nginx 服务器,它充当反向代理。在这台服务器后面有一个网络服务器,也在 Docker 中运行,运行 WordPress。

在 http 上运行时,这一切都很好。但是我要转到 https,但我无法让它正常工作。

我收到“混合模式”消息,说样式表和脚本被阻止,因为它们是通过 https 提供的。在 WordPress 中,我添加了以下内容,如下所示wp-config.php

define('FORCE_SSL_ADMIN', true);
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
$_SERVER['HTTPS']='on';

我也改变了这个:

define('WP_HOME', 'https://www.example.com');
define('WP_SITEURL', 'https://www.example.com');

我还更改了数据库中引用的所有http://www.example.com内容https://www.example.com

当我输入https://www.example.com时,我收到 1 个“混合内容”警告,提示样式表正在通过 http 加载,即使有一个样式表加载得很好。

混合内容

如果我输入https://www.example.com/wp-admin我会得到很多样式表和脚本的混合内容。

wp-管理员

所有这些链接都可以通过 https 访问,但不知何故,它们仍然通过 http 提供服务。我搜索了数据库中的每个文件和所有内容,但在任何地方都找不到对http://example.com的任何引用。

这是反向代理:

server {
    listen 80;
    server_name www.example.com example.com;
    return 301 https://www.example.com$request_uri;
}
    
server {
  listen        443 ssl;
  server_name   www.example.com example.com;

  ssl_certificate       /etc/ssl/private/example.com/example.com.crt;
  ssl_certificate_key   /etc/ssl/private/example.com/example.com.key;

  location / {
    proxy_pass  http://10.0.100.10:8082;
    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-Ssl         on;
    proxy_set_header    X-Forwarded-Proto       $scheme;
  }
}

这是后端

server {
    server_name _;

    listen 80;

    root /var/www/myapp;
    index index.php index.html index.htm;

    access_log /var/log/nginx/back-access.log;
    error_log /var/log/nginx/back-error.log;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # PHP-FPM Configuration Nginx
    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php-domain:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

谁能理解为什么会发生这种情况以及问题出在哪里?

标签: wordpressnginxnginx-reverse-proxy

解决方案


我不确定您是否X-Forwarded-Proto使用此配置将 HTTP 标头传递到您的 FastCGI 后端。你可以试试这个吗?

    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php-domain:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param HTTP_X_FORWARDED_PROTO $http_x_forwarded_proto if_not_empty;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

如果没有帮助,请将您的fastcgi_params文件内容添加到问题中。

更新

有一种方法可以在不改变的情况下做到这一点wp-config.php

map $http_x_forwarded_proto $fastcgi_https {
    https   on;
    default $https;
}
server {
    ...
    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php-domain:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param HTTP_X_FORWARDED_PROTO $http_x_forwarded_proto if_not_empty;
        fastcgi_param HTTPS $fastcgi_https if_not_empty;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

推荐阅读