How do I setup Varnish 4.0 with nginx setup for subdomain site?

  • Posted November 24, 2014

Can anyone help on how to configure Varnish cache 4.0 for subdomain site like instead of ?

Running on LEMP stack and it’s a Wordpress site.

I’ve installed Varnish 4.0 with the help from the link below. But their Varnish configuration guide is 3.0 and for a main domain site like .


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This comment has been deleted

Configuring Varnish 4.0 will be much the same as in that tutorial. Like in that article, you’ll need to configure it to listen on port 80 and send requests to port 8080 where Nginx will listen. Varnish will pass everything on and Nginx will be responsible for routing the requests after that. If there is only one subdomain hosted on the server, then just make sure that server_name is correctly set in your Nginx configuration.

Here’s a sample Nginx conf that you might use for a PHP site like WordPress:

server {
        listen 8080 default_server;

        root /var/www/html;
        index index.php index.html index.htm;


        location / {
                # try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php?q=$uri&$args;

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;

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

Note it is listening on port 8080 and the server_name is set.

If you were hosting more than one site on the same server and you only wanted one to be cached by Varnish, you’ll need to disable it for the subdomain in /etc/varnish/default.vcl by using something like:

if ( == '') {
    return (pass);