Nginx 虚拟主机配置

时间:2022-11-24 12:47:31

在真实的服务器环境,为了充分利用服务器资源,一台 nginx web 服务器同时会配置
N 个虚拟域名主机,即多个域名对于同样一个 80 端口。然后服务器 IP 数量很多,也可以
配置基于多个 IP 对应同一个端口。
vi 修改 nginx.conf server 段配置内容如下:

server {
    listen 80;
    server_name www.a.com;
#access_log logs/host.access.log main;
    location / {
    root html/a;
    index index.html index.htm;
}
server {
    listen 80;
    server_name www.b.com
    #access_log logs/host.access.log main;
    location / {
    root html/b;
    index index.html index.htm;
}

创建两个不同的目录 mkdir –p /usr/local/nginx/html/{a,b},然后分别在两个目录创
建两个不同的 index.html 网站页面即可。通过客户端配置 hosts 指向两个域名,然后在 IE
浏览器访问测试效果。