nginx 重写与重定向

本文详细介绍了如何在Nginx中使用rewrite指令进行URL重写和重定向,以应对网站结构或域名变更。示例包括了内部重写(last和break标志)和外部重定向(redirect和permanent标志)。通过具体配置和测试用例,展示了不同重写和重定向规则的效果,帮助理解Nginx的URL管理策略。
摘要由CSDN通过智能技术生成

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

[root@centos ~]# curl localhost:8080/break/default
default html

匹配location 1,随后重写页面(输出路径 /default/default 内容)

 

localhost:8080/break2/default

[root@centos ~]# curl localhost:8080/break2/default
break

匹配location 2,随后重写页面(输出路径 /default/default 内容),echo "break"; 覆盖重写后的页面

 

localhost:8080/last/test

[root@centos ~]# 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

[root@centos 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

[root@centos 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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值