WordPress multisite subdirectory on nginx behind reverse proxy

Question

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', '/');
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‘);

in progress 0
, , , , Carobell 3 years 2018-12-25T13:29:48-05:00 1 Answer 122 views 0

Answer ( 1 )

    0
    2021-07-15T18:32:33-05:00

    A reverse proxy sits in front of a web server and receives all the requests before they reach the origin server. It works similarly to a forward proxy, except in this case it’s the web server using the proxy rather than the user or client. Reverse proxies are typically used to enhance performance, security, and reliability of the web server.
    This tutorial is very much informative I appreciate it .

Leave an answer

Browse
Browse