9

I have a Wordpress site running in a Vagrant development environment. When I load up http://localhost:8080, the site comes up fine, but as soon as I try to access the admin by going to http://localhost:8080/wp-admin, I'm redirected to http://localhost/wp-admin/.

Two things here:

  1. Nginx is forcing the trailing slash (which I'm not doing intentionally, afaik, but would be fine).
  2. In the trailing slash redirection, the port is getting lost (which is very much not fine).

I've tried adding the port_in_redirect directive (using both values, to be honest) that I've seen in other answers to similar questions, but it changed nothing.

Any idea what I'm missing?

Rob Wilkerson
  • 1,465
  • 4
  • 17
  • 25

4 Answers4

3

The default proxy_redirect when using a proxy_pass inside a location (adapted to your use case) is:

location / {
  proxy_pass     http://localhost:8080/;
  proxy_redirect http://localhost:8080/ /;
}

Since you are using the same 8080 port in Vagrant, this will match and remove the port.

There are two easy solutions to avoid this:

  1. Forward to a different port in the vagrant configuration so that proxy_redirect doesn't match.
  2. Disable proxy_redirect using proxy_redirect off;.

(I know this was 4 years ago, but I just had a similar problem while testing KeyCloak, so I hope this answer helps others that face this problem.)

PChambino
  • 131
  • 3
0

nginx/1.21.3 doesn't seem to obey its own documentation as to redirection when URI replacement is ambiguous.

Following config allowed me to make both /wp-admin and /wp-admin/ work.

server {
    location = /wp-admin {
        rewrite    /wp-admin / break;
        proxy_pass http://172.17.0.1:8000;
    }
    location /wp-admin/ {
        proxy_pass http://172.17.0.1:8000/;
    }
}

P.S. I wanted to setup a reverse proxy for a service behind a custom endpoint. I spent hours trying to disable silent redirect that loses port when trailing slash is absent. I hope this helps someone.

Yuri Pozniak
  • 101
  • 1
0

You need to use proxy_redirect to modify the response header so localhost is rewritten to localhost:8080

Sameer
  • 4,118
  • 2
  • 17
  • 11
  • Well, here's the thing. As far as I can tell, it's WP doing the redirection, not Nginx. `proxy_redirect` looks like it _performs_ a redirection which isn't something I need to do. Am I reading that wrong? – Rob Wilkerson Jan 30 '13 at 19:15
  • To clarify, `proxy_redirect` doesn't perform a redirect, it performs a find and replace of the Location header returned by the upstream server. – PChambino Feb 16 '17 at 10:35
0

If you notice Wordpress doing a redirect (as opposed to NGINX, as you already checked), make sure the settings of wordpress inside the database are correct.

This is because Wordpress keeps the full website url inside a database table called wp_options

Under a standard installation, these options for the url have option id 1 and 2, and a key value of siteurl and home. You can use tools like PhpMyAdmin to modify the database to modify these values, to fix the redirect issues.

Ferrybig
  • 179
  • 1
  • 9