【问题标题】:nginx frontend over https and backend over httpnginx 前端通过 https 和后端通过 http
【发布时间】:2021-02-28 01:00:31
【问题描述】:

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

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

在 http 上运行时一切正常。但是我要转到 https 并且无法正常工作。

我收到“混合模式”消息,说样式表和脚本被阻止,因为它们是通过 https 提供的。 在 WordPress 中,我添加了以下内容,如 herewp-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,我会得到很多样式表和脚本的混合内容。

所有这些链接都可以通过 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;
    }
}

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

【问题讨论】:

    标签: wordpress nginx nginx-reverse-proxy


    【解决方案1】:

    我不确定您是否使用此配置将 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;
        }
    }
    

    【讨论】:

    • 非常感谢您的回复!它没有帮助,但它让我思考。我已经把 '$_SERVER['HTTPS']='on';' 东西放在 wp-config.php 的底部,所以我试着把它放在开头,现在它正在工作..
    • 不能是缓存响应吗?你可以从隐身模式检查它吗?如果你在没有if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) 检查的情况下强制$_SERVER['HTTPS']='on'; 会发生什么?
    猜你喜欢
    • 2020-08-23
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    相关资源
    最近更新 更多