Nginx configuration with port multi-domain multi-site problem

Before encountered this problem, but also the Internet to find a lot of answers are the same configuration of two server configurations in nginx, but which would resolve a domain name does not come out, so embarrassed, here the record about how to encounter this problem solve

 

Under the conf nginx nginx.conf file directory file, if there are a plurality of server, then 一定要 to ensure that one of the default_server set, so that if nginx according server_name not match the appropriate server, the request may be forwarded to the default server process.

This configuration only needs to be configured in two server nginx configuration on it. Both server listens to the same port, but different server_name. as follows:


# 代码是网站拷贝的, 适当的修改了一点, 只要形式一样就可以了

server {
    listen      80 ;
    server_name xxxx.com;
    root        D:/test/webroot/test/;
    index       index.html index.htm;

    location / {
            allow  all;
    }
}
server {
    listen      80 ;
    server_name www.123.com;
    root        D:/test/webroot/apache/;
    index       index.html index.htm;

    location / {
            allow  all;
    }
}
# [关键点]
# 单文件多站点配置, 如果不配置其中一个为 default_server 则失败
# 多文件多站点配置, 也是一样, 将server放置不同的文件 然后 include vhost/*.conf 进来
server {   
    listen      80 default_server;
    server_name localhost;
    root        D:/test/webroot/nginx/;
    index       index.html index.htm;

    location / {
            allow  all;
    }
}
Released eight original articles · won praise 13 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_37655695/article/details/84837613