nginx 到 apache-wordpress 的反向代理工作,但 proxy_pass 到外部 url 失败

nginx reverse proxy to apache-wordpress works but proxy_pass to external url fails

我有一个用于 apache wordpress 的 nginx 反向代理设置,工作正常。但是,根据位置需要重定向到失败的外部 url。请检查以下配置。这是一个有效的设置吗?

https://platform.com/ - 这有效 - 任何后续的 wp 页面也有效

https://platform.com/pen - this needs to redirect to https://abcdef.com - 这不起作用 - 404 页面加载错误任何帮助?

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

    server_name platform.com;
    server_tokens off;

    root /var/www/html/def/public/;
    index index.php;

    ssl on;
    ssl_certificate /tmp/fgh.crt;
    ssl_certificate_key /tmp/fgh.pem;

    access_log /var/log/nginx/access2.log;
    error_log /var/log/nginx/error2.log;

    location / {
            proxy_set_header X-Forwarded-Proto $scheme;
            try_files $uri @apache;
    }

    location @apache {
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8080;
    }

     location ~[^?]*/$ {
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8080;
     }

     location /pen {
            proxy_pass https://abcdef.com;
    }
   }

您正在做的是 proxy_pass 到 https://abcdef.com ,而不是重定向。如果你的意思是重定向代码是:

 location /pen {
        return 301 https://abcdef.com;
}

如果不是最终重定向,请使用 302 而不是 301,因此不会被缓存(对于测试来说更好)。

出现 404 的原因是因为您正在通过 host/url https://platform.com/pen[= 的请求访问 https://abcdef.com 20=] 如果命运服务器不准备接收整个 url,它会 returns 404,因为找不到 /pen。

将服务器名称(wordpress 站点)从 http 前缀更改为 www 前缀后,代理传递重新指示有效。必须将所有 http https 服务器块重定向到 nginx 配置中的 www 服务器块