NGINX Proxy to wordpress website NGINX Proxy to wordpress website wordpress wordpress

NGINX Proxy to wordpress website


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:

  • Ubuntu 18.04
  • NGINX version 1.4
  • PHP 7.2
  • Wordpress 4.8.5


So this is my config for a proxified wordpress like https://happy-dev.fr/blog :

location /blog/ {     proxy_set_header X-Original-Request $request_uri;     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;     proxy_pass https://blog.happy-dev.fr/;     proxy_redirect https://$proxy_host/ /;}

This works with the wordpress at this address blog.happy-dev.fr without the need to put it in a /blog/ subdirectory because it overrides the default behavior which is:

proxy_redirect https://$proxy_host/ /blog/

I found this there http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect


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.