Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx and asterisk server_name?

Tags:

nginx

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?

like image 596
Ahmed Avatar asked May 30 '13 16:05

Ahmed


People also ask

Does nginx need server_name?

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.

What does server_name _ mean in nginx?

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 ""; .

Can nginx listen on multiple ports?

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!


1 Answers

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.

like image 94
pjmorse Avatar answered Jan 04 '23 01:01

pjmorse