Apache as a Backend for nginx

Apache is powerful, nginx is fast. Here’s how to use both of them to provide blazing fast speeds while retaining the same feature set your site is built upon. While nginx is very powerful in its own right, it does not yet share the same amount of modules and module support that Apache does. A common example is mod_rewrite for Apache pretty much isn’t easily implemented in to nginx.

In these examples we’ll assume you’re running nginx and Apache on the same server. See my load balancing tutorial as a supplement to this if you wish to have multiple Apache backends, and to use nginx as a load balancer (reverse proxy). These instructions should be distribution-agnostic, however configuration file locations will be given in accordance to how Debian/Ubuntu places them. Adjust according to taste.

The first step is to tell Apache to stop listening on port 80, and have it listen somewhere else. If this is a separate server from the nginx load balancer, set the localhost IP to the proper LAN IP. In your /etc/apache2/ports.conf, make the following change:


Now, verify that the site that Apache is handling contains an appropriate ServerName. If you don’t do this, PHP will generate strange URI’s with the wrong port, and other odd behavior will occur. In, for example, /etc/apache2/sites-enabled/001-beginlinux.com, within your VirtualHost directive:

ServerName beginlinux.com

Note that you didn’t specify a port. This will cause PHP to rely on the HTTP headers to determine the port, though some software will do its own detection (such as PHPMyAdmin). Software that does this should provide a way to define it’s URI.

Once you’re done, reload Apache’s configuration.

service apache2 reload

Now we need to set up nginx to use Apache as the backend provider. In /etc/nginx/sites-enabled/beginlinux.com:

upstream beginlinux.com {

server {
        listen 80 default_server;
        server_name beginlinux.com www.beginlinux.com;

        root /var/www/beginlinux.com;
        index index.php;

        location / {
                        proxy_set_header X-Forwarded-Host $http_host;
                        proxy_set_header X-Forwarded-Server $host;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_pass http://beginlinux.com;


        location ~* ^.+.(jpg|jpeg|gif|png|mp4|mp3|css|zip|pdf|txt|js|flv|swf|html|htm)$ {
       expires max;

That last location line tells nginx to serve files matching that regex to be served from nginx itself, rather than being passed on to the Apache back end. Adjust this as needed! The current line serves static files that shouldn’t require processing by Apache, but, if you are a heavy user of mod_rewrite then this might not be appropriate and will likely need editing.

Reload nginx’s configuration, and give it a test. You should be done! As always, the error logs will guide you should something go wrong.