I'm trying to configure nginx in a way that will mean less fiddling later down the road.
I don't want to have to create a config file for each domain and sub domain, I want a single configuration file that can handle all (or two if sub domain needs seperation from the main domain).
For example, let's say these domains are in use:
- example.com
- /var/www/example/public
- www.example.com
- /var/www/example/public
- test.example.com
- /var/www/example/subdomains/test/public
- abc.example.com
- /var/www/example/subdomains/abc/public
- myothersite.com
- /var/www/myothersite/public
Ofcourse www and non-www should no co-exist - so one should redirect to the other. Let's imagine I'm not redirecting from one to the other in this case, they should both use the same directory.
The above bullet points illustrate the type of directory structure I'm trying to achieve, however all my attempts have failed so far.
server {
listen 80 default_server;
server_name ~^(www\.)?(?<domain>.+)$;
# Location of each domain
location / {
root /var/www/$domain/public;
index index.php index.html index.htm;
}
# ... some other config stuffs
}
I then attempted to set up a sub domain configuration file, however this one always seem to take presidence (and it also meant I had to name my site folders like "example.com" or "example.co.uk").
When I went to an address like "test.example.com" it would show up in the error logs as trying to access /var/www/test.example.com/public
.
Any ideas? It is much appreciated!
I'm on CentOS 6.4 and nginx 1.0.15.