【问题标题】:nginx reverse proxy to apache-wordpress works but proxy_pass to external url failsnginx 反向代理到 apache-wordpress 工作,但 proxy_pass 到外部 url 失败
【发布时间】:2018-09-24 12:37:14
【问题描述】:

我有一个适用于 apache wordpress 的 nginx 反向代理设置,效果很好。但是,基于位置需要重定向到失败的外部 url。请检查以下配置。这是一个有效的设置吗?

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

https://platform.com/pen - 这需要重定向到 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;
    }
   }

【问题讨论】:

    标签: wordpress nginx apache2 reverse-proxy


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      如果不是明确的重定向,请使用 302 而不是 301,因此不会被缓存(测试时更好)。

      给出 404 的原因是因为您使用主机/url https://platform.com/pen 的请求访问 https://abcdef.com 如果命运服务器没有准备好接收整个 url,它会返回 404,因为 /pen 没有找到。

      【讨论】:

      • 感谢您的回复 我尝试了以上但无济于事。 Nginx 日志显示几个 301 请求并以 404 失败
      • 另外我想 return 不一定会做代理通行证。 url 将显示 abcdef.com 而不是 platform.com/pen,这违背了代理传递的目的。
      • 你对服务 abcdef.com 的服务器有控制权吗?
      • 是的,我们有。 abcdef.com 通过 aws LB 提供服务
      • 接收这些请求的服务器是否为platform.com/pen 准备好了?你能把abcdef.com服务器的配置部分放在你接收和处理platform.com/pen代理通行证的地方吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 2019-03-20
      • 2019-05-03
      • 2018-02-15
      • 1970-01-01
      • 2016-11-15
      • 2016-12-22
      相关资源
      最近更新 更多