I have a statically served site (using nginx). I want to host a wordpress blog (hosted on a different instance) under /blog folder. When using a nginx proxy:
location /blog/ {
proxy_set_header X-Is-Reverse-Proxy "true";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://55.555.55.555;
}
and the following wp-config.php:
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/');
define('WP_HOME', 'http://SOMESITE.com/blog/');
All the files from wp-content
(and wp-includes
) are not being served correctly, since they are being searched under http://SOMESITE.com/wp-content/*
instead of http://SOMESITE.com/blog/wp-content/*
.
Adding some extra proxy rules didn't work, ex:
location ~* (\/wp-content) {
proxy_set_header X-Is-Reverse-Proxy "true";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://55.555.55.555;
}
nor redefining the wp-config.php:
define('WP_SITEURL', 'http://SOMESITE.com/blog/');
Would love any ideas. I am also pretty sure it is a common use case, but couldn't find any working tricks here.
THX.
I finally got a NGINX reverse proxy working for a Wordpress blog!
My setup is a Wordpress site served by NGINX on port 8080 and a default site (on port 80) that serves the Wordpress blog on the subdirectory "blog". (e.g. http://www.example.com/blog).
In my "default site" NGINX configuration, I defined the following reverse proxy location:
location ^~ /blog/ {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
In wp-config.php I made the following modifications:
/** set the site URL */
define('WP_HOME','http://www.example.com/blog');
define('WP_SITEURL','http://www.example.com/blog');
/** Fix to get the dashboard working with the reverse proxy.*/
$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/", $_SERVER['REQUEST_URI']);
For completeness, here is the basic NGINX config for the Wordpress blog:
server {
listen 8080;
listen [::]:8080;
server_name example.com;
root /var/www/blog;
<...>
location / {
index index.php
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_intercept_errors on;
fastcgi_index index.php;
}
}
Versions:
For me the answer was a bit simpler:
location /blog {
proxy_ssl_server_name on;
proxy_pass https://blog.example.com;
}
wp-config.php
/** set the site URL */
define('WP_HOME','https://www.example.com/blog');
define('WP_SITEURL','https://www.example.com/blog');
/** Fix to get the dashboard working with the reverse proxy.*/
$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/", $_SERVER['REQUEST_URI']);
Note that this was made for https
.
All credits goes to @dagmar as I have based my answer on his. I coundn't put in comments as it wouldn't fit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With