企业级 ##Nginx网页地址重定向拓展##

一.建立虚拟主机:

1.编辑主配置文件,添加网页名

[root@server1 ~]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf

这里写图片描述

[root@server1 conf]# mkdir  /www1
[root@server1 conf]# mkdir  /www2
[root@server1 conf]# cd /www1/
[root@server1 www1]# vim index.html

这里写图片描述

[root@server1 www1]# cd /www2/
[root@server1 www2]# vim index.html

这里写图片描述

[root@server1 www2]# nginx -s reload  ##重启服务

2.【真机】添加解析测试:

[root@foundation39 Desktop]# vim /etc/hosts

这里写图片描述
这里写图片描述
这里写图片描述

二.网站加密:

[root@server1 ~]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf

这里写图片描述

[root@server1 ~]# cd /etc/pki/tls/certs/
[root@server1 certs]# make cert.pem
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:shaanxi
Locality Name (eg, city) [Default City]:xi'an
Organization Name (eg, company) [Default Company Ltd]:westos
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:server1
Email Address []:root@localhost
[root@server1 certs]# cp cert.pem /usr/local/nginx/conf/
[root@server1 certs]# 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
[root@server1 certs]# nginx -s reload

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

三.重定向:
1.输入www.westos.org 或 westos.org 都可以访问
(1)修改配置文件

[root@server1 ~]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述
(2)测试

[root@foundation39 Desktop]# curl -I www.westos.org
HTTP/1.1 200 OK                          ##访问成功
Server: nginx/
Date: Tue, 07 Aug 2018 08:55:26 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Tue, 07 Aug 2018 07:55:54 GMT
Connection: keep-alive
ETag: "5b69508a-f"
Accept-Ranges: bytes

[root@foundation39 Desktop]# curl -I westos.org
HTTP/1.1 200 OK                   ##westos.org也能访问成功
Server: nginx/
Date: Tue, 07 Aug 2018 08:55:44 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 07 Aug 2018 01:53:21 GMT
Connection: keep-alive
ETag: "5b68fb91-264"
Accept-Ranges: bytes

2.永久重定向加密(网站前面可加任意字符)
(1)修改配置文件

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述
(2)测试

[root@foundation39 Desktop]# curl -I westos.org
HTTP/1.1 301 Moved Permanently                 ##301永久重定向
Server: nginx/
Date: Tue, 07 Aug 2018 09:03:39 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://www.westos.org                 ##把www.westos.com重定向为                                                                                                     https://www.westos.org

[root@foundation39 Desktop]# curl -I www.westos.org
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:03:57 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://www.westos.org

3.访问www.westos.org/index.html重定向到https://www.westos.org/index.html
(1)修改配置文件

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述
(2)测试

[root@foundation39 Desktop]# curl -I www.westos.org/index.html         ##后面加一个路径
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:09:29 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://www.westos.org/index.html        ##后面路径不会改变,前面重定向

[root@foundation39 Desktop]# curl -I westos.org/index.html
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:09:42 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://www.westos.org/index.html

4.网站后面加任意路径,不会影响重定向结果

[root@server1 ~]# cd /www2/
[root@server1 www2]# vim index.html
[root@server1 www2]# nginx -s reload

这里写图片描述
【真机】添加解析:

[root@foundation39 Desktop]# vim /etc/hosts

这里写图片描述
这里写图片描述

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述

【测试】

[root@foundation39 Desktop]# curl bbs.westos.org
bbs.westos.org                                      ## 可以访问
[root@foundation39 Desktop]# curl  -I  www.westos.org/bbs
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:31:54 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://www.westos.org/bbs

5.访问www.westos.org/bbs跳转到bbs.westos.org

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述

测试:

[root@foundation39 Desktop]# curl  -I  www.westos.org/bbs
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:33:40 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://bbs.westos.org

6.重定向网址前端,后面的不变:

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述

[root@foundation39 Desktop]# curl  -I  www.westos.org/bbs/index.html
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:38:11 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: https://bbs.westos.orgindex.html

7.网址前面后后面都重定向:

[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload

这里写图片描述
这里写图片描述

测试:

[root@foundation39 Desktop]# curl  -I  bbs.westos.org
HTTP/1.1 301 Moved Permanently
Server: nginx/
Date: Tue, 07 Aug 2018 09:46:39 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
Location: http://www.westos.org/bbs//
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>