nginx 虚拟主机include的使用

如果我们的域名都配置到nginx.conf.,这样文件会很乱, 影响管理和阅读.所以nginx的include支持文件拆分出来,分成不同的配置文件,以下是虚拟主机的配置文件拆分
1. 基于域名虚拟主机

没有配置include前

wroker_processes 1;#进程数
evevts {
         #事件区块
    worker_connections 1024;#每个work进程支持最大并发
}
http {
                       #http区域开始
    include mime.types; #nginx支持媒体类型库文件
    default_type application/octet-stream;#默认的媒体类型
    sendfile        on;#开启高效传输模式
    keepalive_timeout   65;#连接超时
    server {
      #第一个server区域,单独的虚拟主机站点
        liseten         80;#提供的服务端口
        server_nanme www.watson.com;#提供服务的域名主机名
        location / {
         # 第一个location区域开始
            root  /web/www; #站点目录
            index index.html index.htm; #默认首页文件
        }
        }
    server {
     
        liseten         80;
        server_nanme abc.watson.com;
        location / {
     
            root  /web/abc;
            index index.html index.htm;
        }
        }
      server {
     
        liseten         80;
        server_nanme ten.watson.com;
        location / {
     
            root  /web/ten;
            index index.html index.htm;
        }
        }
}

2. 创建站点目录写入网页内容

[root@www nginx]# mkdir -p /web/{www,abc,ten}
[root@www nginx]# echo abc.watson.com > /web/abc/index.html 
[root@www nginx]# echo www.watson.com > /web/www/index.html
[root@www nginx]# echo ten.watson.com > /web/ten/index.html

3. nginx的include配置
在http块中添加include vhost/*.conf

#user  nobody;
worker_processes  1;

#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;
    include  vhost/*.conf; 

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
     #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    server {
     
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
     
            root   html;
            index  index.html index.htm;
        }




创建存放虚拟主机的目录分别写入每台虚拟主机的配置

[root@www nginx]# mkdir conf/vhost
[root@www nginx]# cat conf/vhost/www.conf 
server {
        listen 80;
        server_name www.watson.com;
        location / {
	    root /web/www;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            # proxy_pass http://xxx.xxx.xxx;

        }

}
[root@www nginx]# cat conf/vhost/abc.conf 
server {
        listen 80;
        server_name abc.watson.com;
        location / {
	    root /web/abc;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            # proxy_pass http://xxx.xxx.xxx;

        }

}
[root@www nginx]# cat conf/vhost/ten.conf 
server {
        listen 80;
        server_name ten.watson.com;
        location / {
	    root /web/ten;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            # proxy_pass http://xxx.xxx.xxx;

        }

}

访问测试

[root@www nginx]# curl www.watson.com
www.watson.com
[root@www nginx]# curl abc.watson.com
abc.watson.com
[root@www nginx]# curl ten.watson.com
ten.watson.com

你可能感兴趣的:(Nginx,nginx,linux,运维)