Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx server_name wildcard or catch-all

People also ask

What should be the server_name in nginx?

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.

How do I map a domain in nginx?

Create the web root and configuration file Create the root directory to host our website's files. Create the Nginx configuration file under /etc/nginx/sites-available. For easy reference, name the configuration file after the domain name. Open the configuration file in a text editor.

How do I change my localhost domain to nginx?

Go to c:\Windows\System32\Drivers\etc\hosts directory and open the file with a text editor and then add this line 127.0. 0.1 localhost site1 site2 . Then open any browser and hit site1/ . Make sure it is http:// , not https:// .


Change listen option to this in your catch-all server block. (Add default_server) this will take all your non-defined connections (on the specified port).

listen       80  default_server;

if you want to push everything to index.php if the file or folder does not exist;

try_files                       $uri /$uri /index.php;

Per the docs, It can also be set explicitly which server should be default, with the **default_server** parameter in the listen directive


As a convention, the underscore is used as a server name for default servers.

From http://nginx.org/en/docs/http/server_names.html

In catch-all server examples the strange name “_” can be seen:

server {
   listen       80  default_server;
   server_name  _;
   return       444;
}

There is nothing special about this name, it is just one of a myriad of >invalid domain names which never intersect with any real name. Other >invalid names like “--” and “!@#” may equally be used.

Note that server_name _; alone is not enough. The above example only works because of default_server in the listen directive.


This will work:

server_name ~^(.+)$

For me somehow define default_server was not working. I solved it by

server_name ~^.*$

using regular expression of all.