对于小流量的网站来说,在nginx服务器上只运行一个网站太浪费服务器资源了,如果我们的公司有多个网站业务,在一台服务器上同时运行多个网站,不禁能充分利用起服务器的性能,还能减少公司成本。那么这个功能如何实现呢? 我们先来看一看nginx的配置文件: vim /usr/local/nginx/conf/nginx.conf
http { server { listen 81; #监听的端口,前边可添加IP。 server_name localhost; #主机名 access_log logs/access.log combined; #日志位置 location / { root html; #网页文件存放的目录 index index.html index.htm; #默认首页文件,优先级从左到右 } } } 只需要看这一部分就可以了。每一个server模块(红色部分)就相当于一台独立的主机。我们只需要在http模块中添加sever即可。
示例: mkdir /usr/local/nginx/html/www.a.com mkdir /usr/local/nginx/html/www.b.com 在两个目录中分别添加两个index.html文件内容分别为: This is A! This is B! 然后修改nginx配置文件的http部分内容为: http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65;
server { listen 81; server_name localhost; location / { root html; index index.html index.htm; } } server { listen 81; access_log logs/www.a.com.access.log combined; location / { root html/www.a.com; index index.html index.htm; } } server { listen 81; access_log logs/www.b.com.access.log combined; location / { root html/www.b.com; index index.html index.htm; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
}
} 然后重启nginx服务后访问网站: 这样,简单的虚拟主机便配置好了。
|