正在加载...

在Google上搜索的资料很多人都说nginx目前不支持.htaccess文件,我按照nginx的规则试验了一下,结果发现nginx是完全支持.htaccess文件的!

方法如下:

1. 在需要使用.htaccess文件的目录下新建一个.htaccess文件,
如本人的一个Discuz论坛目录:

vim /var/www/html/168pc/bbs/.htaccess

2. 在里面输入规则,我这里输入Discuz的伪静态规则:

# nginx rewrite rule
rewrite ^(.*)/archiver/((fid|tid)-[w-]+.html)$ $1/archiver/index.php?$2 last;
rewrite ^(.*)/forum-([0-9]+)-([0-9]+).html$ $1/forumdisplay.php?fid=$2&page=$3 last;
rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^(.*)/profile-(username|uid)-(.+).html$ $1/viewpro.php?$2=$3 last;
rewrite ^(.*)/space-(username|uid)-(.+).html$ $1/space.php?$2=$3 last;
rewrite ^(.*)/tag-(.+).html$ $1/tag.php?name=$2 last;
# end nginx rewrite rule

wq保存退出。

3. 修改nginx配置文件:

vim /etc/nginx/nginx.conf

4. 在需要添加伪静态的虚拟主机的server{}中引入.htaccess文件,如图所示:

include /var/www/html/168pc/bbs/.htaccess;(把这个改成你.htaccess文件的具体位置)

wq保存退出。

5. 重新加载nginx配置文件:

/etc/init.d/nginx reload

重新打开网页看看,如果伪静态正常就证明你的rewrite rule语法是正确的。

正常,完毕!

补充:偶在网上发现了个可以在线将Apache Rewrite伪静态规则自动转换为Nginx Rewrite网页。大家可以试试看。

http://www.anilcetin.com/convert-apache-htaccess-to-nginx/

此地址里面的内容包含可以完成上面说的略做修改的功能。就是把.htaccess中的规则自动转换成nginx下面可用的规则。

总结:.htaccess文件本来是apache专用的分布式配置文件,提供了针对每个目录改变配置的方法,即在一个特定的目录中放置一个包含指令的文件,其中的指令作用于此目录及其所有子目录。其实修改一下,nginx也可使用.htaccess文件实现多种功能。实现伪静态只是.htaccess的其中一个用途,.htaccess还可以做很多的用途,如过滤访问IP,设置web目录访问权限、密码等。

: http://www.ha97.com/942.html

本文相关评论 - 才 25 条评论
2010-07-26 06:12:19
WordPress 2.9.2 WordPress 2.9.2

[…] Apache的mod_rewrite是配合了在site folder里面的.htaccess文件,实现网页的静态访问,就是说,当网站有大量的固定内容页面,就可以使用这个功能,使得把那些相对固定的页面做成静态的,其实,我的理解是把所有的.php后缀的都做成.html,这样极大的提高了用户的访问速度,另外主要是减轻了服务器的负载,当用户请求的时候,直接返回伪静态页面,非常高效。 更正一下,Nginx可以使用.htaccess文件,看link […]

2010-08-25 09:01:38
Firefox 3.6.7 Firefox 3.6.7 CentOS 5 CentOS 5

“把所有的.php后缀的都做成.html,这样极大的提高了用户的访问速度”
这样没用的,php每次一样需要查询数据库,使用缓存效果就比较好。

2010-08-27 17:27:04
Firefox 3.6.8 Firefox 3.6.8 Windows XP Windows XP

这不还是nginx的格式吗?也不是Apache的.htaccess标准格式啊,你这样不就是写了个nginx的伪静态规则,然后include一下吗?叫什么名字都是一样的吧

2010-08-29 13:25:38
Firefox 3.1b3 Firefox 3.1b3 Solaris Solaris

你理解错偶的意思了,偶只是说nginx也可以用.htaccess文件来实现一些功能。不必都放在nginx.conf里面~~

2011-02-12 13:45:37
Firefox 3.6.13 Firefox 3.6.13 Windows XP Windows XP

。。。有点标题党的感觉!

2011-02-12 17:58:10
Google Chrome 9.0.597.98 Google Chrome 9.0.597.98 Windows 7 Windows 7

呵呵,不算标题党吧~~

2011-03-16 13:07:55
Maxthon 2.0 Maxthon 2.0 Windows XP Windows XP

我留言中的地址,想搞伪静态,不知道把nginx.conf文件放在vps的那个目录,是这个吗?/usr/local/nginx/conf,如果是,这里面已经有了nginx.conf,我在这个VPS绑定了几个域名,那么不同的伪静态规则怎么和域名对应呢?还是将伪静态规则放在域名对应的文件夹下面,谢谢!请赐教!

2011-05-18 22:59:23
Firefox 4.0 Firefox 4.0 GNU/Linux GNU/Linux

这个很简单,自己google吧~

2011-03-22 00:10:19
Maxthon 2.0 Maxthon 2.0 Windows XP Windows XP

已经解决了!还有个疑问,子目录下的nginx规则应该怎么写?

2011-03-26 11:29:20
Maxthon 2.0 Maxthon 2.0 Windows XP Windows XP

其实可以增加一aa.conf文件来实现。

2011-04-13 21:24:43
Firefox 3.6.16 Firefox 3.6.16 Ubuntu 10.10 Ubuntu 10.10

.htaccess 怎么防下载呢?

2011-05-18 23:01:57
Firefox 4.0 Firefox 4.0 GNU/Linux GNU/Linux

自己google吧~

2011-04-17 10:08:28
Sogou Explorer Sogou Explorer Windows XP Windows XP

楼主这个只是include而已,.htaccess 可以换成其他文件名,实质和apache的完全不同,呵呵。相当于就是把rewrite重写规则给独立出来一个文件罢了。

2011-05-18 23:01:36
Firefox 4.0 Firefox 4.0 GNU/Linux GNU/Linux

也可以这样理解,就当标题党吧~

2011-05-26 09:26:08
Firefox 4.0.1 Firefox 4.0.1 Windows XP Windows XP

leeon正解,确实只是一个include而已,换什么名都可以,跟apache的完全不同,就不是一个概念

2011-06-08 23:22:20
Google Chrome 11.0.696.77 Google Chrome 11.0.696.77 Windows 7 Windows 7

呵呵~~

2012-09-17 16:40:31
Google Chrome 19.0.1084.52 Google Chrome 19.0.1084.52 Windows 7 x64 Edition Windows 7 x64 Edition

你应该加一句,nginx中有有几句话需要开启,让nginx能够读.htaccess文件

骗子
2012-11-19 13:45:29
Firefox 16.0 Firefox 16.0 Windows 8 x64 Edition Windows 8 x64 Edition

关键是如何写nginx的伪静态,可是你已经写好了

2012-12-03 15:50:53
Firefox 17.0 Firefox 17.0 Windows XP Windows XP

简单明了 。。

2013-01-03 12:41:45
Google Chrome 23.0.1271.97 Google Chrome 23.0.1271.97 Windows 7 x64 Edition Windows 7 x64 Edition

问题在于每次需要重启nginx;而这很糟糕;因为.htaccess有时会被程序修改,经常是需要每次http request时动态解析的。

erhuabushuo
2013-01-11 19:09:56
Firefox 17.0 Firefox 17.0 Ubuntu x64 Ubuntu x64

deny ip 是我程序动态生成的。可是无法不reload生效

强人
2013-05-02 10:33:23
Google Chrome 26.0.1410.64 Google Chrome 26.0.1410.64 Windows XP Windows XP

很好的站点!非常感谢您的指教!

2013-06-02 02:50:24
Google Chrome 27.0.1453.94 Google Chrome 27.0.1453.94 Windows 8 x64 Edition Windows 8 x64 Edition

nginx -t 不通过。。。奇怪

2013-06-14 16:32:19
Internet Explorer 10.0 Internet Explorer 10.0 Windows 7 x64 Edition Windows 7 x64 Edition

使用不了啊……
nginx: [emerg] unknown directive “ErrorDocument” in /mnt/hgfs/0001688/html/.htaccess:7
nginx: configuration file /etc/nginx/nginx.conf test failed

2015-07-08 22:23:50
Google Chrome 43.0.2357.130 Google Chrome 43.0.2357.130 Windows 7 x64 Edition Windows 7 x64 Edition

博主威武,找了半天终于在你这里搞成功了。。。感谢