Home  >  Q&A  >  body text

Server_name does not work when nginx configures the server?

Why does server_name not work when nginx configures the server?

server {
           listen 8000;
           server_name kaixuan.test.com;
           root /data1/htdocs/kaixuan.test.com/;
           location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
                include        fastcgi_params;
           }
           location / {
                 index index.html;
           }
}

server {
           listen 80;
           server_name kaixuan.hehe.com;
           root /data1/htdocs/kaixuan.hehe.com/;
           location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
                include        fastcgi_params;
           }
           location / {
                 index index.html;
           }
}

The above is my code. I configured two servers. The server_name and port are different.
But when I visited kaixuan.hehe.com:8000, I also entered kaixuan.test.com. [Note the port]
Similarly, I can also access kaixuan.hehe.com when I visit kaixuan.test.com. Is this normal?
If this is normal, how can we solve it online? Add a default one so that he can enter it by default?

我想大声告诉你我想大声告诉你2713 days ago1111

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-05-16 17:17:16

    Yes, add a default blocker.

    When the rules of all servers do not match, nginx will use the first server configuration, so generally the first server will use the blocking page.

    server {
       listen 80;
       server_name _;
       
       return 404;
    }

    reply
    0
  • Cancelreply