Nginx and asterisk server_name?

Is there a way to proxy all traffic to a specific server if the domain is not something else?

Basically a * for 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 will use it if none of the other server configurations matches?

+4
source share
4 answers

As @Valery Viktorovsky says, you cannot use * for server_name . You can set the server block as "default" to receive all requests that do not match the others. See this post for an explanation. It will 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 server_name .

+6
source

You cannot use * for server_name . From the doc:

A wildcard name can only contain an asterisk at the beginning or end of a name, and only at the border of a point.

However, you can use the regex:

 server { server_name ~^(www\.)?(.+)$; location / { root /sites/$2; } } 
+7
source

Just a quick update .. as per the docs, wildcards now work: http://nginx.org/en/docs/http/server_names.html

+2
source

FYI, for people like me who were looking for something completely different, the following configuration will listen and respond to all requests on port 80, asking if you are using an IP address or a URL (all AKA):

 server { listen 80 default_server; server_name _; ... } 

Taken from: http://nginx.org/en/docs/http/server_names.html

In the catch-all server examples, you can see the strange name "_":

server {listen to 80 default_server; server name _; return 444; } There is nothing special about this name, ...

You will get errors on sudo service nginx restart or ... start , while the symbolic link /etc/nginx/sites-enabled/default is still active since it uses the same match pattern. Just remove this link and everything should work.

+1
source

Source: https://habr.com/ru/post/1483622/


All Articles