Running Apache behind an NGINX proxy was a good idea like in 2012.
…but not anymore.
I think we can just pick one and move on. Back in the days, Apache was faster at handling dynamic content (and also had htaccess compatibility) and NGINX (paired with Varnish) was way faster at handling static content. It seemed like a tricky compromise to pick one over the other. And for a few years, having both seemed like a crazy idea.
But nowadays, NGINX is fast enough with dynamic content and probably caches even better without Varnish. You really don’t need Apache at all anymore unless you want .htaccess compatibility. You don’t need to add another layer to your stack, and the extra proxy time and added stack complexity probably isn’t even worth the savings.
You can stop reading here or continue on for more details…
Why is anyone still running Apache behind NGINX?(Or NGINX in front of Apache?)
Reason #1 – They think there’s a performance benefit.
They think NGINX in front serves static content faster, and Apache in back serves dynamic content faster, therefore giving the best of both worlds. Like my intro paragraph, I no longer agree with this.
You should either use Apache alone:
- Apache excels in handling dynamic content – but is it really excelling by all that much? Probably not. Is there also a memory waste, maybe a little big. Will it change it your life for anybody with 100k visits/month and below? Probably not!
- Apache can still serve static content quickly via application caching – there are many plugins available for this. It’s really easy! Oh and yes, I’ve heard of people claiming that caching needs ram….err not all types of caching needs ram. The fastest type (static page caching) needs only fast disk space which is perfect for the SSD-era that we live in now.
- Apache has htaccess compatibility – this is nice for folks migrating over from shared hosting and not yet comfortable with NGINX commands and don’t have a sys-admin around to help them.
- Apache can be easily replaced by LiteSpeed – LiteSpeed works faster while using less resources (like NGINX), has built-in caching mechanism, and also htaccess compatible.
- Apache uses more resources than NGINX – but it’s only a bit more, it’s not like you can’t just add a few more bucks to the server to offset that difference. If you like/need Apache, use Apache. By the way, the newer Apache is getting better as well!
- Apache is more compatible with web applications – this is like a 2% difference, honestly. All serious web apps are now written for both Apache and NGINX, but sure there might a be few that only run on Apache. So there you go.
- Apache is compatible with cPanel – if you need cPanel, just stop here.
Or use NGINX alone:
- NGINX excels in static content – super fast, feels snappy. Requires very little resource use.
- NGINX is good enough for dynamic content – you’re not losing anything here. NGINX used to be bad for serving dynamic php but that’s no longer the case (thanks to updated modules). If it makes you feel any better, just know that many sys-admins prefer NGINX over Apache easily. It’s free, easy to work with, highly performant, and uses less server resources (saves on server costs).
- NGINX isn’t compatible with htaccess – this is part of its speed advantage. But also too, if you really have any rewrites or configurations you have to add and don’t know how to do it yourself, you can hire a sys-admin for cheap. There’s also free online generators that can convert Apache rewrite rules to NGINX.
- NGINX is easier for sys-admins to configure – they can set it up as a web server, reverse proxy, or load balancer easily. If you’re ever on that level of having to scale your application onto multiple servers, pure NGINX is gonna be a lot easier to manage it.
- NGINX handles attacks better – I think by its nature of using less memory, it can take more brute force and DDOS type of attacks but stalling out. Apache on the other hand has been noted many times for its easy vulnerability to security attacks.
Reason #2 – They want NGINX speed with Apache’s htaccess compatibility
Like I said above already (pick any one solution below):
- Either give up NGINX and just implement some static caching plugin. (keeping Apache)
- Or install LiteSpeed. (getting rid of NGINX)
- Or ditch Apache and figure out a way to rewrite rules.
Pick the option that best fits your scenario. Obviously, you may have consider the needs of others if this server is not only for you but also for webhosting clients. I would personally not deal with this proxy mess just to have htaccess compatibility.
Reason #3 – They really need Apache compatibility for some apps.
This is the only legit reason to have both and honestly in this instance, I wouldn’t even bother keeping around NGINX around but I understand why some do it (thinking NGINX will reduce wasteful ram usage on static content).
Apache is great to have if you’re running off WHM/cPanel and/or functioning as a webhosting service. There’s just so many modules you can install quickly that will be useful for all your different client use cases.
The real differences between Apache & NGINX
- Both are similar speeds. Both can be fast. (not a ‘difference’, actually)
- Apache is compatible with more applications.
- NGINX is easier for sys-admins to set-up.
- NGINX can handle way more traffic.
Unless you’re getting into the range of 1 million visits/month, you really \shouldn’t even be worried about which one to use. Both will be fine (and fast). Worst case scenario, Apache might require a $40 server to handle what NGINX can do with only a $10 server. But either way, you pick the one that fits your application best and then simply apply some caching or scale up from there. If you’ve really got that much traffic, just go with NGINX or fork out a few bucks and get LiteSpeed.
Other reading on Apache vs NGINX: