Varnish, Nginx and Apache

Discussion regarding Joomla! 3.x Performance issues.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Locked
almooj-craig
Joomla! Guru
Joomla! Guru
Posts: 500
Joined: Mon Aug 11, 2008 3:05 pm

Varnish, Nginx and Apache

Post by almooj-craig » Wed Oct 07, 2015 4:08 pm

Hello All,

Currently I use Nginx before Apache to serve out static files, .gz if available otherwise it gzips them. Then Apache generates the other pages. Modules are cached and I use a modified page cache that stores the files gziped.

I am toying around with trying Varnish ahead of Nginx, so that it can serve out it's own cached files. I assume that it will cache everything unless the file is excluded. I read this interesting article.
https://www.varnish-cache.org/trac/wiki/ArchitectNotes

These times are from http://www.webpagetest.org and the dns has been cached.

Here is the time it takes Apache to build and then serve out a page and store it that has not been cached. Most likely all the modules have been cached.

DNS Lookup: 2 ms
Initial Connection: 319 ms
Time to First Byte: 498 ms
Content Download: 124 ms
Total Time: 943 ms
Bytes In (downloaded): 10.9 KB

Once the page has been cached, all of the required css is stuffed into the header. Then there are two ways that file can be served out.

(1) Let Nginx serve the cached home page only, by using rewrite and serve out the gziped file directly, this uses no php or database access. Here is that time:

DNS Lookup: 2 ms
Initial Connection: 27 ms
Time to First Byte: 35 ms
Content Download: 145 ms
Total Time: 209 ms
Bytes In (downloaded): 11.6 KB

(2) Let Apache serve out the file. It uses a little php to figure out if they can accept gzip, checks to see if their ip has been banned and sets the session cookie and inserts it into the database. It's time is:

DNS Lookup: 2 ms
Initial Connection: 27 ms
Time to First Byte: 48 ms
Content Download: 148 ms
Total Time: 225 ms
Bytes In (downloaded): 11.8 KB

The Nginx is 16 ms quicker than Apache but I'm not setting cookies or session info. The other odd thing is that Nginx always gets a "start to render" time that's just after the file has been downloaded, but Apache will usually get a "start to render" time that happens before the file has finished loading, most likely because the file is chunked and Nginx does not chunk the file. As a consequence the "start to render" times are usually very similar.

Now the Varnish question. It seems that the only times Varnish would improve are the Initial Connection and Time to First Byte. Is that correct? Also Varnish would not set the session cookie unless you used a httprequest to Apache, and there is the problem of getting the correct remote ip address, but I read that there is a plugin for that.

As a guess, using Varnish, how much do you think the Initial Connection and Time to first Byte would drop from 75ms? How much would the speed of a jpg file improve if it was served by Varnish?

Thanks,
Craig

User avatar
ribo
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 3507
Joined: Sun Jan 03, 2010 8:47 pm
Contact:

Re: Varnish, Nginx and Apache

Post by ribo » Wed Oct 07, 2015 4:21 pm

chat room spontes : http://www.spontes.com

almooj-craig
Joomla! Guru
Joomla! Guru
Posts: 500
Joined: Mon Aug 11, 2008 3:05 pm

Re: Varnish, Nginx and Apache

Post by almooj-craig » Wed Oct 07, 2015 4:31 pm

Ribo,

Thanks, I previously read that discussion and that was what started me thinking about Varnish. It seems that you can overcome session and ip problems with configuration and plugins, but nobody stated how much of a speed improvement there was. Thus my post.

Craig

User avatar
ribo
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 3507
Joined: Sun Jan 03, 2010 8:47 pm
Contact:

Re: Varnish, Nginx and Apache

Post by ribo » Wed Oct 07, 2015 4:47 pm

almooj-craig wrote:
and there is the problem of getting the correct remote ip address, but I read that there is a plugin for that.



Thanks,
Craig
In ngingx you must set except varnish default.vcl and nginx.conf to correct the ip
In apache there are apache modules can do the job

For me the boost was so big. The varnish cache (and when we say cache for example we speak about we hit homepage and we again hit homepage) is like you have html site. I saw in a site of mine in cache from a google chrome plugin and with dsl connection of mine until 0.13sec from 0.40sec. In other i saw bigger time but not much bigger.
chat room spontes : http://www.spontes.com

almooj-craig
Joomla! Guru
Joomla! Guru
Posts: 500
Joined: Mon Aug 11, 2008 3:05 pm

Re: Varnish, Nginx and Apache

Post by almooj-craig » Wed Oct 07, 2015 9:26 pm

Ribo,

Thanks for the info. Am I correct in thinking that Varnish could improve the Initial Connection and the Time to First Byte numbers? Possibly the Content Download time might be affected if the file is served directly from memory.

Do you ever use webpagetest.org? It has a nice feature that breaks down the response time of each file. On the details page, just click on the file and it gives you the times of each step. If possible could you post the following times for the index home file that Varnish serves out? Then I could compare them to the numbers I get from Nginx and Apache.

Home page using only Nginx:
DNS Lookup: 2 ms
Initial Connection: 27 ms
Time to First Byte: 35 ms
Content Download: 145 ms
Total Time: 209 ms
Bytes In (downloaded): 11.6 KB

Thanks,
Craig

User avatar
ribo
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 3507
Joined: Sun Jan 03, 2010 8:47 pm
Contact:

Re: Varnish, Nginx and Apache

Post by ribo » Wed Oct 07, 2015 9:39 pm

I am not trust 100% all this pages. I trust more my eye than these. Also every site is different from images, css, js, requests, traffic, server requirements,etc.
You have not something to lose to test varnish. If you have the knowledge how to install and configure. Also you can exclude domains from varnish.
I used apache without varnish and now i use with varnish and the boost is very very big. That' s all.
chat room spontes : http://www.spontes.com

almooj-craig
Joomla! Guru
Joomla! Guru
Posts: 500
Joined: Mon Aug 11, 2008 3:05 pm

Re: Varnish, Nginx and Apache

Post by almooj-craig » Sun Oct 11, 2015 11:51 am

Ribo,

I agree that the first impression of how a page loads is important, but I also like to quantify the different aspects of how the server is performing.

I installed a version 4 of varnish so when I tried to use the default.vcl example from
https://snipt.net/fevangelou/the-perfec ... a-websites there were a lot changes that needed to be made. Anyway I ran out of time but wanted to get something up and running so I used a version 4 vcl designed for wordpress. When I finish converting the above example I'll post it.

Craig

almooj-craig
Joomla! Guru
Joomla! Guru
Posts: 500
Joined: Mon Aug 11, 2008 3:05 pm

Re: Varnish, Nginx and Apache

Post by almooj-craig » Sun Oct 11, 2015 12:03 pm

Here are the current numbers from my previous nginx speed compared with the varnish speed.

Nginx served directly from cache using rewrite.
DNS Lookup: 2 ms
Initial Connection: 27 ms
Time to First Byte: 35 ms
Content Download: 145 ms
Total Time: 209 ms
Bytes In (downloaded): 11.6 KB

Varnish after it's been cached by varnish. X-Cache: HIT and X-Cache-Hits: 11
DNS Lookup: 2 ms
Initial Connection: 24 ms
Time to First Byte: 49 ms
Content Download: 146 ms
Total Time: 221 ms
Bytes In (downloaded): 11.6 KB

Currently Varnish is a tad slower, but I need to do a few more tests, after I convert that other vcl file.

Craig

almooj-craig
Joomla! Guru
Joomla! Guru
Posts: 500
Joined: Mon Aug 11, 2008 3:05 pm

Re: Varnish, Nginx and Apache

Post by almooj-craig » Sun Oct 11, 2015 12:23 pm

Here is a little better one for Varnish:

DNS Lookup: 1 ms
Initial Connection: 25 ms
Time to First Byte: 44 ms
Content Download: 128 ms
Total Time: 198 ms
Bytes In (downloaded): 11.6 KB

User avatar
ribo
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 3507
Joined: Sun Jan 03, 2010 8:47 pm
Contact:

Re: Varnish, Nginx and Apache

Post by ribo » Sun Oct 11, 2015 7:34 pm

That s good. I am waiting from your last test results and your thoughts about varnish and its vcl configuration
chat room spontes : http://www.spontes.com

almooj-craig
Joomla! Guru
Joomla! Guru
Posts: 500
Joined: Mon Aug 11, 2008 3:05 pm

Re: Varnish, Nginx and Apache

Post by almooj-craig » Fri Oct 23, 2015 11:10 am

The set up is Varnish 4.1 (memory cache) -> Nginx (static files) -> Apache (Joomla)

This is a different page, it's a little smaller and I'm using Varnish memory storage. I believe the other times I posted were first HDD storage, then SSD storage.

DNS Lookup: 1 ms
Initial Connection: 27 ms
Time to First Byte: 45 ms
Content Download: 102 ms
Total Time: 175ms
Bytes In (downloaded): 9.7 KB
Start to Render: 0.197s
Document Complete: 0.217s

Overall it's a tad quicker than serving out a file that Nginx gets directly out of the cache (home page only) or letting Apache serve out a .gz style cached file. Also static files are not cached but delivered by Nginx and even those seem a little quicker. With Varnish in front, Nginx is more responsive, where before somtimes it would get a little sleepy on the initial connection time.

The Varnish configuration can be a little daunting, a lot of trial and error to see what works with the way I run the Joomla cache. Then lots of testing from different locations.

One thing to note is that the User-Agent header is used in the hash so that should be unset if you want more hits. This could cause some problems if your javascript relies on that info which really isn't a good idea anyway.

Also it's a good idea to strip cookie using regexp or just unset, set-cookie is harder to deal with because you only seem to have access to the first set-cookie unless you use a vmod which I haven't tried yet because the two usefull ones seem to be for version 3. I remember reading that Varnish wouldn't cache pages with cookies or set-cookie, but at least with Varnish 4.1 it will and then Varnish will return a set-cookie in a cached page. I believe both cookie and set-cookie are used in the lookup hash but I'm not sure.

Craig

almooj-craig
Joomla! Guru
Joomla! Guru
Posts: 500
Joined: Mon Aug 11, 2008 3:05 pm

Re: Varnish, Nginx and Apache

Post by almooj-craig » Mon Nov 02, 2015 2:39 pm

Here is an example webpagetest waterfall screen shot of a Joomla page that has one image in the article. This page has already been cached by Varnish and the css that is used in the page is written directly in the header. The static files are not cached by Varnish but served directly using Nginx and are from .gz if they are js or css. The two txt files are served by Apache from a httprequest because that content changes more frequently than the cached page.
Capture.JPG
You do not have the required permissions to view the files attached to this post.

User avatar
leolam
Joomla! Master
Joomla! Master
Posts: 20652
Joined: Mon Aug 29, 2005 10:17 am
Location: Netherlands/ Germany/ S'pore/Bogor/ North America
Contact:

Re: Varnish, Nginx and Apache

Post by leolam » Mon Nov 02, 2015 5:18 pm

Forgetting MariaDB 10.x.x analysis in this threat (Dro-in for MySQL and about 50% faster)

Leo 8)
Joomla's #1 Professional Services Provider:
#Joomla Professional Support: https://gws-desk.com -
#Joomla Specialized Hosting Solutions: https://gws-host.com -

almooj-craig
Joomla! Guru
Joomla! Guru
Posts: 500
Joined: Mon Aug 11, 2008 3:05 pm

Re: Varnish, Nginx and Apache

Post by almooj-craig » Mon Nov 02, 2015 11:01 pm

Leo,

Thanks for the comment.

I currently use MariaDB, but I haven't used the analyze statement to check any queries. It seems like a good tool to check and optimize complex queries. Have you checked a lot of Joomla's queries?

Craig


Locked

Return to “Performance - Joomla! 3.x”