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!
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 ~^(.+)$
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
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.
For me somehow define default_server was not working. I solved it by
server_name ~^.*$
using regular expression of all.
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 ~. "";
}
Try $http_host
server {
server_name $http_host;
}
Related
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!
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 ~^(.+)$
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
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.
For me somehow define default_server was not working. I solved it by
server_name ~^.*$
using regular expression of all.
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 ~. "";
}
Try $http_host
server {
server_name $http_host;
}
Our nginx config serves multiple sites with their own subdomains.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name ~^(?P<sub>.+)\.(example1|example2)\.com$;
root /var/www/instances/$sub;
...
}
I want to redirect one specific subdomain with its path to different sites, but I cannot figure out how to write the check. I need to check for the host part and then check for the path to decide where the redirect should land.
The map looks somehting like this:
Old URI
New URI
sub1.example1.com/wiki/0/$path
newSub1.example1.com/wiki/0/$path
sub1.example1.com/wiki/20/$path
newSub2.example1.com/wiki/0/$path
Where $path is simply the rest of the request URI
All other requests to sub1.example1.com should work as before.
The obvious solution is to split sub1.example1.com into a separate server block. As you will see from this document a server_name with an exact name always takes precedence over a server_name with a regular expression.
This means that there are two server blocks with near identical contents, but this can be mitigated by using the include directive.
Alternatively, you can test the value of $host$request_uri using a map directive. This is less efficient, as you will be testing the URL in every site.
For example:
map $host$request_uri $redirect {
default 0;
~*^sub1.example1.com/wiki/0/(?<path>.*)$ //newSub1.example1.com/wiki/0/$path;
~*^sub1.example1.com/wiki/20/(?<path>.*)$ //newSub2.example1.com/wiki/0/$path;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name ~^(?P<sub>.+)\.(example1|example2)\.com$;
root /var/www/instances/$sub;
if ($redirect) { return 301 $scheme:$redirect; }
...
}
I configured nginx (and dnsmasq) to listen to example.test and *.example.test wildcard subdomain. Everything seems to work fine. Here is nginx.conf:
server {
listen 80;
server_name ~(\.)?example\.test$;
...
}
Now I want to respond to all other *.test domains from their own directory on disk. I just don't know how to make it happen, following config doesn't work (just disables above configuration):
server {
listen 80;
server_name \.test;
...
}
Even following configuration has same effect:
server {
listen 80;
server_name ~(?!(\.)?example)\.test;
...
}
Both these configs work, but disables *.example.test and example.test configuration and responds to them just as other *.test domains.
Here is my question:
How can I configure nginx to respond to *.test but respond to example.test and *.example.test in a different way?
After a wasting a whole day, I finally managed to fix the issue.
For those who may find themselves in such a confusing situation, here is the solution:
# First server block for default configuration:
server {
listen 80;
server_name ~^[a-zA-Z0-9\-_]+\.test$; # matches domain names (e.g. anything.test)
...
}
server {
listen 80;
server_name ~(\.)?example\.test$ example.test; # matches all subdomains (e.g. subdomain.example.test and sub.subdomain.example.test) as well as example.test
...
}
I have settings to www rewrite or return.
1:
server {
listen 80;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
2:
server {
listen 80;
server_name example.com;
return 301 https://www.$host$request_uri;
}
Are these two things equal?
It seems to be working well, but I can't have assurance.
It depends on which server block is the default server for port 80.
If this server block is also the implicit default server for port 80, it may need to handle requests for server names other than example.com, in which case the value of $host will not be equal to the value of $server_name. See this document for more.
You could use $server_name instead of $host. See this document for details.
server_name in nginx does not match
I want to match with such FQDNs
I came up with
server_name "~^(www.)?ucwebapi-uccore(\d{0,3})-(\d{0,3})\.testme\.net";
To Match
ucwebapi.testme.net
ucwebapi-uccore.testme.net
ucwebapi-uccore1-0.testme.net
ucwebapi-uccore999-999.testme.net
Validated with https://regex101.com/r/tAwEp9/2
Tested with
server_name "~^(www.)?ucwebapi-uccore(\d{0,3})-(\d{0,3})\.testme\.net ucwebapi1.testme.net";
to see if ucwebapi1.testme.de server is reachable at all.
Is there any restriction im not aware of?
Thank you.
Try this:
server_name "~^(www.)?ucwebapi(-uccore)?(\d{1,3}-\d{1,3})?\.testme\.net";
It looks like there are some missing characters between your regex101 page and what ended up in your config.
I've also tuned it a bit so that it will NOT match:
ucwebapi-uccore999.testme.net
ucwebapi-uccore-.testme.net
ucwebapi-uccore-999.testme.net
I've never seen server_name configurations with double-quotes... but I'm not shure if that solves the problem.
Some example configurations here.
Edit: Do you have a default virtual server like this:
server {
listen 80;
server_name _;
return 404; # default
}
# now add your specific server
server {
listen 80;
server_name "~^(www.)?ucwebapi-uccore(\d{0,3})-(\d{0,3})\.testme\.net ucwebapi1.testme.net";
...
}
Specific configurations will only work if you have a default configured.
#abcdn: your absolutely right, i didn't know that!
Try:
server_name "~^(www.)?ucwebapi-uccore(\d{0,3})-(\d{0,3})\.testme\.net" ucwebapi1.testme.net;
Your server_name quotes encompassed the entire line. What is probably perceived as 2 separate entries, nginx interpolated as a single entry.