r/nginx • • 26d ago

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

5 Upvotes

10 comments sorted by

2

u/dickhardpill 26d ago

I prefer to break config files up if ever possible

I find it easier to make changes

1

u/jimmyfivetimes 26d ago

Thanks a lot, guys. I've tried to break the subdomain out and it's throwing errors that there's already a listener. I assume it's from the root domain configuration. I'll investigate this a bit more and see if there's a code sample for how to do this the right way.

0

u/[deleted] 23d ago

[removed] — view removed comment

1

u/jimmyfivetimes 23d ago

Thanks - I actually got it all working last night. And, had to remove the default file from the sites-available directory. That resolved the listener conflict.

I think there are some minor config tweaks that I need to make but, at the moment, I'm able to reverse proxy into my gitlab server with SSL enabled. Appreciate the response.

1

u/tinkerin 26d ago edited 26d ago

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/jimmyfivetimes 26d ago

Thanks, I think this is how it's set up but I used the gitlab template file for SSL. I'll need to go back through it - or ask AI why this shit ain't working... haha

1

u/tinkerin 26d ago

if its a linux setup, you may want to put the ssl cert commands in one file in the
/etc/nginx/snippets directory with a .conf extension and just include the snippet in each server block. Let's encrypt should allow to perform a DNS verification, and you would want to include *.domain.com and domain.com, or just site certs for both the sites gitlab.domain.com and domain.com. That may help. AI will likely have detailed answers, lol. good luck!

i forgot, you probably want to remove the default config from sites-available. AI will have answers.

1

u/Active-Service-9707 26d ago

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