首页 > 代码库 > nginx有关.htaccess小结

nginx有关.htaccess小结

可能很多朋友都常用nginx不支持.htaccess,只有apache才支持.htaccess文件,其实这是错误的看法nginx也是支持.hatccess的哦.

其实nginx和.htaccess一点关系都没有,只是一大堆人深受apache的影响觉得nginx应该也要支持.htaccess功能。在nginx的配置中直接include .htaccess文件就好

include /站点目录/.htaccess;

多么简单,但是更让人哭笑不得的是有大部分人根本就不知道nginx与apache是两个不同webserver程序,于是不少人这么干:在nginx里include .htaccess文件后,把apache伪静态规则的.htaccess上传到站点根目录下...无语。其实nginx可以include任何文件,而不仅仅是.htaccess。

其实nginx这样引用.htaccess跟apache还是有区别的,写法也不同:

1、nginx是启动引入,因为nginx特别注重效率,所以启动的时候会把所有的配置文件读入内存,然后启动个。如果发现有语法错误,则启动失败。而apache则是动态引入,每次访问站点都会重新载入配置。

2、nginx中的include是手动添加,指定目录。apache已经把.htaccess作为它的一种特色机制,并且支持站所有子目录都可以有.htaccess文件。

 

 

(以下摘自http://wiki.nginx.org/LikeApache-htaccess)

Like Apache: .htaccess

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

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

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 [Per Hour]Nginx FS StatsNginx FS ReadsApache FD 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 you‘re 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.

/var/www/drupal6/sites/example.com/themes/yourtheme/images/layout/header.png
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 [Per Hour] Nginx FS Stats Nginx FS Reads Apache FD Stats Apache FS Reads Comment
144,000 144,000 144,000 1,296,000 576,000 40 req/sec
324,000 324,000 324,000 2,916,000 1,296,000 90 req/sec
576,000 576,000 576,000 51,840,000 2,304,000 160 req/sec

Conclusion

Stop using .htaccess. It‘s horrible for performance. Nginx is designed to be efficient. Adding something like this would destroy that.