问题原文
原帖链接 · 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_serverparameter(because they are the default by default - and there is no other
serverblock around to process requests on port 80 anyway)the
server_namedirective(as they are the only
serverblocks for port 80, so NGINX won't even bother comparingserver_namewith a request'sHostheader: 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_namedeclaration that doesn't match any valid domainWhen NGINX sees that there are more than one block for port 80, it compares the request's
Hostfield toserver_nameto choose the right one. If none match, then the request will be routed to thedefault_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 ...declarationIf the catch-all block for that [
ip_address:]portis 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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
