将Apache的.htaccess转换到nginx中

1、原来的网站在wamp环境下搭建完成,一切正常,上传到虚拟主机环境为lnmp,结果访问时可以打开主页,然后点其他页面全部报404错误;
 
2、经分析得出原因:原网站环境为wamp使用了伪静态,伪静态规则写在网站根目录的.htaccess文件中,Apache下默认识别此文件内容,而Nginx服务器不识别.htaccess文件,导致伪静态规则无效,自然无法解析url地址,导致404错误(文件不存在)
 
3、解决办法:因为Nginx服务器不识别.htaccess文件的,所以原来写在此文件中的伪静态规则需要转移出来,转移方式有两种:

方法一、如果想保留.htaccess文件,则在linux服务器此目录  /usr/local/nginx/conf/rewrite/ 下建立一个伪静态规则配置文件名字任取,例如:/usr/local/nginx/conf/rewrite/my.conf,将原来在.htaccess文件中的rewrite规则转换成nginx下的rewrite规则,提供一个自动转换网址
 
http://www.anilcetin.com/convert-apache-htaccess-to-nginx/    实测真实有用

CentOS 6.2实战部署Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用Nginx搭建WEB服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3下Nginx性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3下配置Nginx加载ngx_pagespeed模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4安装配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx搭建视频点播服务器(仿真专业流媒体软件) http://www.linuxidc.com/Linux/2012-08/69151.htm
 
本案例原规则

 
<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
 </IfModule>
 
转换成Nginx后

 
if (!-d $request_filename){
 set $rule_0 1$rule_0;
 }
 if (!-f $request_filename){
 set $rule_0 2$rule_0;
 }
 if ($rule_0 = "21"){
 rewrite ^/(.*)$ /index.php/$1 last;
 }

然后将转换好的规则替换.htaccess文件内容,将.htaccess文件导入my.conf,my.conf内容如下:
 
location / {
          
                include  /home/wwwroot/dijin.com/web/.htaccess;
                
 }

--至此完成伪静态转移

方法二、如果不想保留.htaccess文件,则前面的步骤照常,最后一步替换的时候直接将转换好的内容放入my.conf文件中,如下:
 
location / {
          
                if (!-d $request_filename){
            set $rule_0 1$rule_0;
                }
                if (!-f $request_filename){
            set $rule_0 2$rule_0;
                }
                if ($rule_0 = "21"){
          rewrite ^/(.*)$ /index.php/$1 last;
                }
                
  }

时间: 2024-08-02 04:29:24

将Apache的.htaccess转换到nginx中的相关文章

Last-Modified和ETag以及Apache和Nginx中的配置

1) 什么是”Last-Modified”? 在浏览器第一次请求某一个URL时,服务器端的返回状态会是200,内容是你请求的资源,同时有一个Last-Modified的属性标记此文件在服务期端最后被修改的时间,格式类似这样: Last-Modified: Fri, 12 May 2006 18:53:33 GMT 客户端第二次请求此URL时,根据 HTTP 协议的规定,浏览器会向服务器传送 If-Modified-Since 报头,询问该时间之后文件是否有被修改过: If-Modified-Si

PHP如何让apache支持.htaccess 解决Internal Server Error The server …错误

TP框架  打开 www.newtp.com/index.php/Home/Index/abc出现 如下错误: Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, [email protected] and inform

如何让apache支持.htaccess 解决Internal Server Error The server …错误

如何让apache支持.htaccess 解决Internal Server Error The server …错误 文章来源:小灰博客| 时间:2013-12-25 12:17:08| 作者:Leo | 2 条评论 文章分类:IT技术分享.PHP.小技巧     标签: .htaccess.apache 今天朋友发来一套叫“PHP爱家房产网源码v5.01_destoon内核%4017558.net”的程序,让修改点东西,我在本地环境打开测试,导入数据库后打开发现报错,应该是服务器内部错误,提

apache 伪静态 .htaccess

虽然网上有很多教程,但是我在这里进行简单对我用到的总结一下. 加载Rewrite模块: 在conf目录下httpd.conf中找到 LoadModule rewrite_module modules/mod_rewrite.so 这句,去掉前边的注释符号"#",或添加这句. .htacess文件,开头内容如下: RewriteEngine on RewriteEngine on是代表开启rewrite.在正是写规则前,先为大家讲一下重写中设计到的特殊字符的含义.和普通正则是通用的! *

Nginx中worker_connections的问题

转载自http://hi.baidu.com/u_chen/item/560f1504a0a77367d45a1184 查看日志,有一个[warn]: 3660#0: 20000 worker_connections are more than open file resource limit: 1024 !! 原来安装好nginx之后,默认最大的并发数为1024,如果你的网站访问量过大,已经远远超过1024这个并发数,那你就要修改worker_connecions这个值 ,这个值越大,并发数也

Nginx中的一些匹配顺序总结

Nginx中经常需要做各种配置,总结如下: 1.server_name配置 nginx中的server_name指令主要用于配置基于名称虚拟主机,同一个Nginx虚拟主机中,可以绑定多个server_name,各个域名用空格隔开即可.如下: server { listen 80; server_name test.com www.test.com; ... ... } 如果server_name有多个,那么通过代码如$_SERVER["SERVER_NAME"]获取的始终将是Nginx

apache开启.htaccess及.htaccess的使用方法

今天本地调试PHP程序,用到了.htaccess,而默认配置里面开启.htaccess,在网上找到了开启.htaccess的可行方法,供朋友们借鉴. 今天本地调试PHP程序,用到了.htaccess,而默认配置里面开启.htaccess,在网上找到了开启.htaccess的可行方法,供朋友们借 鉴.(开启的我他的方法不行,查找了一下AllowOverride None全部的都给换成AllowOverride All就了,原因不明,O(∩_∩)O~ 记得修改完httpd.conf以后一定要重启下a

nginx中的超时设置

参考博文: nginx中的超时设置 nginx使用proxy模块时,默认的读取超时时间是60s. 1. send_timeout syntax: send_timeout the time default: send_timeout 60 context: http, server, location Directive assigns response timeout to client. Timeout is established not on entire transfer of ans

phpstudy APACHE支持.htaccess以及 No input file specified解决方案

APACHE支持.htaccess以及 No input file specified解决方案 你的Apache安装文件夹conf里找到httpd.conf文件 索LoadModule rewrite_module modules/mod_rewrite.so 如果前面有注释符号#,请去掉.搜索Options FollowSymLinks,然后将它下面的AllowOverride None 修改为AllowOverride All: [1] 没想到遇见了 No input file specif