一.建立虚拟主机:
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//