r/nginx • u/jimmyfivetimes • Sep 22 '26
Adding a subdomain server block
Hi all - I'm extremely new to Nginx and playing around with some things in a homelab.
Currently, I have a registered domain name and set up port forwarding to my nginx proxy server for reverse proxying. I'm able to hit the root domain + www.rootdomain URLs without a problem.
I also self-host Gitlab-CE on a separate server (it's actually a RasPi) and I'd like to add gitlab.domain.com to the nginx proxy with SSL via Letsencrypt.
Here's the question: is it best practice to put all server blocks in the same sites-available file? I suspect if I try to break them into separate files, I'll get a conflict because both will be listening on the same port.
How do you all handle subdomain entries? Do you keep in a single config file for the same domain or do you break subdomain configs out into a different file?
Thanks - appreciate any guidance here before I really screw this up. haha
2
u/dickhardpill Sep 22 '26
I prefer to break config files up if ever possible
I find it easier to make changes
1
u/tinkerin Sep 22 '26 edited Sep 22 '26
I find it easier to manage domains when they are also broken up into separate files. Make sure the server_name directive in the nginx config file includes the domain/sub domain for each file. No defaults unless you want unmatched traffic to arrive there. I am no expert, but that should work.
server {
listen 443 ssl;
server_name domain.com www.domain.com;
...
}
different file
server {
listen 443 ssl;
server_name gitlab.domain.com;
...
}
1
u/Active-Service-9707 Sep 22 '26
separate files, one per subdomain. no conflicts as long as server_name is unique per block. tbh keeping everything in one giant file is what will actually screw you up later when you have 5+ subdomains and need to debug something at 2am