This has been something I really struggled when I first try to setup a config for accessing my docker containers using my domain and at the time I couldn't find a proper document that I can understand so I told myself why not write it yourself so here it is.
As a classic, update your system and later install NGINX to your machine.
sudo apt update sudo apt install nginx
After that, go to the following directory and create a config file for your application
cd /etc/nginx/sites-available/ sudo nano /etc/nginx/sites-available/my-app
Later, add the following to your config file with the correct information
server { listen 80; server_name my-app.your-domain.com;
location / { proxy_pass http://localhost:port-of-your-app; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
You can check the ports of your docker applications using docker ps inside your machine.
Then create a symlink from the file you created to the folder where NGINX holds enabled sites.
sudo ln -s /etc/nginx/sites-available/my-app /etc/nginx/sites-enabled/
after that test your config file and restart your nginx instance
sudo nginx -t sudo systemctl restart nginx
For every app you create with the statements below here, you need to set separate DNS records in your domain provider.
Here is how you can do it:
You can check if the DNS Records have been changed using this tool. It should show the IP Address of your server when you input your subdomain (my-app.yourdomain.com) if you have done everything correctly.
sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d my-app.your-domain.com
ls -l /etc/nginx/sites-enabled/
/etc/nginx/sites-enabled/my-app -> /etc/nginx/sites-available/my-app
ls /etc/nginx/sites-available/
/etc/nginx/sites-enabled/default