Nginx rewrite 重寫功能和Nginx的正則表達式

1、 Rewrite 跳轉實現
服務協議功能模塊
url 資源定位路徑php

  • nginx————支持url重寫、支持if條件判斷,但不支持else
  • 跳轉————循環最多能夠執行10次,超事後nginx將返回500代碼錯誤
  • rewrite————使用nginx’全局變量或本身設置的變量,結合正則表達式和標誌位實現url重寫以及重定向
    Nginx rewrite 重寫功能和Nginx的正則表達式
    2、Rewrite 使用場景
  • 使用rewrite進行匹配跳轉
  • 使用if匹配全局變量後跳轉
  • 使用location匹配再跳轉
    1.rewrite放在 server{},if{},location{}段中
    2.對域名或參數字符串:使用if全局變量匹配、使用proxy_pass反向代理
    3、nginx正則表達式
    經常使用的正則表達式元字符
    Nginx rewrite 重寫功能和Nginx的正則表達式
    4、Rewrite 命令
    Nginx rewrite 重寫功能和Nginx的正則表達式
    location分類
    Nginx rewrite 重寫功能和Nginx的正則表達式
    正則匹配的經常使用表達式
    Nginx rewrite 重寫功能和Nginx的正則表達式
    5、location優先級
    按優先級排列:
  • = 類型
  • ^~ 類型表達式
  • 正則表達式(和)類型
  • 常規字符串匹配類型,按前綴匹配
  • 通用匹配(/),若是沒有其餘匹配,任何請求都會匹配到
    location優先級規則(從高到低排列)
    1.匹配某個具體的文件
  • location = 完整路徑
  • location ^~ 完整路徑
  • location ~* 完整路徑
  • location ~ 完整路徑
  • location 完整路徑
  • location /
    2.用目錄作匹配訪問某個文件
  • location = 目錄
  • location ^~ 目錄
  • location ~ 目錄
  • location ~* 目錄
  • location 目錄
  • location /
    6、應用場景測試
    公司舊域名 www.domain.com 因業務需求有變動,須要使用新域名 www.newdomain.com 代替,不能廢除舊域名,從舊域名跳轉到新域名,且保持其參數不變html

    [root@localhost bin]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    獲取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    警告:/var/tmp/rpm-tmp.IHyTHc: 頭V4 RSA/SHA1 Signature, 密鑰 ID 7bd9bf62: NOKEY
    準備中...                          ################################# [100%]
    正在升級/安裝...
    1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
    [root@localhost bin]# yum install nginx -y
    [root@localhost ~]# mkdir /abc
    [root@localhost ~]# mount.cifs //192.168.56.1 /mnt
    Password for root@//192.168.254.10/linuxs:  
    [root@localhost ~]# cd /abc/LNMP-C7/LNMP-C7/
    [root@localhost LNMP-C7]# ls
    Discuz_X3.4_SC_UTF8.zip
    mysql-boost-5.7.20.tar.gz
    ncurses-5.6.tar.gz
    nginx-1.12.2.tar.gz
    php-5.6.11.tar.bz2
    php-7.1.10.tar.bz2
    php-7.1.20.tar.bz2
    php-7.1.20.tar.gz
    zend-loader-php5.6-linux-x86_64_update1.tar.gz
    [root@localhost LNMP-C7]# tar -zxvf nginx-1.12.2.tar.gz -C /opt
    [root@localhost LNMP-C7]# useradd -M -s /sbin/nologin nginx
    [root@localhost LNMP-C7]# cd /opt/nginx-1.12.2/
    [root@localhost nginx-1.12.2]# ls
    auto     CHANGES.ru  configure  html     man     src
    CHANGES  conf        contrib    LICENSE  README
    [root@localhost nginx-1.12.2]# yum install gcc gcc-c++ pcre pcre-devel make zlib-devel -y
    [root@localhost nginx-1.12.2]#  ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
    [root@localhost nginx-1.12.2]# make && make install
    [root@localhost conf]# vim nginx.conf
    37         server_name  www.accp.com;
    41         access_log  logs/www.accp.com/access.log  main;
    [root@localhost conf]# yum install bind -y
    [root@localhost conf]# vim /etc/named.conf 
    13         listen-on port 53 { any; };
    21         allow-query     { any; };
    [root@localhost conf]# vim /etc/named.rfc1912.zones 
    25 zone "accp.com" IN {
    26         type master;
    27         file "accp.com.zone";
    28         allow-update { none; };
    29 };
    [root@localhost conf]# cd /var/named/
    [root@localhost named]# ls
    data     named.ca     named.localhost  slaves
    dynamic  named.empty  named.loopback
    [root@localhost named]# cp -p named.localhost accp.com.zone
    [root@localhost named]# vim accp.com.zone 
    www     IN      A       192.168.247.202
    [root@localhost named]# systemctl start named
    [root@localhost named]# systemctl stop firewalld.service 
    [root@localhost named]# setenforce 0
    [root@localhost named]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
    [root@localhost named]# cd /usr/local/nginx/
    [root@localhost nginx]# ls
    conf  html  logs  sbin
    [root@localhost nginx]# cd logs/
    [root@localhost logs]# mkdir www.accp.com
    [root@localhost logs]# ls
    error.log  www.accp.com
    [root@localhost logs]# nginx 
    [root@localhost logs]# netstat -natp | grep 80
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6

    打開win10客戶機,配置dns服務器,並使用域名訪問。
    Nginx rewrite 重寫功能和Nginx的正則表達式
    Nginx rewrite 重寫功能和Nginx的正則表達式mysql

    [root@localhost logs]# vim /usr/local/nginx/conf/nginx.conf
    
        location / {
           #域名重定向
    ‘                 if ($host = 'www.accp.com') {
    ’                       rewrite ^/(.*)$ http://www.kgc.com/$1 permanent;
    ‘               }
                 root /html;
                 index index.html index.htm;
        }
    [root@localhost logs]# vim /etc/named.rfc1912.zones 
    zone "kgc.com" IN {
        type master;
        file "kgc.com.zone";
        allow-update { none; };
    };
    [root@localhost logs]# cd /var/named/
    [root@localhost named]# ls
    accp.com.zone  dynamic   named.empty      named.loopback
    data           named.ca  named.localhost  slaves
    [root@localhost named]# cp -p accp.com.zone kgc.com.zone
    [root@localhost named]# systemctl restart named
    [root@localhost named]# killall -1 nginx

    Nginx rewrite 重寫功能和Nginx的正則表達式

    [root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
    37         server_name  bbs.accp.com;
    42         location /post {
    43                 rewrite (.+) http://www.accp.com/bbs$1 permanent;
    44         }
    [root@localhost html]# cd -
    /var/named
    [root@localhost named]# vim accp.com.zone 
    [root@localhost named]# cat accp.com.zone 
    $TTL 1D
    @   IN SOA  @ rname.invalid. (
                    0   ; serial
                    1D  ; refresh
                    1H  ; retry
                    1W  ; expire
                    3H )    ; minimum
    NS  @
    A   127.0.0.1
    bbs IN  A   192.168.247.202
    [root@localhost named]# killall -3 nginx
    [root@localhost named]# nginx
    [root@localhost named]# systemctl restart named

    Nginx rewrite 重寫功能和Nginx的正則表達式
    基於客戶端IP訪問跳轉linux

    [root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
    
    42 #設置是否合法的IP標誌
    43         set $rewrite true;
    44 #判斷是否爲合法IP,是不是容許的IP
    45         if ($remote_addr = "192.168.247.139"){
    46                 set $rewrite false;
    47         }
    48 #不被容許的IP進行判斷,打上標記
    49         if ($rewrite = true){
    50                 rewrite (.+) /maintenance.html;
    51         }
    52 #匹配標記進行跳轉站點
    53         location = /maintenance.html {
    54                 root html;
    55         }
    [root@localhost named]# cd /usr/local/nginx/html/
    [root@localhost html]# ls
    50x.html  index.html
    [root@localhost html]# vim maintenance.html
    [root@localhost html]# killall -3 nginx 
    [root@localhost html]# nginx

    Nginx rewrite 重寫功能和Nginx的正則表達式
    Nginx rewrite 重寫功能和Nginx的正則表達式
    基於參數匹配的跳轉———跳轉到首頁
    Nginx rewrite 重寫功能和Nginx的正則表達式nginx

    [root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
    37         server_name  www.accp.com;
    42         if ($request_uri ~ ^/100-(100|200)-(\d+).html$){
    43                 rewrite (.*) http://www.accp.com permanent;
    44         }
    [root@localhost named]# vim accp.com.zone 
    [root@localhost named]# cat accp.com.zone 
    $TTL 1D
    @   IN SOA  @ rname.invalid. (
                    0   ; serial
                    1D  ; refresh
                    1H  ; retry
                    1W  ; expire
                    3H )    ; minimum
    NS  @
    A   127.0.0.1
    www IN  A   192.168.247.202
    [root@localhost named]# systemctl restart named
    [root@localhost named]# killall -3 nginx
    [root@localhost named]# nginx

    Nginx rewrite 重寫功能和Nginx的正則表達式
    Nginx rewrite 重寫功能和Nginx的正則表達式
    基於目錄下全部PHP文件跳轉c++

    [root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
    42         location ~* /upload/.*\.php$ {
    43                 rewrite (.+) http://www.accp.com permanent;
    44         }
    [root@localhost named]# nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [root@localhost named]# killall -3 nginx
    [root@localhost named]# nginx

    Nginx rewrite 重寫功能和Nginx的正則表達式
    Nginx rewrite 重寫功能和Nginx的正則表達式
    基於最普通URL請求的跳轉——跳轉到首頁正則表達式

    [root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
    42         location ~* ^/abc/123.html {
    43                 rewrite (.+) http://www.accp.com permanent;
    44         }

    Nginx rewrite 重寫功能和Nginx的正則表達式
    Nginx rewrite 重寫功能和Nginx的正則表達式
    基於最普通URL請求的跳轉——跳轉到首頁sql

    [root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
    42         location ~* ^/abc/123.html {
    43                 rewrite (.+) http://www.accp.com permanent;
    44         }
    [root@localhost named]# killall -3 nginx
    [root@localhost named]# nginx
相關文章
相關標籤/搜索