WordPress multisite subdirectory on nginx behind reverse proxy

I can’t seem to make wordpress multisite subdirectories work behind my reverse proxy.

Locally (from lan ip addresses) all works, I can access the main domain, sub-directories and all dashboards.

The moment I add a reverse proxy, I cannot access the network dashboard (Redirect loop) nor the multisites subdirectories (redirects to lan ip).
If I try to go to the multisites by manually writing the address it will send me to the page without css.

The only one that works with the reverse proxy is the main site.

What I want :

  • www.example.net
  • www.example.net/wp-admin
  • www.example.net/wp-admin/network
  • www.example.net/sub1
  • www.example.net/sub1/wp-admin
  • www.example.net/sub2
  • www.example.net/sub2/wp-admin

What happens :

  • www.example.net –> ok
  • www.example.net/wp-admin –> ok
  • www.example.net/wp-admin/network –> redirect loop
  • www.example.net/sub1 –> no css
  • www.example.net/sub1/wp-admin –> no css
  • www.example.net/sub2 –> no css
  • www.example.net/sub2/wp-admin –> no css

Reverse Proxy conf

server {
        listen       80;
        server_name  *.example.net example.net;
        return 302 $scheme://www.example.net$request_uri;
}

server {
        listen       80;
        server_name  www.example.net;
        return 302 https://$host$request_uri;
}

server {
    listen       443 ssl http2;
    server_name  www.example.net;

    ssl on;
    ssl_certificate /etc/letsencrypt/live/www.example.net/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.example.net/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

        location ^~ / {
                proxy_pass              http://192.168.2.30/;
                proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header      X-Real-IP $remote_addr;
                proxy_set_header        X-Forwarded-Proto       $scheme;
        }
}

wp-config.php

/* Updates asking for FTP */
define('FS_METHOD','direct');

/* Multisite */
define( 'WP_ALLOW_MULTISITE', true );
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', false);
// define('DOMAIN_CURRENT_SITE', '192.168.2.30');
define('DOMAIN_CURRENT_SITE', 'www.example.net');
define('PATH_CURRENT_SITE', "https://wordpress.stackexchange.com/");
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

/* Faire fonctionner SSL (et CSS) */
if ( (!empty( $_SERVER['HTTP_X_FORWARDED_HOST'])) ||
     (!empty( $_SERVER['HTTP_X_FORWARDED_FOR'])) ) {
    $_SERVER['HTTPS'] = 'on';
}

wordpress nginx conf

server {
        listen       80 default_server;
        server_name     192.168.2.30;

        root   /var/www/html/wordpress;

        access_log      /var/log/nginx/wp.access.log;
        error_log       /var/log/nginx/wp.error.log;

        ## WordPress Perm links config
        location / {
                index index.php index.html;
                try_files $uri $uri/ /index.php?$args;
        }

        # Rewrite multisite in a subdirectory '.../wp-.*' and '.../*.php'.
         if (!-e $request_filename) {
            rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
            rewrite ^/[_0-9a-zA-Z-]+.*(/wp-admin/.*\.php)$ $1 last;
            rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
        }

        # Pass all .php files onto a php-fpm or php-cgi server
        location ~ \.php$ {
           try_files $uri =404;
           include fastcgi_params;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
           fastcgi_index index.php;
        }
}

If I add ‘proxy_set_header Host $host’ to reverse proxy conf I get this error when trying to access wp-admin

Error establishing a database connection

If your site does not display, please contact the owner of this network. If you are the owner of this network please check that MySQL is running properly and all tables are error free.

Could not find site www.example.net. Searched for table wp_blogs in database wordpressDB. Is that right?

What do I do now? Read the bug report page. Some of the guidelines there may help you figure out what went wrong. If you’re still stuck with this message, then check that your database contains the following tables:

    wp_users
    wp_usermeta
    wp_blogs
    wp_signups
    wp_site
    wp_sitemeta
    wp_registration_log
    wp_blog_versions

The dashboard does not keep the domain name for links (everything redirects to ip). I tried adding this bit of code to the wp-config but it still didn’t change anything.
define(‘WP_HOME’, ‘https://www.example.net’);
define(‘WP_SITEURL’, ‘https://www.example.net’);

1 Answer
1

The problem was the sql database that did not update urls. I do not know if there is a way to do this easily but I feel like I have looked everywhere for this solution and finally found it with this answer to a somewhat similar question : https://premium.wpmudev.org/forums/topic/how-to-change-url-for-wp-multi-site#post-426799

I started over from where everything works locally (from ip address) then from there I went into the database to change :

  • siteurl and home from wp_options to http://www.example.net/
  • siteurl and home from wp_{blog_id}_options to http://www.example.net/{blog} (where {blog_id} is the ID of the additional sites and {blog} is sub1 and sub2)
  • domain from wp_site and wp_blogs to www.example.net

If I leave this as is I’ll get a Error establishing a database connection

As the above link says to I went back to the wp-config file to change the DOMAIN_CURRENT_SITE to www.example.net.

Letting this as is I get a gateway error, so I went to the reverse proxy and added this bit to the wordpress server location :

proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header Host $host;

And voila! WordPress multisite behind a reverse proxy!

If I want to add a new subsite It keeps all the good info, so I don’t need to redo this. The only “downside” is that I cannot access the website from the local ip anymore, but then I’d just need to go over these steps and change the domain for the ip tol go back.

Leave a Comment