wordpress 帖子,页面 url 重定向到 nginx 上的本地 proxy_pass docker 容器主机名

wordpress 帖子,页面 url 重定向到 nginx 上的本地 proxy_pass docker 容器主机名

我已经成功设置了一个在 dockerized nginx 上运行的 wordpress 站点。当 wordpress 站点启动并运行时,我可以毫无问题地转到主页:https://my_domain.com或任何链接或wp-admin/...(登录后可访问的)。/wp-login.php

但是当我转到https://my_domain.com/sample-pagehttps://my_domain.com/post-id/wp-admin(如果未登录)时,它会立即重定向到 proxy_pass http://wordpress_host:80(在 nginx 配置文件中设置),这是不正确的,它应该https://my_domain.com/post-id在客户端的浏览器中。

客户端浏览器的 URL 错误

/wp-admin/在登录之前使用路由,如果我附加index.php/wp-admin/index.php它,它会工作,而没有它则不会

这是我的 nginx 配置:

server {
    listen 80;
    listen [::]:80;
    server_name my_domain.com www.my_domain.com;

    location / {
        return 301 https://my_domain.com$request_uri;
    }
}


server {
    listen 443 ssl http2;
    server_name my_domain.com www.my_domain.com;

    ssl on;
    server_tokens off;
    ssl_certificate /etc/nginx/ssl/live/my_domain.com/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/live/my_domain.com/privkey.pem;
    ssl_dhparam /etc/nginx/dhparam/dhparam-2048.pem;

    ssl_buffer_size 8k;
    ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
    ssl_prefer_server_ciphers on;
    ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;

    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
    add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
    # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
    # enable strict transport security only if you understand the implications


    location / {
          try_files $uri $uri/ /index.php$is_args$args;

          proxy_pass http://wordpress_host:80; 
          proxy_set_header X-Real-IP $remote_addr; 
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

          proxy_redirect http://wordpress_host:80 https://my_domain.com/;
          proxy_cookie_domain http://wordpress_host:80 my_domain.com;
          proxy_set_header X-Forwarded-Proto https;
    }

    location ~ \.php$ {
          try_files $uri =404;
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          proxy_pass http://wordpress_host:80;
          fastcgi_index index.php;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param PATH_INFO $fastcgi_path_info;

          proxy_set_header X-Real-IP $remote_addr; 
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

          proxy_redirect http://wordpress_host:80 https://my_domain.com/;
          proxy_cookie_domain http://wordpress_host:80 my_domain.com;
          proxy_set_header X-Forwarded-Proto https;
    }

    location ~ /\.ht {
          deny all;
    }
        
    location = /favicon.ico { 
          log_not_found off; access_log off; 
    }

    location = /robots.txt { 
          log_not_found off; access_log off; allow all; 
    }

    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
          expires max;
          log_not_found off;
    }
}

我也在wp-config.php进行配置:

define('FORCE_SSL_ADMIN', true); 

if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') 
$_SERVER['HTTPS']='on';

define('WP_SITEURL', 'https://www.my_domain.com/');
define('WP_HOME', 'https://www.my_domain.com/');

答案1

您已设置了SITEURL前缀www 。这仅与具有前缀的请求匹配www

my.example.com您应该在 nginx 中执行从到 的301 重定向www.my.example.com,以便所有请求都正确路由到 WordPress。

相关内容