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