無法使用 nginx 反向代理程式存取 Wordpress Dashboard

無法使用 nginx 反向代理程式存取 Wordpress Dashboard

在使用 Nginx 設定 WordPress 作為 Apache 後端的反向代理時,我遇到了一些問題。所有頁面都正在加載,但是當我嘗試登入 wp-admin 儀表板時出現錯誤。錯誤是Sorry, you are not allowed to access this page.

我檢查了我的檔案權限、資料庫前綴、.htaccess 甚至資料庫中的使用者元管理權限,一切似乎都很完美。在我設定 nginx 反向代理之前,網站運作得非常好。

這是我的阿帕契2配置

   <VirtualHost *:8081>
       DocumentRoot "/mnt/NAS/wp_data/wordpress/"
       ServerName my_site_url
       ServerAlias www.my_site_url
    
    <Directory "/mnt/NAS/wp_data/wordpress/">
       Options MultiViews FollowSymlinks
       AllowOverride All
       Order allow,deny
       Allow from all
    </Directory>
    
   </VirtualHost>

這是我的阿帕契連接埠.conf

   #Listen 80
   Listen 8081

這是我的nginx 設定

server {
        listen 80;
        listen [::]:80;

        server_name my_site_url;

        rewrite ^ https://$server_name$request_uri? permanent;

}

server {
        listen 443 ssl;
        listen [::]:443 ssl;

        server_name www.my_site_url my_site_url;

        location / {
                proxy_set_header Host $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_pass http://127.0.0.1:8081;
        }

        ssl_certificate /var/www/mycert/certificate.pem;
        ssl_certificate_key /var/www/mycert/private.key;
}

最後但並非最不重要的一點是我的wp-config.php是預設的,除了

if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) ) {
if ( 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
$_SERVER['HTTPS'] = 'on';
}
}
if ( isset( $_SERVER['HTTP_X_REAL_IP'] ) ) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_REAL_IP'];
}

答案1

我可以使用 nginx 作為反向代理在 apache 上運行 Wordpress 及其儀表板

nginx 設定是

 location ^~ /blog/ {
    proxy_pass http://x.y.x.z/;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-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;
}

並在 wp-config.php (在 apache 伺服器上)中加入以下內容

$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/",  $_SERVER['REQUEST_URI']);


if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) ) {
if ( 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
$_SERVER['HTTPS'] = 'on';
}
}
if ( isset( $_SERVER['HTTP_X_REAL_IP'] ) ) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_REAL_IP'];
}

相關內容