nginx多个虚拟主机
##
Nginx多个虚拟主机
想象一下,如果网站部署者在部署网站时,每台linux服务器只运行了一个小网站,那么人气低,流量小的草根站长需要承担高额的服务器租赁费,也造成了硬件资源浪费。于是就有了解决这种局面的办法:虚拟主机。虚拟主机就是将一台服务器分割成了多个“虚拟服务器”,每个站点可以使用各自的硬盘空间,由于节省资源、省钱,许多网站使用虚拟主机来部署网站。虚拟主机的概念就是在web服务里的一个独立的网站站点,这个站点对应独立的域名(IP),具有独立的程序和资源目录,可以独立的对外提供服务。这个独立的站点配置是在nginx.conf中使用server{}代码块标签来表示一个虚拟主机。Nginx支持多个server{}标签,即支持多个虚拟主机站点。
虚拟主机类型
1 | - 基于域名的虚拟主机 |
nginx可以自动识别用户请求的域名,根据不同的域名请求服务器传输不同的内容,只需要保证服务器上有一个可用的ip地址,配置好dns解析服务。
/etc/hosts是linux系统中本地dns解析的配置文件,同样可以达到域名访问效果。
修改nginx.conf1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34egrep -v '#|^$' /opt/nginx1-12/conf/nginx.conf
#配置文件内容如下
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
#虚拟主机1
server {
listen 80;
server_name www.pyyuc.cn;
location /{
root html/pyyuc;
index index.html index.htm;
}
}
#虚拟主机2
server {
listen 80;
server_name www.pythonav.cn;
location /{
root html/pythonav;
index index.html index.htm;
}
}
}
1 | [root@oldboy_python /opt/nginx1-12 14:52:12]#curl www.pythonav.cn |