Nginx-rewrite

Nginx-rewrite

Rewrite application scenarios


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

Commonly used regular expressions

Insert picture description here

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
Insert picture description here
Insert picture description here
[root@client1 ~]# vim /etc/named.conf
修改配置文件
Insert picture description here
[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
Insert picture description here
[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
Insert picture description here
client
Insert picture description here

Insert picture description here
[root@client1 named]# vim /etc/nginx/conf.d/default.conf
Add command line
Insert picture description here
[root@client1 named]# vim /etc/named.rfc1912.zones'Add
command line
Insert picture description here
Insert picture description here

Method Two

1. Configure the domain name mapping first.
In the C drive/Windows/system32/drivers/etc/hosts file,
Insert picture description here
enter [root@client1 ~]# vi /etc/nginx.conf and modify the configuration file

Insert picture description here
Insert picture description here
Insert picture description here
Add the command line
Insert picture description here
[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
Insert picture description here
Insert picture description here
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 .

Insert picture description here

Redirect and add directories based on old and new domain names

Modify configuration parameters

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

Insert picture description here
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;
      }
      }
      

Insert picture description hereExperimental results

Insert picture description here

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;       
    }
    **把末尾添加的命令删掉**

Insert picture description here

Experimental results
Insert picture description here

Insert picture description here

Jump based on all php files in the directory

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

Modify configuration parameters
Insert picture description here
Experimental results
Insert picture description here

Jump based on the most common URL request

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

Modify the configuration file
Insert picture description here

Experimental results
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_50346902/article/details/110476790