nginx一个ip配置多个网站的访问问题

1 .问题是这样的,我配置了两个网站,然后我无论访问www.hello.com还是www.world.com ,都是访问www.hello.com的内容,为什么??


#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;

    #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;
    # include test/*.conf;
server {
        listen       80;
        server_name  www.hello.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   test1;
            index  index.html 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;
            }
        }
server {
        listen       80;
        server_name  www.world.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   world;
            index  index.html 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;
            }
        }

}

2 . 接着我把关www.hello.com的部分给注释了, 这样不是没配置www.hello.com的资源了吗,但是我访问www.hello.com还是可以访问,页面变成了和www.world.com一样的页面,为什么这么神奇....我已经彻底懵逼了

#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;

    #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;
    # include test/*.conf;
server {
        listen       80;
        server_name  www.hello.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   test1;
            index  index.html 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;
            }
        }
server {
        listen       80;
        server_name  www.world.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   world;
            index  index.html 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;
            }
        }

}

求nginx大佬指点,感激不尽!!!!!

阅读 2.3k
2 个回答

感谢各位大佬的回答!!我自己解决了,可能是由于我的域名购买的是一级域名的原因,我配置域名时多加了www. 我的天,困扰了我一天终于解决了,再次感谢各位大佬的回答,谢谢

你的第二个serve配置嵌套在第一个里面了,放到外层,与第一个serve并列;如:

http {
    server {
        listen       80;
        server_name  www.hello.com;
        ....
    }  
    server {
        listen       80;
        server_name  www.world.com;
        ....
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题