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.domain.com
. These work great. Now I'd like all other domain requests to go to a single index.php
- I have loads of domains and subdomains and it's impractical to list them all in an nginx config.
So far I've tried setting server_name
to *
but that failed as an invalid wildcard. *.*
works until I add the other server blocks, then I guess it conflicts with them.
Is there a way to run a catch-all server block in nginx after other sites have been defined?
N.B. I'm not a spammer, these are genuine sites with useful content, they're just powered by the same CMS from a database!
Only 1
server
directiveFrom Nginx
listen
DocsIf you only have 1
server
directive, that will handle all request, you don't need to set anything.Multiple
server
directiveIf you want to match all request with specified
server
directive, just adddefault_server
parameter tolisten
, Nginx will use thisserver
directive as default.About
server_name _;
From Nginx Docs
It doesn't matter what
server_name
you set, it is just an invalid domain name.For me somehow define default_server was not working. I solved it by
using regular expression of all.
A simple underscore works as well :
Source : http://nginx.org/en/docs/http/server_names.html
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).if you want to push everything to index.php if the file or folder does not exist;
Now you can use mask:
Look more here: http://nginx.org/en/docs/http/server_names.html
This will work: