Apache and Nginx

I have just installed Nginx on my Linode VPS, and I have moved my blog and website to it to. I have also consolidated my blog and personal website into one site.

I have put Nginx in front of Apache, so all the static files are served by Nginx, with the rest (PHP) being served by Apache using Nginx as a proxy.

Apache is now listening to port 8080, with Nginx on port 80

This is the current Nginx config for my reverse proxy. It is pulls together several config files that can be found for Nginx reverse proxies.

server {
        listen 80;

        root /srv/www/chrisrogers.me.uk/www/public_html;
        index index.php index.html index.htm;

        server_name chrisrogers.me.uk;

        location / {
                try_files $uri @proxy;
        }

        location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {
                expires      30d;
        }

        location @proxy {
                proxy_set_header X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header Host $host;
                proxy_pass http://127.0.0.1:8080;
         }
        location ~* \.php$ {
                proxy_set_header X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header Host $host;
                proxy_pass http://127.0.0.1:8080;
        }
        location ~ /\.ht {
                deny all;
        }
}

This should (in theory) serve all static files straight away, with a 30d expiry date, and pass the rest of the requests to apache.

I’m quite new to Nginx so I will probably be adding and improving this as I learn more over time.


Leave a Reply

Your email address will not be published. Required fields are marked *