0

I'm trying to get my Wordpress blog hosted on blog.domain.com to work on domain.com/blog.

I have been able to do this with proxy_pass successfully but the issue is certain scripts such as the login page for admins (/wp-login.php) does not work - it gives me File not found.. I have isolated the issue to my Laravel applications FastCGI setup in my Nginx configuration. When I comment it out, my blog works but then my Laravel app is inaccessible. I've been trying different configurations but I have little knowledge with Nginx.

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mydomain.com;
    server_tokens off;
    root /home/appfolder/mydomain.com/public;

...

...

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DO NOT REMOVE!)
    include forge-conf/mydomain.com/server/*;

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

    
# WHEN I COMMENT OUT THIS BLOCK, BLOG WORKS BUT LARAVEL BREAKS
    location ~ \.php$ {
         fastcgi_split_path_info ^(.+\.php)(/.+)$;
         fastcgi_pass unix:/var/run/php/php7.4-fpm-mydomain.sock;
         fastcgi_index index.php;
         include fastcgi_params;
    }
    
    
    location /blog {
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_pass  https://blog.mydomain.com;
    }
    location /blog/wp-content {
        proxy_pass  https://blog.mydomain.com/wp-content;
    } 
    location /blog/wp-includes {
        proxy_pass https://blog.mydomain.com/wp-includes;
    }
    location /blog/wp-content/uploads {
        proxy_pass https://blog.mydomain.com/wp-content/uploads;
    }
    location /blog/wp-login.php {
        proxy_pass https://blog.mydomain.com/wp-login.php;
    }
    location /blog/wp-admin {
        proxy_pass https://blog.mydomain.com/wp-admin;
    }
    location /bitnami {
        proxy_pass https://blog.mydomain.com/bitnami;
    }
    location /mod_pagespeed_beacon {
        proxy_pass https://blog.mydomain.com/mod_pagespeed_beacon;
    }
    
    

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/mydomain.com-error.log error;

    error_page 404 /index.php;



    location ~ /\.(?!well-known).* {
        deny all;
    }
}

...

Hosted on an Ubuntu server managed by Laravel Forge. Any ideas?

Update: As per Richards answer, I have started getting it working a little bit but I need to add rules for all files which doesn't seem efficient.

I'm trying to get all .PHP files related to Wordpress (after /blog/) to be forwarded to the proxy. I have tried:

    location ~ /blog/wp-admin/(\d+) {
        add_header X-debug-message "$1";
        proxy_pass https://blog.mydomain.com/wp-admin/$1;
    }

But this doesn't seem to work.

Tim Rowley
  • 420
  • 4
  • 15
  • The *regular expression* `location` takes precedence over a *prefix* `location` unless you use the `^~` operator. See [this document](http://nginx.org/en/docs/http/ngx_http_core_module.html#location). – Richard Smith Feb 06 '21 at 09:39
  • @RichardSmith Thanks for that! I added the '=' operator to the wp-login.php location which worked but there are a number of .php files, so how would I make this a bit more dynamic. For example, I get the same problem with 'wp-admin/edit.php' -> File not found'. \ – Tim Rowley Feb 06 '21 at 10:30

1 Answers1

0

I sorted it out by creating a regular expression to match anything after /wp-admin/ on forward it to the proxy. The configuration I use is:

location ~ (blog\/wp-admin)\/(.*)\.php$ {
    proxy_pass https://blog.mydomain.com/wp-admin/$2.php?$query_string;
}

$2 is the second capture group (.*) which is appended to the proxy_pass URL and added any further arguments with ?$query_string.

Tim Rowley
  • 420
  • 4
  • 15