nginx虚拟主机优化

Nginx虚拟主机优化

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      9998 ;
        server_name  www.hello.com;
        location / {
            root   html/hello;
            index  index.html index.htm;
        }
    }


    server {
        listen       9999;
        server_name  www.world.com;
        location / {
            root   html/world;
            index  index.html index.htm;

        }
    }
}

虚拟主机配置文件优化

#在nginx同级目录,创建存放主机配置文件的目录
mkdir zj

#创建虚拟主机配置文件
vi hello.conf

#将nginx.conf文件内的hello虚拟主机server模块拷贝到hello.conf内
server {
    listen       9998;
    server_name  www.hello.com;
    location / {
        root   html/hello;
        index  index.html index.htm;

    }
}

#创建world.conf
vi world.conf

#将nginx.conf文件内的world虚拟主机server模块拷贝到world.conf内
server {
    listen       9999;
    server_name  www.world.com;
    location / {
        root   html/world;
        index  index.html index.htm;

    }
}

#将主机配置文件移动到zj目录下
mv hello.con world.conf jz/

#处理完之后的nginx.conf文件
#通过include extra/world.conf方式导入虚拟主机配置文件
worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include zj/hello.conf;
    include zj/world.conf;
}



#检查配置文件
/appliction/nginx/sbin/nginx -t

#没有问题重启nginx
/appliction/nginx/sbin/nginx -s reload

#访问虚拟主机地址没有任何影响,虚拟主机配置文件与nginx主配置文件分离

 虚拟主机别名设置

#设置别名
#在主机配置文件的域名位置添加没有www的域名
    server {
        listen      9998 ;
        server_name  www.hello.com hello.com;
        location / {
            root   html/hello;
            index  index.html index.htm;
        }
    }

 

posted @ 2018-06-12 16:27  忽略!  阅读(105)  评论(0编辑  收藏  举报