nginx上配置.htaccess伪静态规则

在apache上.htaccess转向,只要apache编译的时候指明支持rewrite模块即可。

但是换到nginx上方法会有不同,有人说把.htaccess转向规则写到nginx的配置文件里面,官方提供的方法之一,肯定可行的。
不过,此方法有个问题:不方便,下次要更改一个伪静态转向规则的时候还得去nginx的配置文件或者nginx的虚拟网站的配置文件里面去改,相比apache直接在目录下放置.htaccess文件,nginx的这个办法显然很原始。

解决方法:
在nginx的配置文件中include .htacces文件就可以实现相同的功能了。

举个例子,要把www.jbxue.com的.htaccess迁移到nginx上,几个步骤:

第一步:修改.htaccess文件,因为apache的rewrite转向规则跟nginx的转向规则还是有一些不一样的,典型的不一样有nginx的根目录需要写在每行转向的地址前,每行规则必须以分号(;)结束,301或者404等跳转使用不同的格式。

apache上htaccess转换到nginx上:

RewriteEngine On
RewriteBase /
RewriteRule ^show-([-]+)-([-]+)\.html$ index.php?action=show&id=$&page=$
RewriteRule ^category-([-]+)-([-]+)\.html$ index.php?action=index&cid=$&page=$
RewriteRule ^archives-([-]+)-([-]+)\.html$ index.php?action=index&setdate=$&page=$
RewriteRule ^(archives|search|reg|login|index|links)\.html$ index.php?action=$
RewriteRule ^(comments|tagslist|trackbacks|index)-([-]+)\.html$ index.php?action=$&page=$
rewriteCond %{http_host} ^jbxue.com [NC]
rewriteRule ^(.*)$ http://www.jbxue.com/$1 [R=301,L]
ErrorDocument http://www.jbxue.com/

转换成nginx的规则

rewrite ^/show-([-]+)-([-]+)\.html$ /index.php?action=show&id=$&page=$;
rewrite ^/category-([-]+)-([-]+)\.html$ /index.php?action=index&cid=$&page=$;
rewrite ^/archives-([-]+)-([-]+)\.html$ /index.php?action=index&setdate=$&page=$;
rewrite ^/(archives|search|reg|login|index|links)\.html$ /index.php?action=$;
rewrite ^/(comments|tagslist|trackbacks|index)-([-]+)\.html$ /index.php?action=$&page=$;
if ($host != 'www.jbxue.com' ) {
rewrite ^/(.*)$ http://www.jbxue.com/$1 permanent;
}
error_page http://www.jbxue.com/;

第二步:修改nginx的配置文件,增加include该.htaccess文件
vi /etc/nginx/sites-available/www.jbxue.com
增加一行:include /var/www/www.jbxuecom/.htaccess

修改为相应的地址。

第三步:测试并重启

代码示例:

/etc/init.d/nginx -configtest
重启生效:
代码示例:

/etc/init.d/nginx  restart

apache上.htaccess转向nginx上配置.htaccess伪静态规则的更多相关文章

  1. Nginx在windows上安装 及 Nginx的配置及优化

    https://www.cnblogs.com/Chiler/p/8027167.html http://www.runoob.com/linux/nginx-install-setup.html 前 ...

  2. centos7 Apache开启URL重写组件并配置.htaccess实现伪静态

    第一.修改httpd.conf文件 A - 在etc/httpd/conf/目录下的httpd.conf 文件,找到: LoadModule rewrite_module modules/mod_re ...

  3. tp5 url 线上访问 在nginx 上 出现404错误,解决办法(1.80nginx 配置 pathInfo)

      对于ThinkPHP的URL访问路劲如:http://域名/index.php/Index/BlogTest/read,原先的Nginx的是不支持的pathinfo路劲的,导致你在thinkPHP ...

  4. Apache、tomcat、Nginx常用配置合集

    配置文件地址: Apache: /etc/httpd/conf/httpd.conf tomcat: /usr/local/tomcat/conf/server.xml Nginx  : /usr/l ...

  5. Apache模块mod_security 和 Nginx过滤配置

    1.安装mod_securityyum install mod_security 2.安装mod_security_crsyum install mod_security_crs 3.在/etc/ht ...

  6. tp5 apache 转 nginx 需要配置的伪静态

    location / { if (!-e $request_filename){ rewrite ^(.*)$ /index.php?s=$ last; break; } }

  7. centos nginx server_name 配置域名访问规则

    今天配置Server_name时,希望禁用一些域名,应为这些域名我想让通过另外一个Server配置 server_name "~^((\w*[^w]{1}\w*)|w{1,2})\.hell ...

  8. 通过ngx-lua来统计Nginx上的虚拟主机性能数据

    Web server调研分析 Filed under: Web Server — cmpan @ 2012-10-29 20:38:34 摘要 简单可依赖的架构首先需要有一个简单可依赖的前端WebSe ...

  9. Nginx Location配置总结

    Nginx Location配置总结 语法规则: location [=|~|~*|^~] /uri/ { - }= 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即 ...

随机推荐

  1. 在Visualsvn Server上创建svn账号和密码

    VisualSVN Server是一个集成的svn服务端工具,是一款svn服务端不可多得的好工具.可以先安装好VisualSVN Server后,运行VisualSVN Server Manger,然 ...

  2. CefSharp开发

    CefSharp是用chromium内核开发的.net版本浏览器工具.目前只支持X86模式.所以在调试的时候要把平台改为X86 CefSharp开发指引:https://ourcodeworld.co ...

  3. 前后端分离, 前端如何防止直接输入URL进入页面?

    转自:https://blog.csdn.net/weixin_41829196/article/details/80444870 前后端分离,如何防止用户直接在地址栏输入url进入页面,也就是判断用 ...

  4. git error:【fatal: unable to access 'https://github.com/userId/prjName.git/': err or setting certificate verify locations:】

    $ git pull origin master fatal: unable to access 'https://github.com/userId/prjName.git/': err or se ...

  5. Redis 优化之 tcp-backlog

    默认值 511 tcp-backlog:511 此参数确定了TCP连接中已完成队列(完成三次握手之后)的长度, 当然此值必须不大于Linux系统定义的/proc/sys/net/core/somaxc ...

  6. 在Ubuntu16.04.4上安装docker

    在Ubuntu16.04.4上安装docker 一.环境准备     首先我们需要一台Ubuntu16.04 虚拟机或者真机,其次,我们对其进行环境配置.       1.1.移除已经安装过的dock ...

  7. .NET控件命名规范

    一.基本数据类型前缀 数据类型    数据类型简写 Array    arr Boolean    bln Byte    byt Char    chr DateTime    dtm Decima ...

  8. 双十一问题:kafka消费能力低下原因思考

    抛去cpu.内存等机器原因,在每个分区皆分配一个进程消费的情况下,利用扩机器来提高kafka消费速率已无能为力 此时发现,在实际洪峰时段的消费速率元达不到先前压测时的消费速率 原因思考: 1.洪峰时段 ...

  9. 用w32tm设置服务器时间同步

    服务器时间同步是一个容易被忽视的问题,但在企业级应用环境中,不同服务器之间的时间差很可能引发应用系统问题.Windows提供的w32tm程序可以用来设置时间同步服务器,其用法如下: 1.指定外部时间源 ...

  10. libevent使用event_new和不使用的两种方法

    写两个简单的demo,对照一下各自的方法 #include <sys/types.h> #include <event2/event-config.h> #include &l ...