Nginx-rewrite

Nginx-rewrite

rewrite应用场景


调整用户浏览的URL,看起来规范
 为了让搜索引擎收录网站内容,让用户体验更好
 网站更换新域名后
 根据特殊的变量、目录、客户端信息进行跳转

常用正则表达式

在这里插入图片描述

基于域名跳转

方法一

[root@client1 ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@client1 ~]# yum -y install nginx
[root@client1 ~]# rpm -qc nginx
在这里插入图片描述
在这里插入图片描述
[root@client1 ~]# vim /etc/named.conf
修改配置文件
在这里插入图片描述
[root@client1 ~]# vim /etc/named.conf
[root@client1 ~]# cd /var/named/
root@client1 named]# cp -p named.localhost aa.com.zone
[root@client1 named]# vim aa.com.zone
在这里插入图片描述
[root@client1 named]# systemctl stop firewalld [root@client1 named]# setenforce 0setenforce: SELinux is disabled
[root@client1 named]# systemctl start named
[root@client1 named]# systemctl start nginx
[root@client1 named]# netstat -anpt |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3281/nginx: master
在这里插入图片描述
客户机
在这里插入图片描述

在这里插入图片描述
[root@client1 named]# vim /etc/nginx/conf.d/default.conf
添加命令行
在这里插入图片描述
[root@client1 named]# vim /etc/named.rfc1912.zones
‘添加命令行
在这里插入图片描述
在这里插入图片描述

方法二

1、先进行域名映射配置
在C盘/Windows/system32/drivers/etc/hosts文件
在这里插入图片描述
进入[root@client1 ~]# vi /etc/nginx.conf 中修改配置文件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在末尾航添加命令行
在这里插入图片描述
[root@client1 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [emerg] open() “/var/log/nginx/www.glt.com-access.log” failed (2: No such file or directory)
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
报错信息需要添加一个目录
[root@client1 ~]# mkdir /var/log/nginx
实验效果
在这里插入图片描述
在这里插入图片描述
创建新的目录

[root@client1 ~]# mkdir -p /usr/share/nginx/html
[root@client1 ~]# echo "this is a hao ren ." > /usr/share/nginx/html/index.html
[root@client1 ~]# cat /usr/share/nginx/html/index.html
this is a hao ren .

在这里插入图片描述

基于旧、新域名跳转并加目录

修改配置参数

[root@client1 post]# vi /etc/nginx.conf

在这里插入图片描述
在末行添加命令行

server {
            listen 80;
            server_name www.aa.com;
             charset utf-8;
             access_log /var/log/nginx/www.aa.com-access.log main;
               location / {
                root html;
                index index.html index.htm;
      }
      }
      

在这里插入图片描述实验结果

在这里插入图片描述

基于参数匹配的跳转

[root@client1 post]# vi /etc/nginx.conf
修改配置参数

server {
     listen       80;
    server_name  www.aa.com;
   charset utf-8;
   if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {        rewrite (.*) http://www.aa.com permanent;        }
   access_log  logs/www.aa.access.log  main;
   location / {            
   root   html;            
   index  index.html index.htm;       
    }
    **把末尾添加的命令删掉**

在这里插入图片描述

实验结果
在这里插入图片描述

在这里插入图片描述

基于目录下所有php文件跳转

[root@client1 post]# vi /etc/nginx.conf 

修改配置参数
在这里插入图片描述
实验结果
在这里插入图片描述

基于最普通url请求的跳转

[root@client1 post]# vi /etc/nginx.conf 

修改配置文件
在这里插入图片描述

实验结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值