Nginx地址重写功能
推荐
原创
©著作权归作者所有:来自51CTO博客作者微笑的小许的原创作品,请联系作者获取转载授权,否则将追究法律责任
Nginx地址重写功能
📒博客主页: 微笑的段嘉许博客主页
🎉欢迎关注🔎点赞👍收藏⭐留言📝
📌本文由微笑的段嘉许原创!
🏆51TOP红人、阿里专家博主~
📆51CTO首发时间:🌴2022年2月日14🌴
✉️坚持和努力一定能换来诗与远方!
🙏作者水平很有限,如果发现错误,一定要及时告知作者哦!感谢感谢!
⭐本文介绍⭐
Nginx的URL重写也被称为Rewrite,主要作用是实现当客户端对特定URL访问的时候,根据客户端的访问信息实现跳转到其他URL处。URL重写在现在的网站中经常常见,比如:
1、对搜索引擎的访问进行优化,利于搜索引擎抓取页面。
2、隐藏站点的真实URL地址,使得URL资源更加简介。
3、当网站升级时,通过URL重写功能,将网站重定向到其他页面。
4、手机和电脑在访问同一站点时,反馈不同的页面,以便使得网站页面使用手机和电脑不同分辨率,使得页面更加美观。
语法:
last
| 表示完成rewrite,一般在if和server语句中,不终止重写后的url
|
break
| 完成本条规则后不在匹配其他的,一般在location中
|
redirect ==302
| 返回带有302代码的临时重定向,浏览器地址会显示跳转后的URL地址。
|
permanent ==301
| 返回301代码的永久重定向,浏览器地址栏会显示跳转后的URL地址。
|
redirect和permanent有一定区别,301会将老地址数据带到新地址上,搜索引擎会把老地址彻底废除,302重定性时,搜索引擎会查看跳转前后哪一个地址更直观而自己选择是否跳转,所以会出现网页劫持现象。
📍地址重写的作用和特点📍
作用
| 特点
|
修改用户请求内容
| 通过ngx_http_rewrite_module模块实现
|
方便用户维护网站
| 支持if判断用户输入内容匹配条件地址重写
|
| 不支持else嵌套到if中循环判断跳转
|
| 支持set定义环境变量和if结合判断地址重写
|
📖实验配置与实现:
拓扑图:
![image-20230214140729123 Nginx地址重写功能_配置文件](https://s2.51cto.com/images/blog/202302/14161700_63eb437c1c0f98442.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
推荐步骤:
- 在Cento01上安装nginx,设置网站根目录/www使用域名www.duanjiaxu.com访问,Centos02上安装DNS服务解析www.duanjiaxu.com和www.duanjiaxu.cn以及www.duanjiaxu.com.en域名
- 配置网站跳转使用if和set判断,判断用户输入域名http://www.duanjiaxu.com/cn跳转到http://www.duanjiaxu.com.cn,判断用户输入域名http://www.duanjiaxu.com/en跳转到英文网站http://www.duanjiaxu.com.en
- return当用户输入http://www.duanjiaxu.com.cn给用户提示404错误,通过return实现当用户输入http://www.duanjiaxu.com.cn跳转到指定域名www.duanjiaxu.com的网站,匹配后通过break跳出终止地址重写
- 永久重定向应用,当用户输入域名www.duanjiaxu.cn跳转到www.duanjiaxu.com网站,临时重定向应用,当用户输入域名www.duanjiaxu.com.en跳转到www.duanjiaxu.com网站,laset的应用当用户输入访问http://www.duanjiaxu.com/cn返回中文,客户端IP地址是192.168.100.30提示访问页面,不是192.168.100.30访问报错
实验步骤:
一、在Centos01上安装Nginx,设置网站根目录/www使用域名www.duanjiaxu.com访问,Centos02上安装DNS服务解析www.duanjiaxu.com和www.duanjiaxu.com.cn以及www.duanjiaxu.com.en域名
1、在Centos01上安装nginx服务
1)上传nginx程序包
[root@centos01 ~]# mount /dev/cdrom /mnt/ //挂载系统光盘
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# ls /mnt/ //查看是哦福挂载成功
CentOS_BuildTag GPL LiveOS RPM-GPG-KEY-CentOS-7
EFI images Packages RPM-GPG-KEY-CentOS-Testing-7
EULA isolinux repodata TRANS.TBL
[root@centos01 ~]# rm -rf /etc/yum.repos.d/CentOS-* //删除系统自带的yum仓库
[root@centos01 ~]# ls /etc/yum.repos.d/
local.repo
[root@centos01 ~]# cat /etc/yum.repos.d/local.repo //查看yum仓库
[local]
name=centos
baseurl=file:///mnt
enabled=1
gpgcheck=0
[root@centos01 ~]# rz //上传nginx源代码程序
z waiting to receive.**B0100000023be50
[root@centos01 ~]# ls //查看是否上传成功
anaconda-ks.cfg nginx-1.16.1.tar.gz 模板 图片 下载 桌面
initial-setup-ks.cfg 公共 视频 文档 音乐
![image-20230214145347675 Nginx地址重写功能_centos_02](https://s2.51cto.com/images/blog/202302/14161700_63eb437c26fdd35681.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
2)安装nginx依赖程序
[root@centos01 ~]# useradd -M -s /sbin/nologin nginx //创建nginx用户
[root@centos01 ~]# yum -y install pcre-devel zlib-devel //安装nginx依赖程序
![image-20230214145413493 Nginx地址重写功能_配置文件_03](https://s2.51cto.com/images/blog/202302/14161700_63eb437cdb54b15510.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
3)解压nginx程序包
[root@centos01 ~]# tar zxf ./nginx-1.16.1.tar.gz -C /usr/src/ //解压nginx程序包
4)安装nginx
[root@centos01 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module //安装模块
[root@centos01 nginx-1.16.1]# make && make install //编译安装nginx程序
[root@centos01 nginx-1.16.1]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ //优化命令
[root@centos01 nginx-1.16.1]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
5)创建网站根目录设置网站主页
[root@centos01 ~]# mkdir /www //创建网站根目录
[root@centos01 ~]# echo "www.duanjiaxu.com" > /www/index.html //设置网站主页
2、配置IP地址和DNS
1)配置IP地址
[root@centos01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32 //进入网卡配置文件
---
TYPE=Ethernet
BOOTPROTO=static
NAME=ens32
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.100.10
NETMASK=255.255.255.0
DNS1=192.168.100.20
---
[root@centos01 ~]# systemctl restart network //重新启动网卡服务
[root@centos01 ~]# cat /etc/resolv.conf //查看DNS是否配置成功
# Generated by NetworkManager
nameserver 192.168.100.20
3、修改nginx主配置文件
1)修改主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf //进入nginx主配置文件
---
2 user nginx; //nginx管理用户
36 listen 192.168.100.10:80; //监听的IP地址和端口
39 charset utf-8; //支持中文
44 root /www/; //网站根目录
2)使用win10虚拟机验证是否能够正常访问网站
[root@centos01 ~]# nginx //启动nginx服务
[root@centos01 ~]# netstat -anptu | grep nginx //监听端口
tcp 0 0 192.168.100.10:80 0.0.0.0:* LISTEN 4193/nginx: master
![image-20230214145644341 Nginx地址重写功能_地址重写_04](https://s2.51cto.com/images/blog/202302/14161700_63eb437c19d7740655.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
4、Centos02上安装DNS服务解析www.duanjiaxu.com和www.duanjiaxu.com.cn以及www.duanjiaxu.com.en域名
1)配置IP地址和DNS
[root@centos02 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32 //进入网卡配置文件
---
TYPE=Ethernet
BOOTPROTO=static
NAME=ens32
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.100.20 //IP地址
NETMASK=255.255.255.0 //子网掩码
DNS1=192.168.100.20 //DNS
----
[root@centos02 ~]# systemctl restart network //重新启动网卡服务
[root@centos02 ~]# cat /etc/resolv.conf //查看DNS是否配置成功
# Generated by NetworkManager
nameserver 192.168.100.20
![image-20230214145819290 Nginx地址重写功能_centos_05](https://s2.51cto.com/images/blog/202302/14161700_63eb437c24cb664508.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
2)安装DNS服务
[root@centos02 ~]# mount /dev/cdrom /mnt/ //挂载系统光盘
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# ls /mnt/ //查看系统光盘是否挂载成功
CentOS_BuildTag GPL LiveOS RPM-GPG-KEY-CentOS-7
EFI images Packages RPM-GPG-KEY-CentOS-Testing-7
EULA isolinux repodata TRANS.TBL
3)安装DNS程序包
[root@centos02 ~]# rpm -ivh /mnt/Packages/bind-9.9.4-50.el7.x86_64.rpm
警告:/mnt/Packages/bind-9.9.4-50.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:bind-32:9.9.4-50.el7 ################################# [100%]
[root@centos02 ~]# rpm -ivh /mnt/Packages/bind-chroot-9.9.4-50.el7.x86_64.rpm
警告:/mnt/Packages/bind-chroot-9.9.4-50.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:bind-chroot-32:9.9.4-50.el7 ################################# [100%]
4)修改DNS主配置文件
[root@centos02 ~]# echo "" > /etc/named.conf //清空主配置文件
[root@centos02 ~]# vim /etc/named.conf //进入主配置文件
---
options {
listen-on port 53 { any; };
};
zone "duanjiaxu.com" IN {
type master;
file "var/named/duanjiaxu.com.zone";
};
zone "duanjiaxu.com.cn" IN {
type master;
file "var/named/duanjiaxu.com.cn.zone";
};
zone "duanjiaxu.com.en" IN {
type master;
file "var/named/duanjiaxu.com.en.zone";
};
[root@centos02 ~]# named-checkconf /etc/named.conf //检查主配置文件是否修改正确
![image-20230214145935265 Nginx地址重写功能_nginx_06](https://s2.51cto.com/images/blog/202302/14161700_63eb437c50a1a31836.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
4)修改第一个DNS区域配置文件
[root@centos02 ~]# vim /var/named/duanjiaxu.com.zone //进入第一个区域配置文件
---
$TTL 86400
@ SOA duanjiaxu.com. root.duanjiaxu.com. (
2023021310
1H
15M
1W
1D
)
@ NS centos02.duanjiaxu.com.
centos02 A 192.168.100.20
www A 192.168.100.10
---
[root@centos02 ~]# named-checkzone duanjiaxu.com /var/named/duanjiaxu.com.zone //查看区域配置文件语法是否有误
zone duanjiaxu.com/IN: loaded serial 2023021310
OK
5)修改第二个DNS区域配置文件
[root@centos02 ~]# cp /var/named/duanjiaxu.com.zone /var/named/duanjiaxu.com.cn.zone //复制第一个区域配置文件
[root@centos02 ~]# vim /var/named/duanjiaxu.com.cn.zone //进入第二个区域配置文件
---
:%s/duanjiaxu.com/duanjiaxu.com.cn/g //把duanjiaxu.com替换为duanjiaxu.com.cn
---
$TTL 86400
@ SOA duanjiaxu.com.cn. root.duanjiaxu.com.cn. (
2023021310
1H
15M
1W
1D
)
@ NS centos02.duanjiaxu.com.cn.
centos02 A 192.168.100.20
www A 192.168.100.10
---
[root@centos02 ~]# named-checkzone duanjiaxu.com.cn /var/named/duanjiaxu.com.cn.zone //检查配置文件修改是否有误
zone duanjiaxu.com.cn/IN: loaded serial 2023021310
OK
![image-20230214150027629 Nginx地址重写功能_centos_07](https://s2.51cto.com/images/blog/202302/14161701_63eb437d1e1fa58711.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
6)修改第三个DNS区域配置文件
[root@centos02 ~]# cp /var/named/duanjiaxu.com.zone /var/named/duanjiaxu.com.en.zone //复制第一个区域配置文件
[root@centos02 ~]# vim /var/named/duanjiaxu.com.en.zone //进入第三个区域配置文件
---
:%s/duanjiaxu.com/duanjiaxu.con.en/g //把duanjiaxu.com替换为duanjiaxu.com.en
---
$TTL 86400
@ SOA duanjiaxu.con.en. root.duanjiaxu.con.en. (
2023021310
1H
15M
1W
1D
)
@ NS centos02.duanjiaxu.con.en.
centos02 A 192.168.100.20
www A 192.168.100.10
---
[root@centos02 ~]# named-checkzone duanjiaxu.com.en /var/named/duanjiaxu.com.en.zone
zone duanjiaxu.com.en/IN: loaded serial 2023021310
OK
![image-20230214150053695 Nginx地址重写功能_nginx_08](https://s2.51cto.com/images/blog/202302/14161700_63eb437c60c6059704.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
7)重新启动DNS服务
[root@centos02 ~]# systemctl restart named //重新启动DNS服务
[root@centos02 ~]# netstat -anptu | grep named //监听网卡
tcp 0 0 192.168.100.20:53 0.0.0.0:* LISTEN 41066/named
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 41066/named
tcp 0 0 127.0.0.1:953 0.0.0.0:* LISTEN 41066/named
tcp6 0 0 ::1:953 :::* LISTEN 41066/named
udp 0 0 192.168.122.1:53 0.0.0.0:* 41066/named
udp 0 0 192.168.100.20:53 0.0.0.0:* 41066/named
udp 0 0 127.0.0.1:53 0.0.0.0:* 41066/named
8)客户端测试DNS服务器
![image-20230214150136435 Nginx地址重写功能_centos_09](https://s2.51cto.com/images/blog/202302/14161700_63eb437c57b2659755.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
9)使用win10虚拟机验证第一个nginx网站是否能够正常访问
![image-20230214150213105 Nginx地址重写功能_centos_10](https://s2.51cto.com/images/blog/202302/14161701_63eb437d11f3d88498.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
二、配置网站跳转使用if和set判断,判断用户输入域名http://www.duanjiaxu.com/cn跳转到http://www.duanjiaxu.com.cn,判断用户输入域名http://www.duanjiaxu.com/en跳到英文网站http://www.duanjiaxu.com.en
1、创建中文网站和英文网站设置主页
1)创建中文网站根目录
[root@centos01 ~]# mkdir /www/cn //创建中文网站根目录
[root@centos01 ~]# echo "cn" > /www/cn/index.html //设置网站主页
2)创建英文网站根目录
[root@centos01 ~]# mkdir /www/en //创建英文网站根目录
[root@centos01 ~]# echo "en" > /www/en/index.html //设置网站主页
3)列出网站根目录数据
[root@centos01 ~]# ls -R /www/ //循环列出网站根目录
/www/:
cn en index.html
/www/cn:
index.html
/www/en:
index.html
2、修改nginx主配置文件当用户输入cn跳转中文网站输入en跳转到英文网站
1)修改nginx主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
---
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com;
charset utf-8;
access_log logs/www.duanjiaxu.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com.cn;
charset utf-8;
access_log logs/www.duanjiaxu.com.cn.access.log;
location / {
set $language 'cn';
rewrite ^/$ http://www.duanjiaxu.com/$language;
}
}
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com.en;
charset utf-8;
access_log logs/www.duanjiaxu.com.access.log;
location / {
set $language 'en';
rewrite ^/$ http://www.duanjiaxu.com/$language;
}
}
}
---
[root@centos01 ~]# nginx -t //检查主配置文件修改是否有误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
2)重新启动nginx服务
[root@centos01 ~]# killall nginx //停止nginx服务
nginx: no process found
[root@centos01 ~]# nginx //启动nginx服务
[root@centos01 ~]# netstat -anptu | grep nginx //监听网卡
tcp 0 0 192.168.100.10:80 0.0.0.0:* LISTEN 44007/nginx: master
3、客户端验证访问
1)客户端访问中文网站
![image-20230214150718426 Nginx地址重写功能_centos_11](https://s2.51cto.com/images/blog/202302/14161700_63eb437c1866c69142.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
2)客户端访问英文网站
![image-20230214150730454 Nginx地址重写功能_centos_12](https://s2.51cto.com/images/blog/202302/14161700_63eb437c13e1e10493.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
4、优化网站跳转
1)修改nginx主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
---
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com;
charset utf-8;
access_log logs/www.duanjiaxu.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com.cn www.duanjiaxu.com.en;
charset utf-8;
#access_log logs/www.duanjiaxu.com.cn.access.log;
location / {
if ($http_host ~ (cn)$) {
set $language 'cn';
rewrite ^/$ http://www.duanjiaxu.com/$language;
}
if ($http_host ~ (en)$) {
set $language 'en';
rewrite ^/$ http://www.duanjiaxu.com/$language;
}
}
}
}
2)重新启动nginx服务
[root@centos01 ~]# killall nginx //停止nginx服务
nginx: no process found
[root@centos01 ~]# nginx //启动nginx服务
[root@centos01 ~]# netstat -anptu | grep nginx //监听网卡
tcp 0 0 192.168.100.10:80 0.0.0.0:* LISTEN 44357/nginx: master
3)访问中文网站
![image-20230214150819450 Nginx地址重写功能_centos_13](https://s2.51cto.com/images/blog/202302/14161700_63eb437c1e71b60853.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
4)访问英文网站
![image-20230214150836450 Nginx地址重写功能_nginx_14](https://s2.51cto.com/images/blog/202302/14161700_63eb437cdcd0d7247.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
三、return当用户输入http://www.duanjiaxu.com.cn给用户提示404错误,通过return实现当用户输入http://www.duanjiaxu.com.cn跳转到指定域名www.duanjiaxu.com的网站,匹配后通过break跳出终止地址重写
1、通过return当用户输入http://www.duanjiaxu.com.cn给用户提示404错误
1)修改nginx主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
---
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com;
charset utf-8;
access_log logs/www.duanjiaxu.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com.cn;
charset utf-8;
access_log logs/www.duanjiaxu.com.access.log;
location / {
if ($http_host = "www.duanjiaxu.com.cn") {
return 404;
}
}
}
}
[root@centos01 ~]# killall nginx //停止nginx服务
nginx: no process found
[root@centos01 ~]# nginx //启动nginx服务
[root@centos01 ~]# netstat -anptu | grep nginx //监听nginx网卡服务
tcp 0 0 192.168.100.10:80 0.0.0.0:* LISTEN 46316/nginx: master
![image-20230214151205636 Nginx地址重写功能_配置文件_15](https://s2.51cto.com/images/blog/202302/14161700_63eb437c1f40052798.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
2、通过return实现当用户输入http://www.duanjiaxu.com.en跳转到指定域名www.duanjiaxu.com的网站
1)修改nginx主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
---
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com;
charset utf-8;
access_log logs/www.duanjiaxu.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com.en;
charset utf-8;
access_log logs/www.duanjiaxu.com.access.log;
location / {
if ($http_host = "www.duanjiaxu.com.en") {
rewrite ^/$ http://www.duanjiaxu.com;
}
}
}
}
[root@centos01 ~]# killall nginx
nginx: no process found
[root@centos01 ~]# nginx
[root@centos01 ~]# netstat -anptu | grep nginx
tcp 0 0 192.168.100.10:80 0.0.0.0:* LISTEN 46435/nginx: master
2)输入www.duanjiaxu.com.en跳转到www.duanjiaxu.com
![image-20230214151342288 Nginx地址重写功能_配置文件_16](https://s2.51cto.com/images/blog/202302/14161700_63eb437c3433b23469.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
3、当用户输入www.duanjiaxu.com.en匹配后通过break跳出地址重写
1)修改nginx主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
---
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com;
charset utf-8;
access_log logs/www.duanjiaxu.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com.en;
charset utf-8;
access_log logs/www.duanjiaxu.com.access.log;
location / {
if ($http_host = "www.duanjiaxu.com.en") {
break;
rewrite ^/$ http://www.duanjiaxu.com;
}
}
}
}
[root@centos01 ~]# killall nginx
nginx: no process found
[root@centos01 ~]# nginx
[root@centos01 ~]# netstat -anptu | grep nginx
tcp 0 0 192.168.100.10:80 0.0.0.0:* LISTEN 46507/nginx: master
2)验证
![image-20230214151515981 Nginx地址重写功能_配置文件_17](https://s2.51cto.com/images/blog/202302/14161700_63eb437c2169767251.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
四、永久重定向应用,当用户输入域名www.duanjiaxu.com.cn跳转到www.danjiaxu.con网站,临时重定向应用,当用户输入域名www.duanjiaxu.com.en跳转到www.duanjiaxu.com网站,laset的应用当用户输入访问http://www.duanjiaxu.com/cn返回中文,客户端IP地址是192.168.100.30提示访问页面,不是192.168.100.30访问报错
1、永久重定向应用,当用户输入域名www.duanjiaxu.com.cn或者www.duanjiaxu.com.en跳转到www.duanjiaxu.com网站
1)修改nginx主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
---
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com;
charset utf-8;
access_log logs/www.duanjiaxu.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com.cn www.duanjiaxu.com.en;
charset utf-8;
#access_log logs/www.duanjiaxu.com.access.log;
location / {
rewrite ^/$ http://www.duanjiaxu.com permanent; //永久重定向到www.duanjiaxu.com
}
}
}
---
[root@centos01 ~]# killall nginx //停止nginx服务
nginx: no process found
[root@centos01 ~]# nginx //启动nginx服务
[root@centos01 ~]# netstat -anptu | grep nginx //监听nginx网卡
tcp 0 0 192.168.100.10:80 0.0.0.0:* LISTEN 44963/nginx: master
2)在win10虚拟机使用抓包软件访问验证(301永久重定向)
![image-20230214151946975 Nginx地址重写功能_nginx_18](https://s2.51cto.com/images/blog/202302/14161700_63eb437c29a5455966.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
![image-20230214151949483 Nginx地址重写功能_地址重写_19](https://s2.51cto.com/images/blog/202302/14161700_63eb437c2169990605.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
2、临时重定向应用,当用户输入域名www.duanjiaxu.com.cn或者www.duanjiaxu.com.en跳转到www.duanjiaxu.com网站
1)修改nginx主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
---
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com;
charset utf-8;
access_log logs/www.duanjiaxu.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com.cn www.duanjiaxu.com.en;
charset utf-8;
#access_log logs/www.duanjiaxu.com.access.log;
location / {
rewrite ^/$ http://www.duanjiaxu.com redirect; //临时重定向到www.duanjiaxu.com
}
}
}
---
[root@centos01 ~]# killall nginx //停止nginx服务
nginx: no process found
[root@centos01 ~]# nginx //启动nginx服务
[root@centos01 ~]# netstat -anptu | grep nginx //监听nginx网卡
tcp 0 0 192.168.100.10:80 0.0.0.0:* LISTEN 45273/nginx: master
2)在win10虚拟机使用抓包软件访问验证(302临时重定向)
![image-20230214152143092 Nginx地址重写功能_nginx_20](https://s2.51cto.com/images/blog/202302/14161700_63eb437c2f37b87039.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
3、laset的应用当用户输入访问www.duanjiaxu.com.cn跳转到www.duanjiaxu.com,当用户输入www.duanjiaxu.com.cn/en跳转到英文网站
1)修改nginx主配置文件
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
---
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com;
charset utf-8;
access_log logs/www.duanjiaxu.com.access.log;
location / {
root /www;
index index.html index.htm;
}
}
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com.cn;
charset utf-8;
#access_log logs/www.duanjiaxu.com.access.log;
location / {
if ($http_host ~* (cn)$) {
rewrite ^/$ http://www.duanjiaxu.com last;
}
location /en {
root /www/en/;
index index.html index.html;
}
}
}
}
---
[root@centos01 ~]# killall nginx //停止nginx服务
nginx: no process found
[root@centos01 ~]# nginx //启动nginx服务
[root@centos01 ~]# netstat -anptu | grep nginx //监听nginx网卡
tcp 0 0 192.168.100.10:80 0.0.0.0:* LISTEN 45524/nginx: master
2)客户端访问验证客户端输入域名是www.duanjiaxu.com.cn跳转到www.duanjiaxu.com域名
![image-20230214152709963 Nginx地址重写功能_nginx_21](https://s2.51cto.com/images/blog/202302/14161700_63eb437c6729345257.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
3)输入www.duanjiaxu.com.cn/en跳转到英文网站
![image-20230214152759452 Nginx地址重写功能_nginx_22](https://s2.51cto.com/images/blog/202302/14161700_63eb437c97f5e22674.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
4、维护网页跳转,当客户端测试机192.168.100.30显示页面,非192.168.100.30主机访问报错误
1)新建错误页面
[root@centos01 ~]# mkdir /www/error //创建错误页面根目录
[root@centos01 ~]# echo "error" > /www/error/error.html //设置错误页面主页
[root@centos01 ~]# ls -R /www/ //查看
/www/:
cn en error index.html
/www/cn:
index.html
/www/en:
index.html
/www/error:
error.html
2)修改nginx主配置文件
user nginx;
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10:80;
server_name www.duanjiaxu.com;
charset utf-8;
access_log logs/www.duanjiaxu.com.access.log;
location / {
root /www;
index index.html index.htm;
set $rewrite true;
if ($remote_addr = "192.168.100.30") {
set $rewrite fales;
}
if ($rewrite = true) {
rewrite (.+) /error.html;
}
location =/error.html {
root /www/error/;
}
}
}
}
---
[root@centos01 ~]# killall nginx //停止nginx服务
nginx: no process found
[root@centos01 ~]# nginx //启动nginx服务
[root@centos01 ~]# netstat -anptu | grep nginx //监听网卡
tcp 0 0 192.168.100.10:80 0.0.0.0:* LISTEN 46134/nginx: master
2)非192.168.100.30主机访问提示错误
![image-20230214152950456 Nginx地址重写功能_地址重写_23](https://s2.51cto.com/images/blog/202302/14161700_63eb437c2579438493.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
3)是192.168.100.30主机访问显示主页
![image-20230214153015862 Nginx地址重写功能_地址重写_24](https://s2.51cto.com/images/blog/202302/14161700_63eb437c49ee433819.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)
🙏作者水平很有限,如果发现错误,一定要及时告知作者哦!感谢感谢!
![Nginx地址重写功能_地址重写_25](https://s2.51cto.com/images/blog/202302/14161700_63eb437c485ce67893.jpg?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184)