nginx server_name wildcard or catch-all

前端 未结 7 727
长情又很酷
长情又很酷 2020-12-04 06:57

I have an instance of nginx running which serves several websites. The first is a status message on the server\'s IP address. The second is an admin console on admin.d

相关标签:
7条回答
  • 2020-12-04 07:18

    This will work:

    server_name ~^(.+)$
    
    0 讨论(0)
  • 2020-12-04 07:22

    If you also want to catch requests with empty Host header (which is allowed in HTTP/1.0) you can use both regex and empty server_name:

    server {
        listen      80;
        server_name ~. "";
    }
    
    0 讨论(0)
  • 2020-12-04 07:28

    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

    0 讨论(0)
  • 2020-12-04 07:31

    Only 1 server directive

    From Nginx listen Docs

    The default_server parameter, if present, will cause the server to become the default server for the specified address:port pair. If none of the directives have the default_server parameter then the first server with the address:port pair will be the default server for this pair.

    If you only have 1 server directive, that will handle all request, you don't need to set anything.


    Multiple server directive

    If you want to match all request with specified server directive, just add default_server parameter to listen, Nginx will use this server directive as default.

    server {
        listen 80 default_server;
    }
    

    About server_name _;

    From Nginx Docs

    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.

    It doesn't matter what server_name you set, it is just an invalid domain name.

    0 讨论(0)
  • 2020-12-04 07:35

    A simple underscore works as well :

    server_name  _;
    

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

    0 讨论(0)
  • 2020-12-04 07:37

    Now you can use mask:

    server {
        listen       80;
        server_name  *.example.org;
        ...
    }
    
    server {
        listen       80;
        server_name  mail.*;
        ...
    }
    

    Look more here: http://nginx.org/en/docs/http/server_names.html

    0 讨论(0)
提交回复
热议问题