.Net Core Hosting with HTTPS on Nginx with LetsEncrypt Cert

Andrew Harkins 46 Reputation points
2021-11-03T14:53:57.933+00:00

Hello all,
I currently have my project running http without any issues. Now I am working on encrypting my traffic by using https. I have a certificate for my domain and I set up all the code and areas I thought were necessary to deploy it as https.
When I do this and run it my web page is not displayed, I just get the default nginx page. When I do this with http my web page is displayed.
Here are all the steps I used for setting up my certificate and deploying it as HTTPS:

1.  .Net Core Project.
Appsettings.json I added these 2 sections.
“Kestrel”: {
    “Endpoints”: {
        “Http”: {
            “Url”: “http://localhost:60110”
        },
        “Https”: {
            “Url”: “https://localhost:60111”
        }
    }
},
“https_port”: 60111

2.  Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>()
                    .UseUrls(@“http://*:60110”)
                    .UseUrls(@"https://*:60111")
               .UseKestrel();
                });
3.  Startup.cs
I added app.UseHsts();

4.  Nginx I have 2 vhost files. 1 is for my ‘listen 80;” which works.
My 2nd is this :
server {
    listen 443 ssl;

    ssl_certificate      /path/to/fullchain.pem #managed by Certbot
    ssl_certificate_key /path/to/privkey.pem    #managed by Certbot

    root /var/www/websiteFolder;

    server_name MyDomain.tech

    location / {

        proxy_pass         http://localhost:60111;
        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;}


}

5.  I setup the symbolic link in nginx/sites-enabled from sites-available
I did the sudo nginx -t and my syntax passes.
I also did the nginx -s reload to reload all my conf and vhost files on nginx.

What am I missing or did I do wrong?
Thank you all.
Developer technologies | ASP.NET | ASP.NET Core
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-11-07T16:27:42.587+00:00

    Normally you would not use UseHttpsRediection. You would put the redirect rule in ngnix configuration.

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-11-04T17:47:40.673+00:00

    Normally you would configure ngnix to use the cert. if you want ngnix to use ssl to your asp.net core app, that a localhost cert. typically a self signed that is installed.


  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-11-05T14:45:50.067+00:00

  3. Andrew Harkins 46 Reputation points
    2021-11-07T15:11:05.873+00:00

    I Finally got this working! as of last night.

    The stuff I was missing in my vHost:

      location ~ /.harkins.tech {  
            allow all;  
            root /var/www/harkinsTest;  
        }  
    

    Thank you!
    Do you think I need

    app.UseHttpsRedirection();  
    

    in my code if I am using

    147123-image.png

    in my server { listen 80; }

    0 comments No comments

  4. Mohamed 0 Reputation points
    2024-01-04T00:10:06.2466667+00:00

    after a looot of troubleshooting , trial and error .... don't proxy your traffic through Cloudflare

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.