Nginx 虚拟主机(同80端口,多域名)

目录:

  1. 修改host文件
  2. 修改nginx.conf
  3. 虚拟主机配置目录
  4. 发布目录
  5. 验证
修改host文件
  1. windows,host路径:C:\Windows\System32\drivers\etc
192.168.197.10 www.test1.com
192.168.197.10 www.test2.com
  1. linux ,host路径:/etc/hosts
修改nginx.conf

原Nginx配置文件

[root@localhost ~]# grep -v -E '#|^$'  /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

修改后的配置:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    include /usr/local/nginx/conf/vhosts/*;

}

回到目录



虚拟主机配置目录
cd /usr/local/nginx/conf/
mkdir vhosts

vhosts/test1.conf

server {
        listen       80;
        server_name  www.test1.com;
        access_log   logs/test1.access.log;
        location / {
            root   html/test/test1;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

vhosts/test2.conf

server {
        listen       80;
        server_name  www.test2.com;
	    access_log   logs/test2.access.log;
        location / {
            root   html/test/test2;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

回到目录



发布目录

创建对应域名的发布目录

mkdir -p /usr/local/nginx/html/test/{test1,test2}


[root@localhost test1]# cat index.html 
<!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 test1</h1>
...

回到目录



验证
[root@localhost test1]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost test1]# /usr/local/nginx/sbin/nginx -s reload

在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值