Nginx proxy_pass to Wordpress on Remote Server

February 9, 2015 1.9k views

I'm trying to set up a proxy to my Wordpress blog using proxy_pass but I'm getting some very strange behavior. My Wordpress blog is located at blog.example.com/blog/ and the website at www.example.com, I'd like the blog to appear at www.example.com/blog/. Here's the code I have, which works for viewing the blog but not for any PHP scripts(logging in, wp-cron, etc all results in "No Input File Specified" error) on the blog:

location /blog/ {
    expires off;
    proxy_pass http://blog.example.com/blog/;
}

Here is the output in my nginx error log when trying to access the wp-admin page:

2015/02/08 14:28:33 [error] 20572#0: *594485 FastCGI sent in stderr: "Unable to open primary script: /home/user/www.example.com/public/blog/wp-login.php (No such file or directory)" while reading response header from upstream, client: 10.0.1.214, server: www.example.com, request: "GET /blog/wp-login.php?redirect_to=http%3A%2F%2Fblog.example.com%2Fblog%2Fwp-admin%2F&reauth=1 HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "www.example.com"

It appears that any PHP script is hitting my .php block:

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

Any help is greatly appreciated.

3 comments
  • Have you found a solution? I'm having the same problem in one of my droplets, we recently decided to move to Nginx and give it a try, but we wanted to keep wordpress running in another droplet under apache

  • @moncefhammou Sort of, I've had to use a pretty ugly tactic to get it working. Instead of the nginx error I'm now just being redirected to the blog server when trying to access certain pages, which works, and with canonical tags set up in WP to point at the main server(www.example.com/blog) I haven't seen any negative impact from search engines, which is a plus. Here is the nginx configuration I'm using:

    location /blog/ {
        expires off;
        try_files $uri $uri/ @apachesite;
    }
    
    location @apachesite {
        resolver 8.8.8.8 8.8.4.4 valid=300s;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://blog.example.com;
        proxy_redirect http://www.example.com/blog/ http://blog.example.com/;
    }
    
  • @beaudierman I actually fixed it with this. Don't know if it's the best way to do it tho. Thanks for you help btw :)

        location ~* ^((?!\/blog)(.+)\.php)$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
    
1 Answer

This question was answered by @moncefhammou:

@beaudierman I actually fixed it with this. Don't know if it's the best way to do it tho. Thanks for you help btw :)

   location ~* ^((?!\/blog)(.+)\.php)$ {
           fastcgi_split_path_info ^(.+\.php)(/.+)$;
           fastcgi_pass unix:/var/run/php5-fpm.sock;
           fastcgi_index index.php;
           include fastcgi_params;
   }

View the original comment

Have another answer? Share your knowledge.