首页 > 代码库 > Nginx 虚拟主机配置

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 虚拟主机配置