天天看點

nginx重寫與重定向

nginx 重寫與重定向

應用:使用重寫或者重定向,避免網站結構或者域名修改後,網站中原有連結失效

************************

rewrite

指令格式:

rewrite old_url new_url [flag]
           

old_url:原有通路路徑,使用正規表達式進行比對

new_url:新的通路路徑

flag:參數可選,可設定的值

last:重寫,位址欄中url位址不變,比對後重新發起請求(服務端跳轉)

break:重寫,位址欄中url位址不變,比對後直接輸出比對頁面

permanent:重定向,位址欄中url位址改變,重新發起請求(用戶端跳轉),傳回響應狀态碼301(永久重定向)

redirect:重定向,位址欄中url位址改變,重新發起請求(用戶端跳轉),傳回響應狀态碼302(臨時重定向)

#輸出頁面重寫
rewrite ^/last/(.*)   /test/$1 last;                 #服務端跳轉到新的頁面
rewrite ^/break/(.*)  /test/$1 break;                #直接輸出重寫頁面
 
 
#請求重定向
rewrite  ^/redirect/(.*)    /test/$1 redirect;       #臨時重定向
rewrite  ^/permanent/(.*)  /test/$1 permanent;       #永久重定向
           

************************

示例:重寫

nginx.conf

user  nginx;
worker_processes  1;
 
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}
           

default.conf

server {
    listen       80;
    server_name  localhost;
 
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
 
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
 
 
    #location 1
    location /break {
        root /usr/share/nginx/html2;
 
        if ( !-e $request_filename ){
             rewrite ^/break/(.*)  /default/default  break;
        }
    }
 
    #location 2
    location /break2 {
        root /usr/share/nginx/html2;
 
        if ( !-e $request_filename ){
             rewrite ^/break2/(.*)  /default/default  break;
             echo "break";
        }
    }
 
 
    #location 3
    location /last {
        if ( !-e $request_filename ){
             rewrite ^/last/(.*)  /test/$1  last;
             echo "last";
        }
    }
 
 
    #location 4
    location /test {
        echo "test";
    }
 
 
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
}
           

建立容器

docker run -it -d -p 8080:80 \
-v /usr/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf \
-v /usr/nginx/html:/usr/share/nginx/html2 \
--name nginx lihu12344/nginx
           

********************

使用測試

localhost:8080/break/default

[[email protected] ~]# curl localhost:8080/break/default
default html
           

比對location 1,随後重寫頁面(輸出路徑 /default/default 内容)

localhost:8080/break2/default

[[email protected] ~]# curl localhost:8080/break2/default
break
           

比對location 2,随後重寫頁面(輸出路徑 /default/default 内容),echo "break"; 覆寫重寫後的頁面

localhost:8080/last/test

[[email protected] ~]# curl localhost:8080/last/default
test
           

比對location 3,重寫路徑 /test/default,随後比對location 4,輸出test

************************

示例:重定向

nginx.conf

user  nginx;
worker_processes  1;
 
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    keepalive_timeout  65;
 
    #gzip  on;
 
    include /etc/nginx/conf.d/*.conf;
}
           

default.conf

server {
    listen       80;
    server_name  localhost;
 
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
 
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
 
    location /redirect {
        if ( !-e $request_filename ) {
             rewrite ^/redirect/(.*)  /test/$1  redirect;
        }
    }
 
    location /permanent {
        if ( !-e $request_filename ) {
             rewrite ^/permanent/(.*)  /test/$1 permanent;
        }
    }
 
    location /test {
        echo "test";
    }
 
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
}
           

建立容器

docker run -it -d -p 8080:80 \
-v /usr/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf \
--name nginx lihu12344/nginx
           

********************

使用測試

curl -I localhost:8080/permanent/test

[[email protected] conf.d]# curl -I localhost:8080/permanent/test
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.2
Date: Fri, 16 Oct 2020 14:28:29 GMT
Content-Type: text/html
Content-Length: 185
Location: http://localhost/test/test
Connection: keep-alive
           

請求重定向,傳回狀态碼:301,重定向url:http://localhost/test/test

curl -I localhost:8080/redirect/test

[[email protected] conf.d]# curl -I localhost:8080/redirect/test
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.14.2
Date: Fri, 16 Oct 2020 14:28:32 GMT
Content-Type: text/html
Content-Length: 161
Location: http://localhost/test/test
Connection: keep-alive
           

請求重定向,傳回狀态碼:302,重定向url:http://localhost:8080/test/test

繼續閱讀