使用 NGINX 作为反向代理和 Apache2 作为 php Web 服务器的 WordPress 继续重定向到本地主机而不是使用域

问题描述 投票:0回答:1

我刚刚在我的域的子目录中安装了 wordpress:exaple.com/wordpress/

当我输入网址时:

 http://exaple.com/wordpress/

我被重定向到:

  https://localhost/wordpress/

Apache2 正在监听端口 8080 NGINX 正在监听端口 80, 443

我使用NGINX作为反向代理,配置如下:

    server {
        server_name example.com;
        location / {
                proxy_pass       http://localhost:8080;
        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
 }
 server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        server_name example.com;
    listen 80;
    return 404; # managed by Certbot
 }

Apache 配置为:

<VirtualHost *:8080>

    DocumentRoot "/var/www/example.com"
    ServerName example.com

    <Directory "/var/www/example.com">
        Options MultiViews FollowSymlinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

    TransferLog /var/log/apache2/nextcloud_access.log
    ErrorLog /var/log/apache2/nextcloud_error.log


</VirtualHost>

在 WordPress 数据库表 wp_options 中,我已经将“siteurl”和“home”两行更改为:

http://localhost/wordpress/

至:

http://example.com/wordpress/

还是不行。

php wordpress apache nginx
1个回答
0
投票

答案就在这篇文章中:

https://stackoverflow.com/questions/66263361/problems-with-nginx-reverse-ssl-proxy-for-apache-for-wordpress?rq=2

为了实现这一点,我更改了 Nginx 配置,如下所示:

    server {
        server_name example.com;
        location / {
                proxy_pass       http://localhost:8080;

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Server $host;


        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
 }
 server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        server_name example.com;
    listen 80;
    return 404; # managed by Certbot
 }

在 WordPress 安装中,我更改了配置,同时 wp-config.php 在第一行“”之后立即添加了以下代码

/**If we got HTTPS from Nginx, we must reply the same way */
if ( $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' )
{
        $_SERVER['HTTPS']       = 'on';
    $_SERVER['SERVER_PORT'] = '443';
        define('FORCE_SSL_ADMIN', true);
}

/**When replying make sure the reply HOST is set to Nginx Rerverse Proxy address, not us */
if ( isset($_SERVER['HTTP_X_FORWARDED_HOST']) )
{
        $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}
© www.soinside.com 2019 - 2024. All rights reserved.