2

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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

6 Comments

Thanks for the response I tried the above with no avail. Nginx log shows couple of 301 requests and fails with 404
Also i suppose return would not necessarily do a proxy pass. the url would display the abcdef.com instead of platform.com/pen which defeats the purpose of proxy pass.
Do you have the control on the server that serves abcdef.com ?
Yes we have. abcdef.com is served through a aws LB
And are the servers that recive those requests prepared for platform.com/pen ? Can you put the part of the configuration of the abcdef.com servers where you recive and handle the platform.com/pen proxy pass?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.