Nginx Proxy_Pass to WordPress – Site.com/Blog to Blog.Site.com

nginxPHPweb-serverWordpress

I'm trying to get my WordPress site to serve from example.com/blog with an Nginx web server proxy_pass to a WordPress site (apache) hosted on another server at blog.site.com.

With my current Nginx config viewing the blog from site.com/blog is working fine, however, I'm unable to access the login for the WordPress site. It seems like it may be falling through to the ~. .php location but I'm unsure how to prevent that behavior.

For example, when I attempt https://example.com/blog/wp-admin it returns a 404 from https://example.com/wp-admin/

If I try https://example.com/blog/wp-admin/ it gives me a stdErr of No input file specified from https://example.com/blog/wp-login.php?redirect_to=https%3A%2F%2Fexample.com%2Fblog%2Fwp-admin%2F&reauth=1

Nginx Config for example.com –

map $uri $expires {
    default off;
    ~\.(jpg|jpeg|png|gif|ico|css|js|pdf)$ 7d;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name site.com;
    root /var/www/example.com/current/public;

    ssl_certificate /etc/nginx/ssl/site.com/454191/server.crt;
    ssl_certificate_key /etc/nginx/ssl/site.com/454191/server.key;

    ssl_protocols TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    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;

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

    location /blog/ {
        proxy_pass https://blog.example.com/;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }

    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/example.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

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

Best Answer

Per the comments, this change solved the problem.

location ^~ /blog/ {
        proxy_pass https://blog.example.com/;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    }