Nginx 笔记与总结(12)Nginx URL Rewrite 实例(ecshop)
访问项目地址:http://192.168.254.100/ecshop
某个商品的 URL:http://192.168.254.100/ecshop/goods.php?id=3
现在需要实现把以上 URL 改写成 http://192.168.254.100/ecshop/goods-3.html(ecshop 支持的简单重写模式)
此时访问 http://192.168.254.100/ecshop/goods-3.html 显示 404:
编辑 nginx 配置文件 nginx.conf:
[root@localhost nginx]# vim conf/nginx.conf
在 server 段中添加一个 location:
location /ecshop {root html; #可以在server中统一配置 rewrite "goods-(\d{1,7})\.html" /ecshop/goods.php?id=$1; #goods的id有1-7位}
注意:在进行 Rewrite 重写时,如果正则表达式中含有 {},那么整个正则表达式就要用引号 "" 包起来。
也就是说如果按照以下的写法,正则表达式不需要用引号包起来:
location /ecshop {root html; #可以在server中统一配置 rewrite goods-(\d+)\.html /ecshop/goods.php?id=$1; # $1 反向引用}
平滑重启 nginx。
此时访问 http://192.168.254.100/ecshop/goods-3.html:
重写成功。
登录后台:http://192.168.254.100/ecshop/admin/privilege.php?act=login
商店设置 -- 基本设置 -- URL 重写 -- 简单重写 -- 确定
此时再来到首页,任意点击一款商品,URL 的形式都自动变成了 http://192.168.254.100/ecshop/goods-xxx.html
但是由于在后台设置了简单重写,此时点击任意一篇文章都会出现 404:
解决方案:
编辑 nginx 配置文件 nginx.conf
[root@localhost nginx]# vim conf/nginx.conf
修改 location:
location /ecshop {root html; #可以在server中统一配置 rewrite goods-(\d+)\.html /ecshop/goods.php?id=$1; # $1反向引用 rewrite article-(\d+)\.html /ecshop/article.php?id=$1;}
另外还有其他例如栏目、分页等 按照相同的规则进行重写
例如 首页 -- 手机类型 -- GSM 手机 -- 诺基亚 的 URL (禁用 URL 重写时)为
http://192.168.254.100/ecshop/category.php?id=3&brand=1&price_min=200&price_max=1700&filter_attr=167.229.202.199&page=2&sort=good_id&order-DESC
启用简单重写后该地址变为:http://192.168.254.100/ecshop/category-3-b1-min200-max1700-attr167.229.202.199-2-goods_id-DESC.html
b 代表 brand
min 代表 price_min
max 代表 price_max
attr 代表 filter_attr
修改 location:
location /ecshop {root html; #可以在server中统一配置 rewrite goods-(\d+)\.html /ecshop/goods.php?id=$1; # $1反向引用 rewrite article-(\d+)\.html /ecshop/article.php?id=$1;rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4filter_attr=$5;rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)-(\d+)-(\w+)-(\w+)\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4filter_attr=$5&page=$6&sort=$7&order=$8;}
平滑重启 nginx。
再如:
http://192.168.254.100/ecshop/category-1-b0.html
修改 location
location /ecshop {root html; #可以在server中统一配置 rewrite goods-(\d+)\.html /ecshop/goods.php?id=$1; # $1反向引用 rewrite article-(\d+)\.html /ecshop/article.php?id=$1;rewrite category-(\d+)-b(\d+)\.html /ecshop/category.php?id=$1&brand=$2;rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4filter_attr=$5;rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)-(\d+)-(\w+)-(\w+)\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4filter_attr=$5&page=$6&sort=$7&order=$8;}
如果设置复杂重写:
即在一些 URL 中,把名称(分类名称、产品名称)也加入到了 URL 中。
修改 nginx.conf:
location /ecshop {root html; #可以在server中统一配置 rewrite goods-(\d+)\.html /ecshop/goods.php?id=$1; # $1反向引用 rewrite article-(\d+)\.html /ecshop/article.php?id=$1;rewrite category-(\d+)-b(\d+)-.*?\.html /ecshop/category.php?id=$1&brand=$2; rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4filter_attr=$5;rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)-(\d+)-(\w+)-(\w+)\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4filter_attr=$5&page=$6&sort=$7&order=$8;}
修改了第 3 条 Rewrite
注意:? 表示懒惰匹配
此时访问 http://192.168.254.100/ecshop/category-2-b0-CDMA%E6%89%8B%E6%9C%BA.html:
其他的 URL 以此类推。
注意:Rewrite 应该按照由繁到简的顺序写,否则翻页或者选择具体型号时会出现,匹配到前面的类别时就不往下进行匹配的结果:
location /ecshop {root html; #可以在server中统一配置 rewrite goods-(\d+)\.html /ecshop/goods.php?id=$1; # $1反向引用 rewrite article-(\d+)\.html /ecshop/article.php?id=$1;rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)-(\d+)-(\w+)-(\w+).*\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4filter_attr=$5&page=$6&sort=$7&order=$8;rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+).*\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4filter_attr=$5;rewrite category-(\d+)-b(\d+).*\.html /ecshop/category.php?id=$1&brand=$2;}
最后,为了只输入 http://192.168.254.100/ecshop/ 而不需要输入 index.php 便能访问网站,在 location /ecshop 段的末尾添加 index index.php:
location /ecshop {root html; #可以在server中统一配置 rewrite goods-(\d+)\.html /ecshop/goods.php?id=$1; # $1反向引用 rewrite article-(\d+)\.html /ecshop/article.php?id=$1;rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+)-(\d+)-(\w+)-(\w+).*\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4filter_attr=$5&page=$6&sort=$7&order=$8;rewrite category-(\d+)-b(\d+)-min(\d+)-max(\d+)-attr([\d\.]+).*\.html /ecshop/category.php?id=$1&brand=$2&price_min=$3&price_max=$4filter_attr=$5;rewrite category-(\d+)-b(\d+).*\.html /ecshop/category.php?id=$1&brand=$2;index index.php;}
图片不显示是因为 location ~ image 段 把图片的根目录指向到了 /var/www 目录下,注释即可:
location ~ image {# root /var/www/;# expires 1d;# index index.html;}
此时访问 http://192.168.254.100/ecshop:
Nginx 笔记与总结(12)Nginx URL Rewrite 实例(ecshop)相关推荐
- Nginx笔记总结十六:nginx优化指南
1.高层的配置 worker_processes 定义了nginx对外提供web服务时的worker进程数 worker_rlimit_nofile 更改worker进程最大打开文件数量限制,如果没有 ...
- Linux实战教学笔记37:企业级Nginx Web服务优化实战(上)
一,Nginx基本安全优化 1.1 调整参数隐藏Nginx软件版本号信息 一般来说,软件的漏洞都和版本有关,这个很像汽车的缺陷,同一批次的要有问题就都有问题,别的批次可能就都是好的.因此,我们应尽量隐 ...
- Nginx笔记系列(1)——Nignx的安装部署
2019独角兽企业重金招聘Python工程师标准>>> Nginx百科 服务器(软件)你能一口气说出几个?从当年"蹒跚学步"学java时开始用 Tomcat,到& ...
- Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)...
一.Nginx Rewrite 规则 1. Nginx rewrite规则 Rewrite规则含义就是某个URL重写成特定的URL(类似于Redirect),从某种意义上说为了美观或者对搜索引擎友好, ...
- 11-4 12 Nginx安装 默认虚拟主机 用户认证 域名重定向
2019独角兽企业重金招聘Python工程师标准>>> 12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx域名重定向 扩展 ngin ...
- [学习笔记] windows 下安装nginx和php以及添加yaf框架和redis扩展
下载nginx和php压缩包 nginx 下载网址:http://nginx.org/en/download.html php 下载网址:http://php.net/downloads.php#v7 ...
- Nginx 笔记与总结(15)nginx 实现反向代理 ( nginx + apache 动静分离)
在 nginx 中,proxy 用来实现反向代理,upstream 用来实现负载均衡. 例如有两台服务器,nginx 服务器作为代理服务器,执行 .html 文件,apache 服务器上执行 .php ...
- 【精选】Nginx模块Lua-Nginx-Module学习笔记(一)Nginx Lua API 接口详解
源码地址:https://github.com/Tinywan/Lua-Nginx-Redis 一.介绍 各种* _by_lua,* _by_lua_block和* _by_lua_file配置指令用 ...
- Nginx 笔记与总结(3)配置虚拟主机
Nginx 重启的另外一种方式,相当于 kill -HUP `cat /usr/local/nginx/logs/nginx.pid`: /usr/local/nginx/sbin/nginx -s ...
最新文章
- detach detach_ pytorch
- 用JavaScript玩转游戏物理(一)运动学模拟与粒子系统
- AtCoder AGC031D A Sequence of Permutations (群论、置换快速幂)
- CentOS7.X中使用yum安装nginx完全教程
- ORA-01502: 索引'P_ABCD.PK_WEB_BASE'或这类索引的分区处于不可用状态
- [js] 用js写一个方法检测浏览器是否支持css3的属性
- 服务器虚拟多台linux,VirtualBox环境下基于多台Ubuntu虚拟机的Hadoop分布式计算环境搭建...
- 基于区块链的思考与创新应用实践
- 【ALB学习笔记】基于事件触发方式的串行通信接口数据接收案例
- Qt与HTML/JavaScript网页端通信和调用
- html原生listview,Html中使用M$控件系列之 ListView 篇
- 峰峰值-峰值-平均值-有效值之间有什么关系?
- 最新Chrome插件开发 api 解析
- 网格计算, 云计算, 集群计算, 分布式计算, 超级计算
- Keyword Spotting (KWS) | Deep Spoken Keyword Spotting: An Overview
- a8处理器相当于骁龙几_ 联发科发布新款中端处理器,能否与高通骁龙765G一战?...
- (公式)用欧拉公式推导三角函数恒等式
- UUP, Windows 11 更新机制的未来
- ios开源框架——UITableView+FDTemplateLayoutCell优化UITableViewCell高度计算
- java中 Excel文件解析及超大Excel文件读写
热门文章
- 区块链技术用解决拜占庭将军问题_区块链是如何解决拜占庭将军问题的?
- 导入项目到IDEA报javax/xml/bind/DatatypeConverter错误?
- linux gfs文件系统,Linux环境下使用GFS文件系统
- numpy 图片填充_用numpy做图像处理(上)
- linux查看系统后台,求助,如何查看后台服务
- c语言推箱子源代码及注释,求大神 解析推箱子源代码 要详细
- linux原子方式,linux – 以原子方式移动目录
- php 的cookie设置时间,php cookie时间设置的方法
- render注册一个链接组件_vue: 单文件组件 render函数
- vue动态发布到线上_Vue 2.6 发布了