nginx配置多站点vhost

vhost配置文件的作用

vhost配置文件的作用是为了将更多的server配置文件的信息,单独存放,不过于集中在nginx.conf配置中,这样有助于查找问题

nginx安装目录

修改nginx.conf

http块中添加include vhosts/*.conf;

#user  nobody;
worker_processes  1;
#pid        logs/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  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /data/lightkits-web/html;
            try_files $uri /index.html;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
      
    }
    # 将vhosts目录下所有conf结尾的文件配置加载到nginx.conf
    # 注意vhosts的
    include vhosts/*.conf;  
 
}

在nginx目录下的vhosts目录中新增conf

cd vhosts
touch welink.conf

文件内容如下

server {
    default_type 'text/html';
    charset utf-8;
    listen 8070; # 监听的端口号
    autoindex off; 
    server_name localhost;  #监听的域名
    # 存放nginx日志的路径
    access_log /usr/src/nginx/logs/welink.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html
    if ( $query_string ~* ".*[\;'\<\>].*" ) {
        return 404;
    }
    location / {
         index index.html; # 请求入口文件
         root  /data/welink/dist/; # 请求的目录
     }
}
注意:编辑conf文件时,每行的末尾用分号;分隔开
将要发布的web应用存放在location指定的目录

测试nginx.conf配置文件是否正确

nginx -t
重新加载配置文件重启
nginx -s  reload

你可能感兴趣的:(nginx配置多站点vhost)