Nginx虚拟主机配置--配置Nginx的主配置文件

单台Nginx WEB服务器同时会配置N个网站,也可称之为配置N个虚拟域名的主机,即多个域名对应同一个80端 口。 每个虚拟主机可以是一个独立网站、可以具有独立域名,同一台物理机上面的虚拟主机相互之间是独立。

虚拟主机的类型

基于IP的虚拟主机

可以在一块物理网卡上绑定多个IP地址。这样就能够在使用单一网卡的同一个服务器上运行多个基于IP的虚拟主 机。设置IP别名也非常容易,只须配置系统上的网络接口,让它监听额外的IP地址。

基于端口的虚拟主机

基于端口的虚拟主机配置,使用端口来区分,浏览器使用域名或ip地址:端口号访问。

基于域名的虚拟主机

基于域名的虚拟主机是最常见的一种虚拟主机。只需配置你的DNS服务器,将每个主机名映射到正确的IP地址,然 后配置Nginx服务器,令其识别不同的主机名就可以了。这种虚拟主机技术,使很多虚拟主机可以共享同一个IP地 址,有效解决了IP地址不足的问题。所以,如果没有特殊要求使你必须用一个基于IP的虚拟主机,最好还是使用基 于域名的虚拟主机。

开始配置实验!

1.确认nginx已经正常运行

[root@iZ2zecxmcsda0x83lfxjajZ /]# ps -ef|grep nginx
root      5704     1  0 Jul10 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    7290  5704  0 Jul11 ?        00:00:00 nginx: worker process
nobody    7291  5704  0 Jul11 ?        00:00:00 nginx: worker process
nobody    7292  5704  0 Jul11 ?        00:00:00 nginx: worker process
nobody    7293  5704  0 Jul11 ?        00:00:00 nginx: worker process
root      8608  8516  0 14:14 pts/0    00:00:00 grep --color=auto nginx

2.重新写入nginx.conf

#user  nobody;
worker_processes  4;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


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"';
    sendfile on;
    keepalive_timeout 65;
    server{
        listen 80;
        server_name www.test1.com;
        access_log logs/test1.com.log main;
    
        location / {
            root html/test1;
            index index.html index.html;
            }
        }
    server{
        listen 80;
        server_name www.test2.com;
        access_log logs/test2.com.log main;
        
        location / {
            root html/test2;
            index index.html index.htm;
        }
    }
}

3.保存后,检查配置文件是否正确

[root@iZ2zecxmcsda0x83lfxjajZ conf]# /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

4.重新读取配置文件

[root@iZ2zecxmcsda0x83lfxjajZ conf]# /usr/local/nginx/sbin/nginx -s reload

5.在html目录下新建两个网站目录test1和test2,并写入两个index.html

[root@iZ2zecxmcsda0x83lfxjajZ conf]# mkdir ../html/test1 ../html/test2 && echo "this is test1." > ../html/test1/index.html && echo "this is test2." > ../html/test2/index.html

6.修改hosts文件后,分别访问www.test1.com和www.test2.com

 

posted @ 2019-07-12 14:28  Quail  阅读(962)  评论(1编辑  收藏  举报