猿问

nginx 不能用同一个端口不同的域名去访问吗?为什么我的server_name不起作用

我本地有个nginx环境,但是我们有很多项目,都需要用80端口,但是,为什么我配置Nginx里面多个server里面都监听80端口,然后通过server_name去区分,在nginx配置里面建了多个.conf的文件,我发现每个域名(我在host里面设置的到80的域名),都会跳转到我第一个项目上面


etc/hosts 代码如下

127.0.0.1   dev.demo.com
127.0.0.1   dev.blog.com

nginx 配置文件路径

第一个项目nginx配置

server {

    #listen 80 default_server;
    #listen [::]:80 default_server ipv6only=on;
    listen 80;

    server_name dev.blog.com;
    root /var/www/blog/public;
    index index.php index.html index.htm;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }
return 404;
}

第二个项目配置

server {

    #listen 80 default_server;
    #listen [::]:80 default_server ipv6only=on;
    listen 80;

    server_name dev.demo.com;
    root /var/www/demo;
    index index.php index.html index.htm;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }
}

问题是,我现在访问 dev.blog.com是正常的,但是,访问dev.demo.com,也会跳转到blog的项目下面,而且,hosts里面,所有指向127.0.0.1的,都会访问到blog项目,请问下,nginx不同像apache一样,通过域名配置虚拟主机吗?

翻过高山走不出你
浏览 878回答 2
2回答

天涯尽头无女友

感谢666666666666666666666
随时随地看视频慕课网APP
我要回答