VestaCP Configuration for Varnish Cache [Part 2]
| |This is the Part 2 article of my previous guide (Part 1) on how to setup Varnish Cache server to run with VestaCP. So if you have not read the Part 1, you better read that first.
Now let’s continue, shall we?
Stage 5 – Nginx Configuration
Step 1 – Go to /etc/nginx/conf.d/ directory and see if there is configuration file of your public IP:
cd /etc/nginx/conf.d ls
Step 2 – Edit that .conf file. Replace xxx.xxx.xxx.xxx with your own actual public IP:
nano xxx.xxx.xxx.xxx.conf
You’ll see something like this:
Step 3 – Now change port :80 to :8082 as example below:
Save changes and exit the editor which in Nano it is Control+O then Control+X.
Step 4 – Also edit vesta.conf file located at /usr/local/vesta/conf/. Use your favorite text editor or in my situation I use Nano:
nano /usr/local/vesta/conf/vesta.conf
change PROXY_PORT from 80 to 8082
Save changes and exit (Control+O then Control+X).
Step 5 – Edit nginx.conf file for each Vesta CP user located at /home/user/conf/web. This step is quite not efficient if you have several Vesta CP users as you have to edit them all. In my example I will edit nginx.conf file for user admin:
nano /home/admin/conf/web/nginx.conf
Again, change port 80 to 8082 at the listen line. See example below:
Save changes and exit the editor (Control+O then Control+X).
Stage 6 – VestaCP Firewall Configuration
Open up your favorite web browser, login to your Vesta CP dashboard as admin then click the Firewall menu on top of the page.
Edit the /WEB section of the firewall
Now ad 8082 in the Port field and hit the green Save button.
Stage 7 – Give it a test
Step 1 – Before you are running for a test drive, you have to firstly restart Nginx and start Varnish Cache server:
service nginx restart service varnish start
Step 2 – Next, you have to setup your domain to point to your server or you can simply edit your local hosts file in your PC.
Useful posts:
Step 3 – Now you can open up your favorite web browser (like Mozilla Firefox or Chrome) and access your website using your domain. Your website should now be displayed.
Stage 8 – More Varnish Configuration
I can see this stage is optional but you may also need this especially if you want to host complex scripts / CMS on production environment. What I’m talking here is tweaking Varnish. What I told you in Part 1 of this article is just editing default.vcl file defining where the backend is, plus there is also necessary config to forward real visitor’s IP addresses so your script and log files will record actual IP and not your server IP. For your information, there are several ready-to-use Varnish .vcl template to use with specific script like WordPress, Joomla or Drupal. Some what I can recommend are:
- https://github.com/pkhamre/wp-varnish
- https://github.com/mattiasgeniar/varnish-3.0-configuration-templates
- https://github.com/JohnMcLear/Wordpress-Varnish-VCL
- https://github.com/mattiasgeniar/varnish-4.0-configuration-templates
Feel free to Googling for more or come with your own configuration.
You can make sure if Varnish is really working on the right port (80) by doing curl:
curl -I http://domain.com
or by using web app like isvarnishworking.com:
if you see the X-Varnish line in the header respond that meaning Varnish is working correctly. With some advanced config in .vcl you can also get cache HIT or Missed status.
That’s all. Do not hesitate to leave comment below. Want to get faster update? Follow me on Twitter or subscribe using your email.
Hi sawiyati,
It’s doesn’t work for me with cloudfare, i got nginx default page. Can you help me to treat this issue ?
Best Regards
doublecheck your .vcl configuration
Hi Sawiyati,
I have double checked but nothing seem wrong. I have two ip address may be the setting that a little different.
Thank You
Thanks for all your great tutorials! I’ve used many of them.
I wish I knew what I was doing wrong with this one. I followed part 1 and 2 as carefully as possible. The site is still working, and the varnish service is running, but no signs that the web site is passing through it at all. Running varnishhist is showing no activity at all, neither hits nor misses.
I’ve had the site running fine on Vesta without varnish for months, but want to see if varnish will improve performance.
The part of your tutorial I’m least clear about is this:
==
“Step 5 – Edit nginx.conf file for each Vesta CP user located at /home/user/conf/web. This step is quite not efficient if you have several Vesta CP users as you have to edit them all. In my example I will edit nginx.conf file for user admin:
nano /home/admin/conf/web/nginx.conf”
==
My sites are all running under the user “jriemerm” and I changed that nginx.conf. There was no nginx.conf file under admin, but none of my sites are running under that user.
Any thoughts?
Hi Sawiyati,
Thanks for this tutorial. I’ve followed every step and I managed to make the varnish cache work on a vps with vestacp installed.
I only have a problem..
After installing the varnish, when i login to phpmyadmin I get two errors from the database.
1. The phpMyAdmin configuration storage is not completely configured, some extended features have been deactivated. To find out why click here.
Open new phpMyAdmin window
2. Connection for controluser as defined in your configuration failed.
Do you know what is might causing this?
How can I solve this problem?
Thank you in advance.
It worked like a charm. I would be of great help if you could make a tutorial on configuring pound for SSL with Varnish in Vestacp. I most of my sites SSL, please make one tutorial
Please create a tutorial with Vestacp + nginx + php-fpm + varnish
Could do a tutorial on installing vestacp with nginx + php fpm + varnish?
I was trying to setup Pound because Varnish Does not support SSL and it simply does not cache https requests, unfortunately I was not able to setup multiple websites with different SSL certificates.
I found out that Nginx can Handle Https request and read the certificate and then pass on the request to Varnish and varnish will serve Cached content 🙂
Since, i benifitted a lot from this website and tutorial I would like to share what I did to solve this problem.
We can use the template feature which is built inside Vestacp.
In the place where we enable Nginx Proxy, we have templates to choose, so we can create a new template.
Step 1:
Goto the location:
/usr/local/vesta/data/templates/web/nginx
Copy and rename the file caching.tpl to varnishssl.tpl
Copy and rename the file caching.stpl to varnishssl.stpl
Replac the content of the file varnishssl.stpl to the below.
server {
listen %ip%:%proxy_ssl_port%;
server_name %domain_idn% %alias_idn%;
ssl on;
ssl_certificate %ssl_pem%;
ssl_certificate_key %ssl_key%;
error_log /var/log/%web_system%/domains/%domain%.error.log error;
location / {
proxy_pass http://127.0.0.1:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Port 443;
proxy_set_header Host $host;
}
}
Save and exit.
Step 2:
Service nginx restart
Service varnish restart
Step3:
No goto Vestacp > Web and Edit. put a check mark on “Proxy Support Nginx”, Select the template we careated from the dropdown “varnishssl” and save.
Works great!!!!