无法使用 nginx 反向代理访问 Wordpress 仪表板

无法使用 nginx 反向代理访问 Wordpress 仪表板

我在设置 WordPress 并使用 Nginx 作为 Apache 后端的反向代理时遇到了一点问题。所有页面都在加载,但当我尝试登录 wp-admin 仪表板时出现错误。错误是Sorry, you are not allowed to access this page.

我检查了文件权限、数据库前缀、.htaccess 甚至数据库中的 usermeta 管理员权限,一切似乎都很完美。在我设置 nginx 反向代理之前,网站运行良好。

这是我的apache2 配置

   <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>

这是我的Apache 端口.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'];
}

相关内容