30 August 2019

htaccess on NGINX, a performance issue on high traffic sites. Why you should use NGINX.

The alternative to Apache for performing sites.

There are situations, such as those of high-traffic websites (sites with millions of visitors per day, for example), where the technological and system requirements are met to find the ideal recipe for maximum performance at the lowest possible computational cost. If we look at all the high-traffic website projects based on open source technologies (and therefore not Microsoft's .net, for example), we'll find that the only constant and common denominator is the web server we discussed in this article: NGINX.

Among the countless PROs, NGINX also has an apparent “con”, that is, it does not have htaccess support like the more famous Apache Web Server.

What is htaccess?

The .htaccess file (Hypertext Access) is a configuration file through which you can change the settings of a specific directory (that is, of a specific folder). By specific directory, in this context, we mean that all the settings chosen for that specific directory also apply to all subdirectories (or subfolders). An .htaccess file can only be used with compatible NCSA servers, such as Apache servers that support modules like mod_rewrite. This type of file is an integral part of the server configurations and any changes to it are automatically reflected on the site.

NGINX does not support htaccess

 

Someone mistakenly believes that the reason is in a position taken by the developer related to the "laziness" of implementing a feature that is certainly convenient on Apache.

But as we have learned from the world of Formula 1 and speed in general, if we want to run fast and be competitive we must make some sacrifices in order to lighten the weight of the vehicle or the webserver.

In this case, the lack of htaccess support isn't a flaw, but rather a feature . More specifically, it's one of those rare cases where not having something isn't a flaw, but rather a benefit.

If you want to use .htaccess at all costs, you are probably wrong and you shouldn't.

Why?

This is a great question. To start, for .htaccess to work, Apache must check EVERY directory in the requested path for the existence of a .htaccess file , and if it exists, it reads and parses EVERY one. This happens for EVERY request . Remember, when you change that single file, the change goes live immediately without having to restart the web server, precisely because Apache reads it every time.

Contacts

http://example.com/site/files/images/layout/header.png

Let's say we are not making any aliases and the file system is the path. This case is a common scenario with most of the sites out there.

There is the / directory, so site /, files /, images / and layout /. This is equivalent to 5 directories that might have an .htaccess file. Let's say you have added an .htaccess in /, files / and images /. There are three .htaccess files. It is quite typical.

Now the numbers, which is 6 file system stat and 4 file system read. Including one for the requested file. This happens for every reading. We will ignore the parsing time because both NGINX and Apache have to do this and we will consider the time difference for this to be negligible.

Requests / Hour NGINX stats NGINX FS readings Apache stat Apache FS readings Comment
1 1 1 6 4 Single request [practically no load]
10 10 10 60 40 Ten requests [practically no load]
3.600 3.600 3.600 21.600 14.400 1 req / sec [very low load]
144.000 144.000 144.000 864.000 576.000 40 req / sec [Moderate traffic - nothing very big]
324.000 324.000 324.000 1,944,00 1.296.000 90 req / sec [Higher traffic site - not massive]
576.000 576.000 576.000 3.456.000 2.304.000 160 req / sec [Rather high traffic, although not yet massive]

 

Even more numbers and slowness

The default for Apache is to use AllowOverride All. Let's take a look at this for a Drupal website. An image for the theme. If your DocRoot website is on, /var/www/drupal6/we just added more file system statistics. This adds 3 stats per request. This is an incredibly common Apache / Drupal installation. It is the end result of countless guides out there.

/var/www/drupal6/sites/example.com/themes/yourtheme/images/layout/header.png

Two .htaccess files will be in this path unless you create them. I assume you have added one in / var / www / because this is common.

Requests / Hour NGINX stats NGINX FS readings Stats Apache FD Apache FS readings Comment
144.000 144.000 144.000 1.296.000 576.000 40 reqs/sec
324.000 324.000 324.000 2.916.000 1.296.000 90 reqs/sec
576.000 576.000 576.000 51.840.000 2.304.000 160 reqs/sec

Interpretation of the data

To be very straightforward without too many words and diplomacy, Apache at worst does 100 times I / O accesses compared to NGINX, at best the example above does 10 times as many.

And we wanted to propose a common case that's neither optimal nor terrible, but think about those nested directories that go 20 or 30 nodes deep. Do you have any idea what that becomes? Apache does 1000 more operations than NGINX's single one . That's way too much even for the most die-hard Apache and htaccess support fanboy.

Practical performance feedback

A real-world performance measurement implies that disk I/O will be significantly increased if you have a high-traffic site with a large number of requests per second. Imagine you bought the latest 1Gbps nVME SSD to ensure performance and I/O throughput on your high-performance WordPress server, and then you install Apache on it. In some cases, you'll degrade performance and I/O so much, even at the kernel level, that you'll get the same performance as a SATA HDD.

Apache is therefore good for classic sites but not for high performance sites.

Are you from Apache do you want to migrate to NGINX but do you necessarily need htaccess?

One of the reasons why you are not reluctant to convert to NGINX is the habit with which you have grown professionally to use Apache provided by default on panels like cPAnel or Plesk. If you are born, grow up with a technology, it is normal and peaceful to die even with the exact same technology. It takes a bit of curiosity and self-criticism to question some false beliefs and try to do better by using a technological solution that is still mounted on most high-traffic sites.

The main problem is to keep using .htaccess on a system that doesn't actually support .htaccess. Concept that if read quickly makes you want to close the page and continue with the good old Apache, but wait maybe we didn't understand there is more you should know and that you will surely like.

Not reading htaccess doesn't mean you can't use rewrite rules

We've written so far that NGINX doesn't read htaccess files, but not that it can't take advantage of the rewrite features . htaccess is a proprietary format used essentially only by Apache, so saying that NGINX doesn't read htaccess is a bit like saying that NGINX doesn't read a proprietary Apache format, but not that it can have the same, or even better, features than htaccess in its own way, according to its own rules.

The only problem is trying to understand how you can convert a project from APACHE htaccess to NGINX rewrite.

Rewrite Rules on NGINX

As we've said, NGINX supports rewrite rules in its own way. It does so according to its own syntax, completely different from that of .htaccess, and it follows its own philosophy, which is to match the rules at the web server's startup and only then. Therefore, unlike Apache, rewrite rules are written to the virtual host's configuration and will be valid for the entire duration of the web server's execution. There's no way to create a .htaccess file (or any NGINX equivalent) and have the web server digest it without restarting the service.

On NGINX, if you want to insert a new rewrite rule, such as a simple 301 redirect from an old deleted page to a new one, you must edit the configuration file of the vhost in question, such as sitename.conf, add the rewrite rule, and then reload and restart the web server using the service nginx reload or service nginx restart syntax to reload the new rule and make it work correctly.

Commercial and organizational problems

One of the major problems with NGINX's limited use, besides the lack of .htaccess support, is that writing new rewrite rules in the vhost requires modifying the vhost and restarting the web server. Both operations require superuser, or root, privileges. In a shared hosting environment where the average user isn't a system administrator, performing these tasks would have been completely impossible, thus undermining the sale of low-cost or average-user hosting services.

Only recently have some control panels like Plesk and cPanel implemented the ability to choose which web server to use between NGINX and Apache (and even a mix of the two solutions via reverse proxy) via the usual point-and-click interface, but despite this, many webmasters, developers and SEOs continue to stubbornly use the same old Apache.

 

Convert htaccess to NGINX

We could do the phenomena and say that basically converting an .htaccess to NGINX is not very complex as the syntax itself is quite simple (in our opinion even easier than the native htaccess one), but then what we will tell you when you need to convert those 500 rewrite lines from htaccess to NGINX? 3 or 4 days to convert all the rules? No thanks.

There are faster and more immediate solutions made available by companies that have decided to make publicly available online converters that allow you to convert the rewrite rules from htaccess to NGINX.

Unfortunately, since there is nothing official and effective, most tend to produce rules that either don't work or work but approach with wrong syntax.

However, the best one out there that allows you to convert medium-complex rules (such as 301 rewrites and similar) in 99% of cases is the one you can find at this link

As the name suggests, it's an htaccess converter that allows you to convert htaccess files to NGINX rules. Here's an example of a real-world use case we used this evening to port a new client's site from Apache to NGINX for performance reasons.

htaccess convert nginx

Going to paste the .htaccess rules in the htaccess section and pressing the "convert" button we will return the respective NGINX syntax that we will copy and paste in the NGINX virtual host configuration and then restart the webserver to test the correctness.

Conclusions

If you have sites of the butcher or pizzeria under your house with a few visits a month go well with Apache and .htaccess, if you manage sites with high traffic and high performance use NGINX as do all sites with very high traffic.

We use NGINX and leave Apache to amateurs.

 

 

Do you have doubts? Don't know where to start? Contact us!

We have all the answers to your questions to help you make the right choice.

Chat with us

Chat directly with our presales support.

0256569681

Contact us by phone during office hours 9:30 - 19:30

Contact us online

Open a request directly in the contact area.

DISCLAIMER, Legal Notes and Copyright. RedHat, Inc. holds the rights to Red Hat®, RHEL®, RedHat Linux®, and CentOS®; AlmaLinux™ is a trademark of the AlmaLinux OS Foundation; Rocky Linux® is a registered trademark of the Rocky Linux Foundation; SUSE® is a registered trademark of SUSE LLC; Canonical Ltd. holds the rights to Ubuntu®; Software in the Public Interest, Inc. holds the rights to Debian®; Linus Torvalds holds the rights to Linux®; FreeBSD® is a registered trademark of The FreeBSD Foundation; NetBSD® is a registered trademark of The NetBSD Foundation; OpenBSD® is a registered trademark of Theo de Raadt; Oracle Corporation holds the rights to Oracle®, MySQL®, MyRocks®, VirtualBox®, and ZFS®; Percona® is a registered trademark of Percona LLC; MariaDB® is a registered trademark of MariaDB Corporation Ab; PostgreSQL® is a registered trademark of PostgreSQL Global Development Group; SQLite® is a registered trademark of Hipp, Wyrick & Company, Inc.; KeyDB® is a registered trademark of EQ Alpha Technology Ltd.; Typesense® is a registered trademark of Typesense Inc.; REDIS® is a registered trademark of Redis Labs Ltd; F5 Networks, Inc. owns the rights to NGINX® and NGINX Plus®; Varnish® is a registered trademark of Varnish Software AB; HAProxy® is a registered trademark of HAProxy Technologies LLC; Traefik® is a registered trademark of Traefik Labs; Envoy® is a registered trademark of CNCF; Adobe Inc. owns the rights to Magento®; PrestaShop® is a registered trademark of PrestaShop SA; OpenCart® is a registered trademark of OpenCart Limited; Automattic Inc. holds the rights to WordPress®, WooCommerce®, and JetPack®; Open Source Matters, Inc. owns the rights to Joomla®; Dries Buytaert owns the rights to Drupal®; Shopify® is a registered trademark of Shopify Inc.; BigCommerce® is a registered trademark of BigCommerce Pty. Ltd.; TYPO3® is a registered trademark of the TYPO3 Association; Ghost® is a registered trademark of the Ghost Foundation; Amazon Web Services, Inc. owns the rights to AWS® and Amazon SES®; Google LLC owns the rights to Google Cloud™, Chrome™, and Google Kubernetes Engine™; Alibaba Cloud® is a registered trademark of Alibaba Group Holding Limited; DigitalOcean® is a registered trademark of DigitalOcean, LLC; Linode® is a registered trademark of Linode, LLC; Vultr® is a registered trademark of The Constant Company, LLC; Akamai® is a registered trademark of Akamai Technologies, Inc.; Fastly® is a registered trademark of Fastly, Inc.; Let's Encrypt® is a registered trademark of the Internet Security Research Group; Microsoft Corporation owns the rights to Microsoft®, Azure®, Windows®, Office®, and Internet Explorer®; Mozilla Foundation owns the rights to Firefox®; Apache® is a registered trademark of The Apache Software Foundation; Apache Tomcat® is a registered trademark of The Apache Software Foundation; PHP® is a registered trademark of the PHP Group; Docker® is a registered trademark of Docker, Inc.; Kubernetes® is a registered trademark of The Linux Foundation; OpenShift® is a registered trademark of Red Hat, Inc.; Podman® is a registered trademark of Red Hat, Inc.; Proxmox® is a registered trademark of Proxmox Server Solutions GmbH; VMware® is a registered trademark of Broadcom Inc.; CloudFlare® is a registered trademark of Cloudflare, Inc.; NETSCOUT® is a registered trademark of NETSCOUT Systems Inc.; ElasticSearch®, LogStash®, and Kibana® are registered trademarks of Elastic NV; Grafana® is a registered trademark of Grafana Labs; Prometheus® is a registered trademark of The Linux Foundation; Zabbix® is a registered trademark of Zabbix LLC; Datadog® is a registered trademark of Datadog, Inc.; Ceph® is a registered trademark of Red Hat, Inc.; MinIO® is a registered trademark of MinIO, Inc.; Mailgun® is a registered trademark of Mailgun Technologies, Inc.; SendGrid® is a registered trademark of Twilio Inc.; Postmark® is a registered trademark of ActiveCampaign, LLC; cPanel®, LLC owns the rights to cPanel®; Plesk® is a registered trademark of Plesk International GmbH; Hetzner® is a registered trademark of Hetzner Online GmbH; OVHcloud® is a registered trademark of OVH Groupe SAS; Terraform® is a registered trademark of HashiCorp, Inc.; Ansible® is a registered trademark of Red Hat, Inc.; cURL® is a registered trademark of Daniel Stenberg; Facebook®, Inc. owns the rights to Facebook®, Messenger® and Instagram®. This site is not affiliated with, sponsored by, or otherwise associated with any of the above-mentioned entities and does not represent any of these entities in any way. All rights to the brands and product names mentioned are the property of their respective copyright holders. All other trademarks mentioned are the property of their respective registrants. MANAGED SERVER® is a European registered trademark of MANAGED SERVER SRL, with registered office in Via Flavio Gioia, 6, 62012 Civitanova Marche (MC), Italy and operational headquarters in Via Enzo Ferrari, 9, 62012 Civitanova Marche (MC), Italy.

JUST A MOMENT !

Have you ever wondered if your hosting sucks?

Find out now if your hosting provider is hurting you with a slow website worthy of 1990! Instant results.

Close the CTA
Back to top