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

血红的双手。 提交于 2019-12-11 06:07:23

问题


I have a nginx reverse proxy setup for apache wordpress which works fine. However based on location need to redirect to an external url which fails. Please check the below config. Is this a valid setup ?

https://platform.com/ - this works - also any subsequent wp pages also works

https://platform.com/pen - this needs to redirect to https://abcdef.com - this doesn't work - 404 page load error Any help ?

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

回答1:


After changing the server name (wordpress site) from http prefix to www prefix, proxy pass re directions worked. Had to redirect all http https server blocks to www server block in nginx config




回答2:


What you are doing is a proxy_pass to https://abcdef.com , not a redirect. if you meant a redirect the code is :

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

If it's not a definitive redirect, use 302 instead of 301, so is not cached (for tests is much better).

The reason the 404 is given is because you are accessing the https://abcdef.com with a request with the host/url https://platform.com/pen If the destiny server is not prepared to recive this whole url, it returns 404, as /pen is not found.



来源:https://stackoverflow.com/questions/52391068/nginx-reverse-proxy-to-apache-wordpress-works-but-proxy-pass-to-external-url-fai

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!