11

In my NGINX configuration, a WordPress blog is on a private server. My NGINX public server proxies the private server's content for https://www.example.com/blog/.

location ^~ /blog/ {    # A "subdirectory", hiding a proxied server

    proxy_pass              http://192.168.0.5:80/;    # The blog resides in the 
                                                       # private's web root, 
                                                       # not in a subdirectory
    proxy_set_header        Host $host;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_redirect          off;
}

The blog is perfectly rendered on calling the domain and subdirectory. Bringing up wp-login does not generate a redirect GET field.

https://www.example.com/blog/wp-login.php

My siteurl and my home variables are both set to the domain with subdirectory.

However, after a successful login, I may see the dashboard, but the URL in my browser gets rewritten to https://www.example.com/wp-admin, causing problems on using the dashboard.

How do I configure WP to rewrite the URL to the subdirectory, although the blog is on a proxied private server?

(Do the subdirectories in the servers have to be symmetrical?)

3 Answers 3

27

I have also met with same problem, I found a workaround, to fix the issue, add below code to wp-config.php

$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/",  $_SERVER['REQUEST_URI']);
Sign up to request clarification or add additional context in comments.

2 Comments

It does not work. Wordpress redirects from /blog to /wp-admin (instead of /blog/wp-admin.
THIS saved my life. Literally the only solution I could find online after a whole day. The comments are incredibly grateful! And this solution incredibly non-intuitive. But hey, it works!
11

WordPress uses two variables to define where it is hosted: WP_HOME and WP_SITEURL. Both can be set using the dashboard, but I prefer to set them in wp-config.php:

define( 'WP_SITEURL', 'https://www.example.com/blog' );
define( 'WP_HOME', 'https://www.example.com/blog' );

It is usual to set an absolute URL (as above) which includes scheme and host name, but I prefer to use a relative URL when operating behind a reverse-proxy, like this:

define( 'WP_SITEURL', '/blog' );
define( 'WP_HOME', '/blog' );

You can probably continue to run WordPress in the root of your private server (assuming it isn't accessed directly). Moving the private server down one level is a little more complicated and involves the web server configuration on both servers to be changed a little.

3 Comments

This was incredibly helpful when trying to proxy_pass with nginx
Note: If you have access to the database, you can just edit 'siteurl' and 'home' in phpMyAdmin or something.
Relative urls does not work in v5.x, shows error The Site address you entered did not appear to be a valid URL. Please enter a valid URL.
0

You have to add the following two rewrites to your wp-config.php:

$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/",  $_SERVER['REQUEST_URI']);
$_SERVER['REQUEST_URI'] = str_replace("/wp-login.php", "/blog/wp-login.php", $_SERVER['REQUEST_URI']);

The first one fixes all admin pages. The second one you need for the password reset flow.

I found an open TRAC issue for the password reset flow from 4 years ago, so it seems we'll have to continue doing these workarounds for the foreseeable future. It doesn't seem like running behind a reverse proxy is something Wordpress expects most people to do.

Comments

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.