Nginx实战应用--构建虚拟主机,Socket的作用
一、虚拟主机
virtual host 虚拟主机: 一个网站对应一个虚拟主机–》server
virtual server
1.虚拟主机:
英语:virtual hosting)或称 共享主机(shared web hosting),又称虚拟服务器,是一种在单一主机或主机群上,实现多网域服务的方法,可以运行多个网站或服务的技术。
(1)基于ip地址 IP-based–》一个域名对应一个ip
(2)基于域名(name-based) --》所有的域名使用同一个ip --》使用非常多的
2.目的
(1)方便管理
所有的虚拟主机都在同一部主机中,因此彼此可以共享相同的配置设置,借此节省管理的人力与成本。
(2)提高性能
相同主机内的虚拟主机可以共享彼此的程序集(Process Pool),因此可以缩短对客户端的回应时间。
(3)降低成本
虚拟主机使得单一服务器的资源可以被更有效的利用,包括存储器、存储空间或处理器资源。因为通常服务单一网址所需的资源对整部服务器来说相当小,如此可让服务器的资源被更有效的利用。
3.实现类型
实现方式主要有三种:
网址名称对应(Name-based)、
IP地址对应(IP-based)
以及Port端口号对应(Port-based)。
网址名称对应(Name-based)
网址名称对应(Name-based)是借由识别客户端所以提供的网址,决定其所对应的服务,这个方法有效的减少IP地址的占用,但缺点是必须仰赖DNS名称对应服务的支持,若名称对应服务中断,对应此名称的服务也会无法取用。
IP地址对应(IP-based)
IP地址对应(IP-based)是指在同一部服务器上,借由同一份配置设置、不同的IP来管理多个服务。
Port端口号对应(Port-based)
近似于IP地址对应,不过是在同一个IP之下,利用不同的Port端口号来区别不同的服务,藉以快速创建多个虚拟主机。例如:
192.168.0.1:80
192.168.0.1:8080
192.168.0.1:8888
4.选择虚拟主机
(1)主机的硬件设施情况
(2)虚拟主机提供商的所拥有的虚拟主机机房,是什么规模的数据中心,是否有足够机房线路的带宽,虚拟主机网站联线的速度是否同时满足他所有虚拟主机客户的流量带宽。
(3)虚拟主机上架设的网站有多少
(4)通常一个虚拟主机能够架设上百至千个网站,若网站数量越多,就是共享同台服务器的人就越多;相比较而言资源就比较吃紧。一般与机器的CPU、存储器等有关。IIS一般为10000/每服务器连接数。
二、构建2个域名的虚拟主机
www.sc.com 192.168.66.2
www.feng.com 192.168.66.2
1.修改nginx.conf
[root@localhost conf]# pwd
/usr/local/scnginx8/conf
[root@localhost conf]#vim nginx.conf
在配置文件里添加2个server块,对应2个不同的域名 --》加到http块
server {
listen 80;
server_name www.sc.com ;
location / {
root html/sc.com;
---》需要在html目录下新建sc.com,并且在里面还要新建首页index.html,内容自己定义
index index.html index.htm;
}
}
server {
listen 80;
server_name www.feng.com;
location / {
root html/feng.com;
---》需要在html目录下新建feng.com,并且在里面还要新建首页index.html,内容自己定义
index index.html index.htm;
}
}
2.新建存放网页的文件夹和首页
[root@localhost html]# pwd
/usr/local/scnginx8/html
[root@localhost html]#
[root@localhost scnginx8]# tree html/feng.com/
html/feng.com/
└── index.html --》内容是feng.com
0 directories, 1 file
[root@localhost scnginx8]# tree html/sc.com/
html/sc.com/
└── index.html --》内容是sc.com
0 directories, 1 file
[root@localhost scnginx8]#
3.启动nginx
[root@localhost scnginx8]# nginx -t
nginx: the configuration file /usr/local/scnginx8/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/scnginx8/conf/nginx.conf test is successful
[root@localhost scnginx8]# nginx -s reload
[root@localhost scnginx8]#
4.测试
4.1在客户机上需要修改hosts文件,在里面添加
www.sc.com 192.168.66.2
www.feng.com 192.168.66.2
4.2 在浏览器里访问域名
http://www.sc.com 或者http://www.feng.com 会看到不同内容
5.优缺点总结
优点: 节省服务器,省钱
缺点:一台虚拟服务器受到攻击,其他的会收到牵连。
共用cpu、内存、磁盘、带宽
如果一台服务器的访问量特别大,会导致其他的网站访问的时候异常。
三.测试:
1.验证IP
中国的ip可以访问,美国的就不能
在服务器上配置多个ip地址,有中国的,长沙的,岳阳的
[root@www scxiongxue99]# ip add add 175.8.132.15/24 dev ens33
[root@www conf]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:fc:45:20 brd ff:ff:ff:ff:ff:ff
inet 192.168.66.2/24 brd 192.168.66.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 175.8.132.15/24 scope global ens33
valid_lft forever preferred_lft forever
inet 8.8.8.8/8 scope global ens33
valid_lft forever preferred_lft forever
inet 120.227.122.104/24 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fefc:4520/64 scope link
valid_lft forever preferred_lft forever
[root@www conf]#
在客户机上也配置不同的地方的ip地址,有中国的,长沙的,岳阳的进行测试
[root@localhost ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:4d:45:69 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.190/24 brd 192.168.0.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe4d:4569/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:4d:45:73 brd ff:ff:ff:ff:ff:ff
inet 192.168.66.254/24 brd 192.168.66.255 scope global noprefixroute ens37
valid_lft forever preferred_lft forever
inet 8.8.8.9/8 scope global ens37
valid_lft forever preferred_lft forever
inet 175.8.132.16/24 scope global ens37
valid_lft forever preferred_lft forever
inet 120.227.122.105/24 scope global ens37
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe4d:4573/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]#
2.测试的效果
[root@localhost ~]# curl 175.8.132.15
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost ~]#
[root@localhost ~]# curl 120.227.122.104
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.21.1</center>
</body>
</html>
[root@localhost ~]#
[root@localhost ~]# curl 8.8.8.8
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.21.1</center>
</body>
</html>
[root@localhost ~]#
四.Socket 和 HTTPS
1.什么是Socket?
所谓套接字(Socket),就是对网络中不同主机上的应用进程之间进行双向通信的端点的抽象。
一个套接字就是网络上进程通信的一端,提供了应用层进程利用网络协议交换数据的机制。
从所处的地位来讲,套接字上联应用进程,下联网络协议栈
是应用程序通过网络协议进行通信的接口,
是应用程序与网络协议根进行交互的接口。
2.socket的作用
作用:完成两个应用程序之间的数据传输
两个网络各自通信连接中的端点
socket也是进程和进程之间通信的方式
1.文件socket -->mysql.sock
-->实现的本机里不同的进程之间的通信
2.网络socket --》ip:port
--》不同的机器上的不同的进程之间的通信
3.HTTPS
全称:Hyper Text Transfer Protocol over SecureSocket Layer
是以安全为目标的 HTTP 通道,在HTTP的基础上通过传输加密和身份认证保证了传输过程的安全性 。
HTTPS 在HTTP 的基础下加入SSL,HTTPS 的安全基础是 SSL,因此加密的详细内容就需要 SSL。
[root@www conf]# pwd
/usr/local/scxiongxue99/conf --》证书存放的位置
[root@www conf]# ls
5151775_www.sanchuangedu.cn.key fastcgi.conf.default GeoLiteCity.dat mime.types scgi_params win-utf
5151775_www.sanchuangedu.cn_nginx.zip fastcgi_params GeoLiteCountry.dat mime.types.default scgi_params.default
5151775_www.sanchuangedu.cn.pem fastcgi_params.default koi-utf nginx.conf uwsgi_params
fastcgi.conf GeoIP.dat koi-win nginx.conf.default uwsgi_params.default
[root@www conf]#
五.nginx 相关的几个问题
1.如何知道我们的网站还在正常的运行?
1.运维人员
2.用户
自研:
自己研究开发
第3方的监控软件:
zabbix,cacti、nagios、open-falcon、prometheus
2.nginx的7层代理和4层代理的区别?
速度和效率
支持的协议
7层只能给http
4层可以给很多的协议做负载均衡(ftp,MySQL,dns,http等)
根据端口来实现
3.实验操作
(1)修改每台主机的主机名,检查防火墙配置和selinux,建议关闭
[root@www ~]# hostnamectl set-hostname LB
[root@www ~]# su - root
上一次登录:四 7月 29 11:01:51 CST 2021从 192.168.0.237pts/1 上
[root@LB ~]# vim /etc/hostname
[root@LB ~]#
[root@LB ~]#
(2)将安装脚本推送到所有的机器上,执行
[root@LB scnginx9]# scp onekey_install_shediao_v10.sh 192.168.0.190:/root
The authenticity of host '192.168.0.190 (192.168.0.190)' can't be established.
ECDSA key fingerprint is SHA256:MV8g4UIa3Qtzcw1xDyu0Evjw77MVkO1XzfxZR/UNYUs.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.0.190' (ECDSA) to the list of known hosts.
root@192.168.0.190's password:
onekey_install_shediao_v10.sh 100% 1893 208.9KB/s 00:00
[root@LB scnginx9]# scp onekey_install_shediao_v10.sh 192.168.0.192:/root
The authenticity of host '192.168.0.192 (192.168.0.192)' can't be established.
ECDSA key fingerprint is SHA256:VQKXQZIH6Ozw5CDABZ4mZ7t+uzpqcxnXgI6Dc0dI55k.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.0.192' (ECDSA) to the list of known hosts.
root@192.168.0.192's password:
onekey_install_shediao_v10.sh 100% 1893 499.1KB/s 00:00
[root@LB scnginx9]# scp onekey_install_shediao_v10.sh 192.168.0.180:/root
The authenticity of host '192.168.0.180 (192.168.0.180)' can't be established.
ECDSA key fingerprint is SHA256:SWz6PpOd+dY9RBGn4IcSTt+O1QE5MdG6Yt+nKVWUF8o.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.0.180' (ECDSA) to the list of known hosts.
root@192.168.0.180's password:
onekey_install_shediao_v10.sh 100% 1893 177.5KB/s 00:00
[root@LB scnginx9]#