I\'m new to Nginx and I\'m trying to get subdomains working.
What I would like to do is take my domain (let\'s call it example.com
) and add:
Add A field for each in DNS provider with sub1.example.com and sub2.example.com
Set the servers. Keep example.com at last
As below
server {
server_name sub1.example.com;
# sub1 config
}
server {
server_name sub2.example.com;
# sub2 config
}
server {
server_name example.com;
# the rest of the config
}
sudo systemdctrl restart nginx
You just need to add the following line in place of your server_name
server_name xyz.com *.xyz.com;
And restart Nginx. That's it.
The mistake is putting a server block inside a server block, you should close the main server block then open a new one for the sub domains
server {
server_name example.com;
# the rest of the config
}
server {
server_name sub1.example.com;
# sub1 config
}
server {
server_name sub2.example.com;
# sub2 config
}
You'll have to create another nginx config file with a serverblock for your subdomain. Like so:
/etc/nginx/sites-enabled/subdomain.example.com
There is a very customizable solution, depending on your server implementation:
Two (or more) SUBdomains in a single nginx "sites" file? Good if you own a wildcard TLS certificate, thus want to maintain ONE nginx config file. All using same services BUT different ports? (Think of different app versions running concurrently, each listening locally to differents ports)
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name ~^(?<sub>.+).example.com;
# default app (the "default" ports, good for the "old" app)
set $app 19069;
set $app-chat 19072;
# new app
# new.example.com
if ( $sub = "new" ) {
set $app 18069;
set $app-chat 18072;
}
# upstreaming
location / {
proxy_redirect off;
proxy_pass http://127.0.0.1:$app;
}
location /longpolling {
proxy_pass http://127.0.0.1:$app-chat;
}
I know the performance will "suck", but then again, since the decision was to go with one server for all it's like complaining that an econobox cannot haul as much people as a bus because the little car has a "heavy" roof rack on top of it.
A regex expert could potentially improve the performance for such a custom solution, specially since it could ommit the CPU expensive "if" conditional statements.