Bubble Foundry


Nginx Sitting in Front of Apache

by Peter.

On my Slicehost virtual server (signup with my referral link) I originally set up Apache as my web server, with a bunch of virtual hosts to manage serve different domains. This has worked well but I am now working on projects (namely Lift apps running in Jetty) where Nginx is a much better solution. So, I decided to stick Nginx in front of Apache.

Translating my vhosts was quite easy: I simply changed their Apache settings to only listen on localhost:8888 and created settings in Nginx that look like this:

server {
  listen 80;
  server_name thedomain.com www.thedomain.com;
    location / {
      access_log off;
      proxy_pass http://127.0.0.1:8888;
      proxy_redirect off;

      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

However, I also want to occasionally throw up the odd PHP script for testing. There’s no mod_php (or libphp, or whatever it’s called these days) for Nginx and the most common solution is using FastCGI. That looks good but I figure that since I already have Apache playing nicely with PHP, I might as well use it. So how to do it? I set up the following in Nginx:

server {
       listen 8989;
       server_name BubbleFoundry.local;
       location / {
                 proxy_pass  http://127.0.0.1:8889;
                 proxy_redirect off;
                 proxy_set_header X-Real-IP $remote_addr;
                 proxy_set_header Host $host;
                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }
}

And in Apache I simply had my default virtual host listen on 8889.

This works well, though I noticed that when Apache returns a 404 error the response returns back to Nginx. If I go to http://myserver:8989/somefolder, it gets kicked back to Nginx which then returns a 404 error. http://myserver:8989/somefolder/ (notice the trailing slash), on the other hand, works. Probably just need to tweak something in my Apache settings, but it’s good enough for now.