Site icon Hip-Hop Website Design and Development

Multisite wp-admin redirecting to main wp-admin using NGINX

When I try to go to the wp-admin of any multi site, it redirects to the main wp-admin.

So if I go to example.com/multi-site-slug/wp-admin/ it redirects to example.com/wp-admin/

NGINX is being used as a reverse proxy.

I’m using the standard WordPress Multisite Subdirectory rules per the codex: https://wordpress.org/support/article/nginx/

If I take out this line in the conf file rewrite ^(/[^/]+)?(/wp-.*) $2 last;, it prevents the redirect but then media assets don’t serve properly on the multisites.

NGINX vhost:

server {

    listen xxx.xxx.xxx.xxx:443 ssl http2 ;

    server_name example.com www.example.com;

    location ~* .(ico|jpe?g|gif|png|bmp|svg|tiff|exe|dmg|zip|rar|7z|docx?|xlsx?|js|css|less|sass|scss|ttf|woff2?|mp3|mp4|mkv|avi|mov|mpe?g|aac|wav|flac)$ {
        expires 7d;
        add_header Cache-Control "public, must-revalidate";
        add_header X-Proxy-Cache "STATIC/TYPE";
    }

    location ~ "^(/wp-content/uploads)" {
        expires 7d;
        add_header Cache-Control "public, must-revalidate";
        add_header X-Proxy-Cache "STATIC/PATH";
    }

    if ($http_cookie ~ "(_logged_in_|items_in_cart|jsid)") {
        set $cache_bypass 1;
    }

    location ~ "^(/.*login.php|.*/xmlrpc.php|.*/wp-cron.php)" {
        limit_req_status 429;
        limit_req zone=example_com burst=8 nodelay;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_no_cache 1;
        proxy_cache_bypass 1;
        add_header X-Proxy-Cache $upstream_cache_status;


        proxy_ssl_verify off;
        proxy_ssl_session_reuse off;
        proxy_ssl_name $http_host;
        proxy_ssl_server_name on;
        proxy_pass https://xxx.xxx.xxx.xxx:8443;
    }



    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_no_cache $cache_bypass;
        proxy_cache_bypass $cache_bypass;
        add_header X-Proxy-Cache $upstream_cache_status;


        proxy_ssl_verify off;
        proxy_ssl_session_reuse off;
        proxy_ssl_name $http_host;
        proxy_ssl_server_name on;
        proxy_pass https://xxx.xxx.xxx.xxx:8443;
    }


    location ~ ^/files/(.*)$ {
      try_files /wp-content/blogs.dir/$blogid/$uri /wp-includes/ms-files.php?file=$1 ;
      access_log off; log_not_found off; expires max;
    }

    location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 24h;
        log_not_found off;
    }

    location ^~ /blogs.dir {
        internal;
        alias /home/example/public_html/wp-content/blogs.dir ;
        access_log off; log_not_found off;      expires max;
    }


    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;
        rewrite ^(/[^/]+)?(/.*.php) $2 last;
    }

    location ~ ^/purge(/.*) {
        proxy_cache_purge example "$scheme$request_method$host$1";
    }
}

The WordPress site is located at: /home/example/public_html/