Tomer
Tomer

Reputation: 4481

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.

Upvotes: 12

Views: 34750

Answers (5)

Sunding Wei
Sunding Wei

Reputation: 2214

I finally got the nginx works for WordPress with subdirectory

https://my-website.com/blog
  1. Setup a localhost website to wordpress
server {
        listen   7789;

        root /data/web/wordpress/;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                index index.php;
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}
  1. nginx reverse proxy
server {
    listen         443 ssl;
    ...

    # the trailing '/' is vital
    location ^~ /blog/ {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_pass http://localhost:7789/;
    }
}
  1. defines to wp-config.php
/** set the site URL */
define('WP_HOME', '/blog');
define('WP_SITEURL', '/blog');
$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/",  $_SERVER['REQUEST_URI']);
  1. change wp-includes/load.php to support ssl reverse proxy
function is_ssl() {
    // Check reverse proxy header, uppercase at server side
    $proto = $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? "http";
    if ($proto == "https")
        return true;
    ...
}
  1. make sure to disable the chrome disk cache to debug (F12 -> Disable cache)

Upvotes: 1

Aleks
Aleks

Reputation: 5320

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.

Upvotes: 4

Suyash Sumaroo
Suyash Sumaroo

Reputation: 83

I had to add an additional line in the nginx config file:

location /wp-content {
    proxy_pass https://blog.site.com/wp-content/;
}

This will take care of loading all your wp-content resources, including assets from your wordpress plugins.

Upvotes: 0

Dagmar
Dagmar

Reputation: 3261

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

Upvotes: 29

Plup
Plup

Reputation: 1232

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

Upvotes: 1

Related Questions