使用 NGINX 作为反向代理和 Apache2 作为 php Web 服务器的 Wordpress 不断重定向到 localhost 而不是使用域

Wordpress with NGINX as reverse proxy and Apache2 as php webserver keep redirecting to localhost instead of using the domain

提问人:Steve 提问时间:11/14/2023 更新时间:11/14/2023 访问量:34

问:

我刚刚在我的域的子目录中安装了 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

评论

0赞 erny 11/15/2023
这里重要的是:就在 的下方,否则,“upstream”将获得给定 URL 的主机名 (localhost...proxy_set_header Host $http_host;proxy_pass)

答:

0赞 Steve 11/14/2023 #1

答案就在这篇文章上:

Nginx Reverse SSL Proxy for Apache for WordPress 的问题

为了实现这一点,我更改了 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在第一行“<?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'];
}