Nginx-rewrite
Nginx-rewrite
Rewrite application scenarios
调整用户浏览的URL,看起来规范
为了让搜索引擎收录网站内容,让用户体验更好
网站更换新域名后
根据特殊的变量、目录、客户端信息进行跳转
Commonly used regular expressions
Redirect based on domain name
method one
[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
client
[root@client1 named]# vim /etc/nginx/conf.d/default.conf
Add command line
[root@client1 named]# vim /etc/named.rfc1912.zones'Add
command line
Method Two
1. Configure the domain name mapping first.
In the C drive/Windows/system32/drivers/etc/hosts file,
enter [root@client1 ~]# vi /etc/nginx.conf and modify the configuration file
Add the command line
[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
error message needs to add a directory
[root@client1 ~]# mkdir /var/log/nginx
experimental effect
Create a new directory
[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 .
Redirect and add directories based on old and new domain names
Modify configuration parameters
[root@client1 post]# vi /etc/nginx.conf
Add a command line at the bottom line
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;
}
}
Experimental results
Jump based on parameter matching
[root@client1 post]# vi /etc/nginx.conf
modify configuration parameters
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;
}
**把末尾添加的命令删掉**
Experimental results
Jump based on all php files in the directory
[root@client1 post]# vi /etc/nginx.conf
Modify configuration parameters
Experimental results
Jump based on the most common URL request
[root@client1 post]# vi /etc/nginx.conf
Modify the configuration file
Experimental results