By Janice Lim
Can anyone help on how to configure Varnish cache 4.0 for subdomain site like foo.example.com instead of example.com ?
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 www.example.com .
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
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;
server_name blog.example.com;
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 (req.http.host == 'notcached.example.com') {
return (pass);
}
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.