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
修改配置文件
实验结果