大网数据广告活动
服务器运维问答

Is server_name really needed for default_server? (Nginx)

· 52CE 我爱测

I tried the 4 cases of code with default_server below one by one in Nginx. The file default.conf with the code is located at /etc/nginx/conf.d/default.conf . 1st: With server_name _; server { listen 80 default_server; listen [::]…

问题原文

原帖链接 · Super Kai - Kazuya Ito · CC BY-SA 4.0

I tried the 4 cases of code with default_server below one by one in Nginx. The file default.conf with the code is located at /etc/nginx/conf.d/default.conf.

1st: With server_name _;

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;

    return 500;
}

2nd: With a different server name(private ip) server_name 10.0.0.0; (My computer's private ip is 10.156.58.216)

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name 10.0.0.0;
    
    return 500;
}

3rd: With server_name "";

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name "";

    return 500;
}

4th: Without server name

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    return 500;
}

My computer's private ip is 10.156.58.216.

Then, the results are all the same showing 500 Internal Server Error page.

I think for default server, we don't need server name. So the 4th code without server name is fine to use.

4th: Without server name

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    return 500;
}

I ask again, Is server name really needed for default server?

社区高分解答

原帖链接 · toraritte · CC BY-SA 4.0

The only time NGINX looks at the server_name directive and listen's default_server parameter during processing the configuration is when there is more than one server block listening on the same [ip_address:]port. (It's also worth pointing out that there can be more than one default_server declaration in one config - one per [ip_address:]port.)

If a server block stands alone in an active configuration (i.e., there are no other server blocks listening on the same port, such as 80), then all of your examples can omit

  • the default_server parameter

    (because they are the default by default - and there is no other server block around to process requests on port 80 anyway)

  • the server_name directive

    (as they are the only server blocks for port 80, so NGINX won't even bother comparing server_name with a request's Host header: all the requests would be directed there anyway)

When does the server_name directive need to be used in tandem with default_server?

One example is when you need a catch-all block to dismiss any connection that is not explicitly handled in other server blocks.

For a concrete example, here's an HTTP-to-HTTPS redirect:

##########################
# HTTP-to-HTTPS redirect #
##########################
server {
    listen 80;
    server_name ourdomain.com;

    return 301 https://$host$request_uri;
}

##########################
# HTTP-to-HTTPS redirect #
##########################
server {
    listen 80;
    server_name another.doma.in;

    return 301 https://$host$request_uri;
}

# ... more `server` blocks listening on port 80

#############
# CATCH-ALL #
#############
server {
   listen 80 default_server; 
   server_name _;

   return 404;
}

In this case, the catch-all block must have both:

  • a server_name declaration that doesn't match any valid domain

    When NGINX sees that there are more than one block for port 80, it compares the request's Host field to server_name to choose the right one. If none match, then the request will be routed to the default_server. (See How NGINX processes a request and the flowchart at the bottom.)

    It's important this block does not match anything because it can be anywhere in the config file, so it can accidentally process a request intended for another one. (Plus, the point is to explicitly configure the domains / IP addresses the server will handle, and deny everything else.)

  • a listen ... default_server ... declaration

    If the catch-all block for that [ip_address:]port is the first one in the file, it will implicitly become the default server for it. Then again, it can also be anywhere in the config, plus it never hurts to be explicit.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-05-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。