Search code examples
.netasp.net-coreubuntunginx

Assign a domain when deploying a .Net API with NGINX on Ubuntu


I deployed a .Net Core API to my Ubuntu machine using nginx. I can access it with my IP, but I'll soon need to deploy it to an Ubuntu VPS, and I have bought a .com domain, how do I assign that domain so when I enter it on the browser I can use the API endpoints? I'm quite newbie on those kind of things.


Solution

  • 1)First you have to configure your ip address to the domain name with an A record when you purchase it. the ip address will be the public IP address of your Ubuntu VPS where your .NET Core API is deployed.

    2)Create a new Nginx configuration file in /etc/nginx/sites-available/:

    sudo nano /etc/nginx/sites-available/yourdomain.com
    

    Insert the following configuration, adjusted for your domain and application paths:

    server {
            listen 80;
            server_name yourdomain.com www.yourdomain.com;
    
            location / {
                proxy_pass http://localhost:5000; # Replace with the port your app is running on
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection keep-alive;
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
            }
        }
    

    3)Link the configuration file from sites-available to sites-enabled:

    sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
    

    4)Reload Nginx to apply changes:

    sudo systemctl reload nginx