0

Recently migrated a multisite network, only to discover some of the sites are using a theme that's incompatible with the server's PHP version.

While I work on a longterm fix, what I'd like to is use NGINX to reverse proxy the non-working sites to the old server, while keeping the current, working sites pointed to the new server.

When I try the following, I'm able to make it to my old site's pages, but static assets (js, css, etc.) return 404, and I'm unable to access any /wp-admin pages. What should I change?

Here's the whole server block config. Save for the location /oldsite, I'm using this config for other server blocks as well. In this case all my WordPress files are stored in their own separate /wordpress/ directory, while /wp-content/ is kept in the root of each project.

server {
    server_name mydomain.com www.mydomain.com;

    root /var/www/html/wordpress/mydomain.com/wordpress/;

    location /oldsite {
        proxy_pass http://my.old.ip;
        proxy_set_header Host $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;
    }

     index index.php index.html;

     location /wp-content {
        root /var/www/html/wordpress/mydomain.com/;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        #If a file isnt found, 404
        try_files $uri =404;
        #Include Nginxs fastcgi configuration
        include /etc/nginx/fastcgi.conf;
    }

    if (!-e $request_filename) {
        # Don't use $uri here, see https://github.com/yandex/gixy/issues/77
        rewrite /wp-admin$ $scheme://$host$request_uri/ permanent;
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;
        rewrite ^(/[^/]+)?(/.*\.php) $2 last;
    }

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

}
3
  • Most likely some other location block takes over this one on requests for static assets. Can you show your full server block? You can omit all confidential info like domain names etc. Jun 30 '20 at 6:58
  • Maybe worth also posting to SO/serverfault as it feels like more of an nginx quesiton than WP?
    – mozboz
    Jun 30 '20 at 9:41
  • @IvanShatsky I updated to show the full server block Jun 30 '20 at 15:23
0

I'm not sure if it works first time, but lets try.

server {
    server_name mydomain.com www.mydomain.com;

    root /var/www/html/wordpress/mydomain.com/wordpress/;

    # The "^~" modifier makes this location to take priority over regex-matched locations
    location ^~ /oldsite {
        proxy_pass http://my.old.ip;
        proxy_set_header Host $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;
    }

     index index.php index.html;

     location /wp-content {
        root /var/www/html/wordpress/mydomain.com/;
    }

    location ~ \.php$ {
        # This rule now goes here, "last" changed to "break" to not leave this location after rewriting URI
        rewrite ^(/[^/]+)?(/.*\.php) $2 break;

        fastcgi_pass 127.0.0.1:9000;
        #If a file isnt found, 404
        try_files $uri =404;
        #Include Nginxs fastcgi configuration
        include /etc/nginx/fastcgi.conf;
    }

    location / {
        # Two other rewriting rules goes inside the root location block to not interfere with "location ^~ /oldsite { ... }"
        if (!-e $request_filename) {
            # Don't use $uri here, see https://github.com/yandex/gixy/issues/77
            rewrite /wp-admin$ $scheme://$host$request_uri/ permanent;
            rewrite ^(/[^/]+)?(/wp-.*) $2 last;
        }

        try_files $uri $uri/ /index.php$is_args$args;
    }

}

I comment the changes I've made to your config.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.