Nginx静态资源站点——Nginx虚拟主机

功能一:搭建一个静态资源网站

1.准备好目录资源

[root@web01 ~]# mkdir /website
[root@web01 ~]# cd /website
[root@web01 website]# pwd /website
[root@web01 website]# vim /website/index.html
<meta charset=utf-8>
Josen love Shea!!!

2.修改nginx.conf

server {
listen 80;
server_name localhost;
#默认编码
charset utf-8;
access_log logs/host.access.log main;
location / {
#定义虚拟主机的资源目录,
root /website/;
#定义首页文件的名字
index index.html index.htm;
}
}

3.重启nginx

[root@web01 website]# nginx -s reload

4.浏览器访问

功能二:静态资源压缩

#nginx.conf开启gzip压缩功能,添加如下语句,针对静态资源压缩

gzip on;
gzip_http_version 1.1;
gzip_comp_level 4;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript
application/x-httpd-php image/jpeg image/gif image/png;

#重载nginx

nginx -s reload

#开启了gzip压缩后,整体的传输资源大小,以及相应速度,都大幅度提高了

功能三:基于多IP虚拟主机

1.环境准备
#添加ip别名

[root@web01 website]# ifconfig ens33:1 192.168.178.200 broadcast 192.168.178.255 netmask 255.255.255.0 up

#此时机器有两个ip,确保都可以通信

[root@web01 website]# ifconfig |grep 192
inet 192.168.178.124 netmask 255.255.255.0 broadcast 192.168.178.255
inet 192.168.178.200 netmask 255.255.255.0 broadcast 192.168.178.255
[root@web01 website]# curl 192.168.178.124
<meta charset=utf-8>
Josen love Shea!!!
[root@web01 website]# curl 192.168.178.200
<meta charset=utf-8>
Josen love Shea!!!

功能四:修改nginx.conf支持多IP虚拟主机

#第一个虚拟主机
server {
#监听的端口和ip
listen 192.168.178.124:80;
#主机域名
server_name 192.168.124.124;

charset utf-8;

access_log logs/host.access.log;
#url匹配
location / {
#HTML文件存放的目录
root /website/s1;
#默认首页文件,从左往右寻找,index.html或是index.htm文件
index index.html index.htm;
}
}
#第二个虚拟主机
server {
listen 192.168.178.200:80;
server_name 192.168.178.200;
charset utf-8;
location / {
root /website/s2;
index index.html index.htm;
}
}

3.分别准备好网站资源

[root@bogon nginx]# echo “我是来自于192.168.178.181的站点 s1.html” >
/website/s1/index.html
[root@bogon nginx]# echo “我是来自于192.168.178.200的站点 s2.html” > /website/s2/index.html

4.重新启动nginx
#当nginx.conf的改动比较小时,用nginx -s reload。当nginx.conf的改动比较大时,用nginx -s stop,在用nginx启动。

[root@web01 website]# nginx -s stop
[root@web01 website]# nginx

5.浏览器访问,基于不同ip的虚拟主机
在这里插入图片描述在这里插入图片描述

功能五:基于多域名主机

基于多IP的虚拟主机可能会造成IP地址不足的问题,如果没有特殊需求,更常用的是基于多域名的形式。

只需要你单独配置DNS服务器,将主机名对应到正确的IP地址,修改Nginx配置,可以识别到不同的主机即可,这样就可以使得多个虚拟主机用同一个IP,解决了IP不足的隐患。

1.在本地hosts文件中,添加对应的解析记录,用于测试使用

[root@web01 website]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain
localhost6 localhost6.localdomain6

127.0.0.1 www.josen.com
127.0.0.1 www.shea.com

2.修改nginx.conf支持多域名虚拟主机

#第一个虚拟主机
server {
#监听的端口和ip
listen 80;
#主机域名
server_name www.josen.com;

charset utf-8;

access_log logs/host.access.log;
#url匹配
location / {
#HTML文件存放的目录
root /website/s1;
#默认首页文件,从左往右寻找,index.html或是index.htm文件
index index.html index.htm;
}
} #第二个虚拟主机 server {
listen 80;
server_name www.shea.com;
charset utf-8;
location / {
root /website/s2;
index index.html index.htm;
}
}

3.分别准备好网站资源

[root@web01 website]# echo “我是多域名虚拟主机,来自域 名www.josen.com” > /website/s1/index.html
[root@web01 website]# echo “我是多域名虚拟主机,来自域名www.shea.com” > /website/s2/index.html

4.测试访问多域名

[root@web01 website]# curl www.josen.com 我是多域名虚拟主机,来自域名www.josen.com
[root@web01 website]# curl www.shea.com 我是多域名虚拟主机,来自域名www.shea.com

功能六:基于多端口的虚拟主机
1.修改nginx.conf支持多域名虚拟主机

#第一个虚拟主机
server {
#监听的端口和ip
listen 80;
#主机域名
server_name www.josen.com;

charset utf-8;

access_log logs/host.access.log;
#url匹配
location / {
#HTML文件存放的目录
root /website/s1;
#默认首页文件,从左往右寻找,index.html或是index.htm文件
index index.html index.htm;
}
}
#第二个虚拟主机
server {
listen 81;
server_name www.josen.com;
charset utf-8; location / {
root /website/s2;
index index.html index.htm;
}
}

2.重载nginx服务

[root@web01 website]# nginx -s reload

3.分别准备好网站资源

[root@web01 website]# echo “我是多端口虚拟主机,来自端 口80” > /website/s1/index.html
[root@web01 website]# echo “我是多端口虚拟主机,来自端 口81” > /website/s2/index.html

4.访问测试多端口

[root@web01 website]# curl 127.0.0.1:80 我是多端口虚拟主机,来自端 口80
[root@web01 website]# curl 127.0.0.1:81 我是多端口虚拟主机,来自端 口81

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值