Nginx 虚拟主机配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx 虚拟主机配置相关的知识,希望对你有一定的参考价值。

nginx.conf 中,一个 Server块 就是一个虚拟主机,一个虚拟主机就是一个独立的 Web站点

(1) 基于域名的虚拟主机:通过不同的域名区分不同的虚拟主机,最常用
(2) 基于端口的虚拟主机:通过不同的端口区分不同的虚拟主机
(3) 基于 IP 的虚拟主机:通过不同的 IP 区分不同的虚拟主机

技术分享
worker_processes  1;
user nobody nobody;
pid /usr/local/nginx/nginx.pid;
 
events {
    worker_connections  1024;
}
 
http {                      
    include       mime.types;                       
    default_type  application/octet-stream;         
    sendfile        on;                             
    keepalive_timeout  65;                        
    server {                                        
        listen       80;                         
        server_name  www.xxxxx.com;                   
        location / {                               
            root   html/www;                           
            index  index.html index.htm;                               
        }
    }
}     
基于域名的虚拟主机配置
技术分享
worker_processes  1;
user nobody nobody;
pid /usr/local/nginx/nginx.pid;
 
events {
    worker_connections  1024;
}
 
http {                      
    include       mime.types;                       
    default_type  application/octet-stream;         
    sendfile        on;                             
    keepalive_timeout  65;  
                      
    server {                                        
        listen       80;                         
        server_name  www.xxxxx.com;                   
        location / {                               
            root   html/www;                           
            index  index.html index.htm;                               
        }
    }

    server {                                        
        listen       80;                         
        server_name  bbs.xxxxx.com;                   
        location / {                               
            root   html/bbs;                           
            index  index.html index.htm;                               
        }
    }

    server {                                        
        listen       80;                         
        server_name  blog.xxxxx.com;                   
        location / {                               
            root   html/blog;                           
            index  index.html index.htm;                               
        }
    }
} 
基于多个域名的虚拟主机配置
技术分享
worker_processes  1;
user nobody nobody;
pid /usr/local/nginx/nginx.pid;
 
events {
    worker_connections  1024;
}
 
http {                      
    include       mime.types;                       
    default_type  application/octet-stream;         
    sendfile        on;                             
    keepalive_timeout  65;  
                      
    server {                                        
        listen       80;                         
        server_name  www.xxxxx.com;                   
        location / {                               
            root   html/www;                           
            index  index.html index.htm;                               
        }
    }

    server {                                        
        listen       81;                         
        server_name  bbs.xxxxx.com;                   
        location / {                               
            root   html/bbs;                           
            index  index.html index.htm;                               
        }
    }

    server {                                        
        listen       82;                         
        server_name  blog.xxxxx.com;                   
        location / {                               
            root   html/blog;                           
            index  index.html index.htm;                               
        }
    }
} 
基于端口的虚拟主机配置
技术分享
## 前提:要有多个网卡

worker_processes  1;
user nobody nobody;
pid /usr/local/nginx/nginx.pid;
 
events {
    worker_connections  1024;
}
 
http {                      
    include       mime.types;                       
    default_type  application/octet-stream;         
    sendfile        on;                             
    keepalive_timeout  65;  
                      
    server {                                        
        listen       192.168.1.1:80;                         
        server_name  www.xxxxx.com;                   
        location / {                               
            root   html/www;                           
            index  index.html index.htm;                               
        }
    }

    server {                                        
        listen       192.168.1.2:80;                         
        server_name  bbs.xxxxx.com;                   
        location / {                               
            root   html/bbs;                           
            index  index.html index.htm;                               
        }
    }

    server {                                        
        listen       192.168.1.3:80;                         
        server_name  blog.xxxxx.com;                   
        location / {                               
            root   html/blog;                           
            index  index.html index.htm;                               
        }
    }
} 
基于IP的虚拟主机配置

 

 

 

 

 

 

 

   

 

 

 

 

 

    

 

 

    

以上是关于Nginx 虚拟主机配置的主要内容,如果未能解决你的问题,请参考以下文章

配置nginx虚拟主机

linux学习:Nginx--常见功能配置片段与优化-06

Nginx——Nginx启动报错Job for nginx.service failed because the control process exited with error code(代码片段

Nginx配置文件详细介绍

nginx虚拟主机

nginx网站服务器