Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX Proxy to wordpress website

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.

like image 626
Tomer Avatar asked Dec 07 '22 21:12

Tomer


2 Answers

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
like image 187
Dagmar Avatar answered Dec 18 '22 05:12

Dagmar


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.

like image 42
Aleks Avatar answered Dec 18 '22 03:12

Aleks