Is there a way to proxy all traffic to a certain server unless the domain is something different?
Basically a *
for the server_name
property?
server {
listen 80;
server_name foo.com
}
server {
listen 80;
server_name *
}
Or, is there a way to set a "default" server, and then it would use it if none of the other specific server configs match?
If no server_name is defined in a server block then nginx uses the empty name as the server name. nginx versions up to 0.8. 48 used the machine's hostname as the server name in this case. If a server name is defined as “ $hostname ” (0.9.
On the other side, server_name _; defines an invalid server names which never intersect with any real name. It is just a non-match. So in the event of no matches, nginx will select the first server{} block and use that. To conclude, you can use server_name _; for catch-all server block but not server_name ""; .
To make Nginx Listen on multiple ports for a single virtual host file, you can add multiple listen directives. If you want to make Nginx listen for different virtual hosts on different ports, you can use different ports in listen directive in different virtual host files. It's that easy!
As @Valery Viktorovsky's answer says, you can't use a * for server_name
. You can designate a server
block as the "default" to receive all requests which don't match any others. See this post for an explanation. It would look like this:
server {
listen 80 default_server;
server_name wontmatch.com; # but it doesn't matter
}
See the docs for server_name
for more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With