Nginx Rewrite相关功能-域名永久与临时重定向实战篇
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.试验前的准备环境
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 ~]# ll /yinzhengjie/softwares/nginx/conf.d/
total 4
-rw-r--r-- 1 root root 278 Dec 24 11:10 node101_yinzhengjie_org.cn.conf
[root@node101.yinzhengjie.org.cn ~]#
[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;
}
}
[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 ~]#

[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/nginx/conf.d/
total 4
-rw-r--r-- 1 root root 278 Dec 24 11:10 node101_yinzhengjie_org.cn.conf
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# vim /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_com.conf
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_com.conf
server {
listen 80;
listen 443 ssl;
server_name node101.yinzhengjie.com;
location / {
root /yinzhengjie/data/web/nginx/html;
index index.html;
}
location = /favicon.ico {
root /yinzhengjie/data/web/nginx/images/jd;
}
}
[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 ~]#
4>.准备测试数据

[root@node101.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/data/web/nginx/static mkdir: created directory ‘/yinzhengjie/data/web/nginx’ mkdir: created directory ‘/yinzhengjie/data/web/nginx/static’ [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# echo "<h1>node101.yinzhengjie.org.cn<h1>" > /yinzhengjie/data/web/nginx/static/index.html [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/web/nginx/static/index.html <h1>node101.yinzhengjie.org.cn<h1> [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/data/web/nginx/images/jd mkdir: created directory ‘/yinzhengjie/data/web/nginx/images’ mkdir: created directory ‘/yinzhengjie/data/web/nginx/images/jd’ [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# wget https://www.jd.com/favicon.ico -O /yinzhengjie/data/web/nginx/images/jd/favicon.ico --2019-12-24 11:22:34-- https://www.jd.com/favicon.ico Resolving www.jd.com (www.jd.com)... 220.194.105.131, 2408:8710:20:1140:8000::3 Connecting to www.jd.com (www.jd.com)|220.194.105.131|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 25214 (25K) [image/x-icon] Saving to: ‘/yinzhengjie/data/web/nginx/images/jd/favicon.ico’ 100%[===========================================================================================================>] 25,214 --.-K/s in 0.002s 2019-12-24 11:22:34 (12.3 MB/s) - ‘/yinzhengjie/data/web/nginx/images/jd/favicon.ico’ saved [25214/25214] [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/images/jd/ total 28 -rw-r--r-- 1 root root 25214 Mar 25 2016 favicon.ico [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/data/web/nginx/html mkdir: created directory ‘/yinzhengjie/data/web/nginx/html’ [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# echo "<h1>node101.yinzhengjie.com<h1>" > /yinzhengjie/data/web/nginx/html/index.html [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/web/nginx/html/index.html <h1>node101.yinzhengjie.com<h1> [root@node101.yinzhengjie.org.cn ~]# [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 11:20 html drwxr-xr-x 3 root root 16 Dec 24 11:21 images drwxr-xr-x 2 root root 24 Dec 24 11:14 static /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/static: total 4 -rw-r--r-- 1 root root 35 Dec 24 11:14 index.html [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
5>.启动nginx服务
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:443 *:*
LISTEN 0 128 :::22 :::*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# netstat -untalp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23179/nginx: master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 23179/nginx: master
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 23179 1 0 11:35 ? 00:00:00 nginx: master process nginx
nginx 23180 23179 0 11:35 ? 00:00:00 nginx: worker process
nginx 23181 23179 0 11:35 ? 00:00:00 nginx: worker process
nginx 23182 23179 0 11:35 ? 00:00:00 nginx: worker process
nginx 23183 23179 0 11:35 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
6>.浏览器访问"http://node101.yinzhengjie.org.cn/",如下图所示。
6>.浏览器访问"http://node101.yinzhengjie.com/",如下图所示。
二.配置临时重定向
1>.实验说明
因业务需求,现将node101.yinzhengjie.org.cn的请求临时重定向到node101.yinzhengjie.org.com。
2>.编辑子配置文件
[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;
rewrite / http:/node101.yinzhengjie.com permanent;
}
location = /favicon.ico {
root /yinzhengjie/data/web/nginx/images/jd;
}
}
[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 ~]#
3>.重新加载nginx的配置文件
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 23179 1 0 11:35 ? 00:00:00 nginx: master process nginx
nginx 23180 23179 0 11:35 ? 00:00:00 nginx: worker process
nginx 23181 23179 0 11:35 ? 00:00:00 nginx: worker process
nginx 23182 23179 0 11:35 ? 00:00:00 nginx: worker process
nginx 23183 23179 0 11:35 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 23179 1 0 11:35 ? 00:00:00 nginx: master process nginx
nginx 23293 23179 0 11:39 ? 00:00:00 nginx: worker process
nginx 23294 23179 0 11:39 ? 00:00:00 nginx: worker process
nginx 23295 23179 1 11:39 ? 00:00:00 nginx: worker process
nginx 23296 23179 1 11:39 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
4>.浏览器访问"node101.yinzhengjie.org.cn",发现浏览器自动跳转了,如下图所示。
5>.浏览器再一次访问"node101.yinzhengjie.org.cn",发现浏览器自动跳转了,如下图所示,并没有使用本地缓存。
三.配置永久重定向
1>.试验说明
因业务需求,现将node101.yinzhengjie.org.cn的请求永久重定向到node101.yinzhengjie.org.com。
2>.编辑子配置文件并重新加载配置
[root@node101.yinzhengjie.org.cn ~]# vim /yinzhengjie/softwares/nginx/conf.d/node101_yinzhengjie_org.cn.conf
[root@node101.yinzhengjie.org.cn ~]#
[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;
if ( $scheme = http ){
#rewrite / http://node101.yinzhengjie.com redirect;
rewrite / http://node101.yinzhengjie.com permanent;
}
}
location = /favicon.ico {
root /yinzhengjie/data/web/nginx/images/jd;
}
}
[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 ~]#
3>.浏览器访问"node101.yinzhengjie.org.cn",发现浏览器自动跳转了,如下图所示。
4>.浏览器再一次访问"node101.yinzhengjie.org.cn",发现浏览器自动跳转了,如下图所示,竟然使用了本地缓存。
四.临时重定向和永久重定向的区别
相同点:
都可以使重定向到指定的URL。
不同点:
临时重定向不会缓存域名解析记录(A记录),但是永久重定向会缓存数据到本地。