I have a server successfully hosting a number of domains using nginx. Each domain has a separate nginx configuration file.
One domain, let's call it example.com is the default server
It has a config file like so:
# config file for example.com
#
#redirects all www to NON-www
#
server {
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 80 default_server;
server_name example.com;
charset utf-8;
root /blah/example.com/public/www;
This works great -- until I add a second config file for various "administrative redirects" for folders and subdomains of the same domain.
I did this because this domain is quite old and had a bunch of apps and sites in subdirectories before domains were purchased for those. On the off chance some users have old bookmarks, we want to make sure those still work.
I would like to maintain these redirects in a separate nginx config file for easier oversight.
So here's the second config file (simplified for purposes of discussion):
# config file for Administrative_Redirects.example.com
#redirects Webmin and/or Panel request to proper port and https
#
server {
listen 80;
server_name webmin.example.com;
return https://webmin.example.com:10000;
}
server {
listen 80;
server_name panel.example.com;
return https://webmin.example.com:10000;
}
#------------------------------------------------------------------------
#
#redirects all Webmail and related cPanel requests to XYZ account
#
server {
listen 80;
server_name webmail.example.com;
return https://3rdPartyMail.com:port;
}
server {
listen 80;
server_name www.webmail.example.com;
return https://3rdPartyMail.com:port;
}
server {
listen 80;
server_name cpanel.example.com;
return https://AnotherDomain.com:port;
}
#------------------------------------------------------------------------
#
#redirects subdirs to proper domains and/or subdomains
#
# ~*/(regex_case-INSensitive)
server {
listen 80;
server_name example.com;
# some_app
location ~*/(some_folder)
{
return 301 $scheme://SomeOtherDomain.com;
}
# another_app
location ~*/(another_folder)
{
return 301 $scheme://AnotherDomain.com;
}
# Some_Name
location ~*/(some_name)
{
return 301 $scheme://some_name.example.com;
}
}
# strip www subdomain
server {
listen 80;
server_name www.example.com;
# some_app
location ~*/(some_folder)
{
return 301 $scheme://SomeOtherDomain.com;
}
# another_app
location ~*/(another_folder)
{
return 301 $scheme://AnotherDomain.com;
}
# Some_Name
location ~*/(some_name)
{
return 301 $scheme://some_name.example.com;
}
}
# -----------
My two problems:
IF this config file for "Administrative_Redirects.example.com" is active, everything within it resolves fine, BUT browsing to example.com or www.example.com result in 404.
Browsing to any_made-up_subdomain.example.com (i.e. anything NOT specified in config file for "Administrative_Redirects.example.com") serves the document root (i.e. root /blah/example.com/public/www; ) . . . but should probably result in 404
The problem seems to come from the following sections in the config file for "Administrative_Redirects.example.com"
#-------
server {
listen 80;
server_name example.com;
#-------
and
#-------
server {
listen 80;
server_name www.example.com;
#-------
These seem to "preempt" the default_server config file which alpha-sorts later in the list of active nginx sites.
How can I keep the "Administrative_Redirects.example.com" from trampling on the default_server nginx config file?
Is there a better way of doing this while maintaining the redirects in a separate file?
Can I, for example, include these "administrative redirects" into the default_server config file so I wouldn't have such a huge mess there?
Thanks in advance for any insights!
server
blocks with the samelisten
port andserver_name
,nginx
should give you an error or a warning - and at best will ignore one of theserver
blocks altogether.