Htaccess Di Nginx

  • Administrator
  • 04 Apr 2022, 14:22
  • nginx,htaccess
Portfolio-title

You can’t do this. You shouldn’t. If you need .htaccess, you’re probably doing it wrong.

Why?

This is a great question. For starters, for .htaccess to work Apache needs to check EVERY directory in the requested path for the existence of a .htaccess file and if it exists it reads EVERY one of them and parses it. This happens for EVERY request. Remember that the second you change that file, it’s effective. This is because Apache reads it every time.

Numbers

Let’s say we’re not doing any funky aliasing and the file system looks like the path. This covers most of the sites out there. There is the / directory, then site/, files/, images/, and layout/. This amounts to 5 directories that could have a .htaccess file. Let’s say you added a .htaccess in /, files/ and images/. That’s three .htaccess files. That’s pretty typical.

Now the numbers, that’s 6 file system stats and 4 file system reads. Including one for the requested file. This happens for every read. We’ll ignore parsing time because both NGINX and Apache need to do this and we’ll consider the difference in time for this negligible.

Requests / HourNGINX FS StatsNGINX FS ReadsApache FS StatsApache FS ReadsComment
11164Single Request [Pretty much no load]
1010106040Ten Requests [Pretty much no load]
3,6003,6003,60021,60014,4001 req/sec [Very low load]
144,000144,000144,000864,000576,00040 req/sec [Moderate traffic - nothing very large]
324,000324,000324,0001,944,001,296,00090 req/sec [Higher traffic site - not massive]
576,000576,000576,0003,456,0002,304,000160 req/sec [Pretty high traffic - still not massive though]

More Numbers

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


Two .htaccess files will be in this path unless you create your own. I’ll be assuming you added one in /var/www/ because this is common.

Requests / HourNGINX FS StatsNGINX FS ReadsApache FD StatsApache FS ReadsComment
144,000144,000144,0001,296,000576,00040 req/sec
324,000324,000324,0002,916,0001,296,00090 req/sec
576,000576,000576,00051,840,0002,304,000160 req/sec

Conclusion

Stop using .htaccess. It’s horrible for performance. NGINX is designed to be efficient. Adding something like this destroys that.