Nginx Rewrite相关功能-URI 重定向
Nginx Rewrite相关功能-URI 重定向
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.实验环境说明
1>.虚拟机配置
[root@node101.yinzhengjie.org.cn ~]# uname -r
3.10.0-957.el7.x86_64
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# uname -m
x86_64
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# free -h
total used free shared buff/cache available
Mem: 7.6G 309M 6.9G 8.8M 412M 7.1G
Swap: 7.9G 0B 7.9G
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# hostname -i
172.30.1.101
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# grep 172.30.1.101 /etc/hosts
172.30.1.101 node101.yinzhengjie.org.cn node101.yinzhengjie.com
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
2>.nginx的主配置文件配置
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes 4;
worker_cpu_affinity 00000001 00000010 00000100 00001000;
events {
worker_connections 100000;
use epoll;
accept_mutex on;
multi_accept on;
}
http {
include mime.types;
default_type text/html;
server_tokens off;
charset utf-8;
log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"re
sponsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}';
access_log logs/access_json.log my_access_json;
ssl_certificate /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.crt;
ssl_certificate_key /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
3>.准备测试数据
[root@node101.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/data/web/nginx/{break,last,test} mkdir: created directory ‘/yinzhengjie/data/web/nginx/break’ mkdir: created directory ‘/yinzhengjie/data/web/nginx/last’ mkdir: created directory ‘/yinzhengjie/data/web/nginx/test’ [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# echo "<h1>BREAK<h1>" > /yinzhengjie/data/web/nginx/break/index.html [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# echo "<h1>LAST<h1>" > /yinzhengjie/data/web/nginx/last/index.html [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# echo "<h1>TEST<h1>" > /yinzhengjie/data/web/nginx/test/index.html [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/ -R /yinzhengjie/data/web/nginx/: total 0 drwxr-xr-x 2 root root 24 Dec 24 12:38 break drwxr-xr-x 2 root root 24 Dec 24 11:20 html drwxr-xr-x 3 root root 16 Dec 24 11:21 images drwxr-xr-x 2 root root 24 Dec 24 12:38 last drwxr-xr-x 2 root root 24 Dec 24 11:14 static drwxr-xr-x 2 root root 24 Dec 24 12:38 test /yinzhengjie/data/web/nginx/break: total 4 -rw-r--r-- 1 root root 14 Dec 24 12:38 index.html /yinzhengjie/data/web/nginx/html: total 4 -rw-r--r-- 1 root root 32 Dec 24 11:20 index.html /yinzhengjie/data/web/nginx/images: total 0 drwxr-xr-x 2 root root 25 Dec 24 11:22 jd /yinzhengjie/data/web/nginx/images/jd: total 28 -rw-r--r-- 1 root root 25214 Mar 25 2016 favicon.ico /yinzhengjie/data/web/nginx/last: total 4 -rw-r--r-- 1 root root 13 Dec 24 12:38 index.html /yinzhengjie/data/web/nginx/static: total 4 -rw-r--r-- 1 root root 35 Dec 24 11:14 index.html /yinzhengjie/data/web/nginx/test: total 4 -rw-r--r-- 1 root root 13 Dec 24 12:38 index.html [root@node101.yinzhengjie.org.cn ~]#
二.编辑nginx的子配置文件实现URL重定向(测试写法1,都拿不到数据,只能拿到状态码)
1>.编辑子配置文件并重新加载配置文件
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf
server {
listen 80;
listen 443 ssl;
server_name node101.yinzhengjie.org.cn;
location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
}
location = /favicon.ico {
root /yinzhengjie/data/web/nginx/images/jd;
}
location /break {
rewrite ^/break/(.*)/ /test/$1 break;
root /yinzhengjie/data/web/nginx;
index index.html;
return 666;
}
location /last {
rewrite ^/last/(.*)/ /test/$1 last;
root /yinzhengjie/data/web/nginx;
index index.html;
return 777;
}
location /test {
root /yinzhengjie/data/web/nginx;
index index.html;
return 888;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
2>.使用curl命令测试返回的状态码
[root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/break/index.html
HTTP/1.1 666
Server: yinzhengjie2019
Date: Tue, 24 Dec 2019 06:34:34 GMT
Content-Length: 0
Connection: keep-alive
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/last/index.html
HTTP/1.1 777
Server: yinzhengjie2019
Date: Tue, 24 Dec 2019 06:34:40 GMT
Content-Length: 0
Connection: keep-alive
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/test/index.html
HTTP/1.1 888
Server: yinzhengjie2019
Date: Tue, 24 Dec 2019 06:34:46 GMT
Content-Length: 0
Connection: keep-alive
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/break/index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/last/index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/test/index.html
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
三.编辑nginx的子配置文件实现URL重定向(测试写法2,break拿不到状态码,但访问"/break"能拿到"/test"的数据)
1>. 编辑子配置文件并重新加载配置文件
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf
server {
listen 80;
listen 443 ssl;
server_name node101.yinzhengjie.org.cn;
location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
}
location = /favicon.ico {
root /yinzhengjie/data/web/nginx/images/jd;
}
location /break {
rewrite ^/break/(.*) /test/$1 break;
root /yinzhengjie/data/web/nginx;
index index.html;
return 666;
}
location /last {
rewrite ^/last/(.*) /test/$1 last;
root /yinzhengjie/data/web/nginx;
index index.html;
return 777;
}
location /test {
root /yinzhengjie/data/web/nginx;
index index.html;
return 888;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
2>.使用curl命令测试返回的状态码
[root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/break/index.html HTTP/1.1 200 OK Server: yinzhengjie2019 Date: Tue, 24 Dec 2019 06:20:34 GMT Content-Type: text/html; charset=utf-8 Content-Length: 13 Last-Modified: Tue, 24 Dec 2019 04:38:44 GMT Connection: keep-alive ETag: "5e019654-d" Accept-Ranges: bytes [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/last/index.html HTTP/1.1 888 Server: yinzhengjie2019 Date: Tue, 24 Dec 2019 06:20:47 GMT Content-Length: 0 Connection: keep-alive [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/test/index.html HTTP/1.1 888 Server: yinzhengjie2019 Date: Tue, 24 Dec 2019 06:21:01 GMT Content-Length: 0 Connection: keep-alive [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/break/index.html <h1>TEST<h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/last/index.html [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/test/index.html [root@node101.yinzhengjie.org.cn ~]#
四.编辑nginx的子配置文件实现URL重定向(测试写法3,拿到的都是"/test"的数据)
1>.编辑子配置文件并重新加载配置文件
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf
server {
listen 80;
listen 443 ssl;
server_name node101.yinzhengjie.org.cn;
location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
}
location = /favicon.ico {
root /yinzhengjie/data/web/nginx/images/jd;
}
location /break {
rewrite ^/break/(.*) /test/$1 break;
root /yinzhengjie/data/web/nginx;
index index.html;
}
location /last {
rewrite ^/last/(.*) /test/$1 last;
root /yinzhengjie/data/web/nginx;
index index.html;
}
location /test {
root /yinzhengjie/data/web/nginx;
index index.html;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
2>.使用curl命令测试返回的状态码
[root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/break/index.html HTTP/1.1 200 OK Server: yinzhengjie2019 Date: Tue, 24 Dec 2019 06:27:11 GMT Content-Type: text/html; charset=utf-8 Content-Length: 13 Last-Modified: Tue, 24 Dec 2019 04:38:44 GMT Connection: keep-alive ETag: "5e019654-d" Accept-Ranges: bytes [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/last/index.html HTTP/1.1 200 OK Server: yinzhengjie2019 Date: Tue, 24 Dec 2019 06:27:18 GMT Content-Type: text/html; charset=utf-8 Content-Length: 13 Last-Modified: Tue, 24 Dec 2019 04:38:44 GMT Connection: keep-alive ETag: "5e019654-d" Accept-Ranges: bytes [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/test/index.html HTTP/1.1 200 OK Server: yinzhengjie2019 Date: Tue, 24 Dec 2019 06:27:26 GMT Content-Type: text/html; charset=utf-8 Content-Length: 13 Last-Modified: Tue, 24 Dec 2019 04:38:44 GMT Connection: keep-alive ETag: "5e019654-d" Accept-Ranges: bytes [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/break/index.html <h1>TEST<h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/last/index.html <h1>TEST<h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/test/index.html <h1>TEST<h1> [root@node101.yinzhengjie.org.cn ~]#
五.编辑nginx的子配置文件实现URL重定向(测试写法4,拿到的都是各自的数据)
1>.编辑子配置文件并重新加载配置文件
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf
server {
listen 80;
listen 443 ssl;
server_name node101.yinzhengjie.org.cn;
location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
}
location = /favicon.ico {
root /yinzhengjie/data/web/nginx/images/jd;
}
location /break {
rewrite ^/break/(.*)/ /test/$1 break;
root /yinzhengjie/data/web/nginx;
index index.html;
}
location /last {
rewrite ^/last/(.*)/ /test/$1 last;
root /yinzhengjie/data/web/nginx;
index index.html;
}
location /test {
root /yinzhengjie/data/web/nginx;
index index.html;
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
2>.使用curl命令测试返回的状态码
[root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/break/index.html HTTP/1.1 200 OK Server: yinzhengjie2019 Date: Tue, 24 Dec 2019 06:31:47 GMT Content-Type: text/html; charset=utf-8 Content-Length: 14 Last-Modified: Tue, 24 Dec 2019 04:38:14 GMT Connection: keep-alive ETag: "5e019636-e" Accept-Ranges: bytes [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/last/index.html HTTP/1.1 200 OK Server: yinzhengjie2019 Date: Tue, 24 Dec 2019 06:31:54 GMT Content-Type: text/html; charset=utf-8 Content-Length: 13 Last-Modified: Tue, 24 Dec 2019 04:38:29 GMT Connection: keep-alive ETag: "5e019645-d" Accept-Ranges: bytes [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl -I http://node101.yinzhengjie.org.cn/test/index.html HTTP/1.1 200 OK Server: yinzhengjie2019 Date: Tue, 24 Dec 2019 06:31:58 GMT Content-Type: text/html; charset=utf-8 Content-Length: 13 Last-Modified: Tue, 24 Dec 2019 04:38:44 GMT Connection: keep-alive ETag: "5e019654-d" Accept-Ranges: bytes [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/break/index.html <h1>BREAK<h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/last/index.html <h1>LAST<h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# curl http://node101.yinzhengjie.org.cn/test/index.html <h1>TEST<h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/12065125.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。