Hosting multiple ASP NET Core sites on unbuntu and nginx as reverse proxy
Asked Answered
I

3

6

I am trying to host multiple ASP NET Core sites with different domains on Linux, Unbunt 18.04 and using nginx as reverse proxy.

These are the steps:

1) Creating new .conf files in /etc/nginx/sites-available

2) Creating folders in /var/www/ and uploadin the .net app

3) Creating new .service files for each .conf file

The default nginx .conf is unchanged.

The .conf files look like this:

server {
    listen        80;
    server_name   domain;
    location / {
        proxy_pass         http://localhost:5000;
        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;
    }
}

The .service files look like this:

[Unit]
Description=Event Registration Example

[Service]
WorkingDirectory=/var/www/example
ExecStart=/usr/bin/dotnet /var/www/example/example.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

With this configuration, even I deploy few sites, all of them are redirected to same content. My goal is to host multiple .net core apps on same server. How the configuration should look like?

Imperishable answered 16/10, 2019 at 18:46 Comment(4)
Are you linking the new .conf from sites-available to sites-enabled and then reloading nginx?Pyrenees
Yes, I am linking every .conf file from sites-available to sites-enabled, reloading Nginx and .service for each service.Imperishable
@Imperishable Are you successful in doing this? I am looking for a solution for similar question #66194615 and needs a way to send nginx.conf via codepipe line to set everything automatically on re-publishNapper
i have a similar issue... may somebody help?Homelike
E
8

I had a similar issue.

Each of your applications nginx config files should point to the correct port number that the .Net Core application is set to run on.

This is determined in each of your .Net Core applications program.cs in the .UseUrls() extension, e.g.

public static IWebHost CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseUrls("http://0.0.0.0:2001")
                .UseStartup<Startup>()
                .Build();

Each application will need to have a different port number and have this reflected in its nginx config files, like so:

server {
    listen        80;
    server_name   domain;
    location / {
        proxy_pass         http://localhost:2001;
        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;
    }
}

Hope this helps.

Eastwood answered 24/10, 2019 at 15:9 Comment(2)
This is the correct answer, thank you. Only thing that i missed was in the program file to specify the url.Imperishable
@Jack Thank you, This is solve my issueSupersensitive
C
3

If you want to host two or more applications on one server
you need to configure nginx something like this:

cat /etc/nginx/conf.d/domain.conf

server {
    listen        80;
    server_name   domain;

    location /prod {
        rewrite            /prod(.*) $1 break;
        proxy_pass         http://localhost:5000;

        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;
    }

    location /dev {
        rewrite            /dev(.*) $1 break;
        proxy_pass         http://localhost:5001;

        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;
    }
}

The configuration of two .Net Core applications will look like:

cat /etc/systemd/system/example_prod.service

[Unit]
Description=Example production on .Net Core

[Service]
WorkingDirectory=/var/www/example
ExecStart=/usr/bin/dotnet /var/www/example/example.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=example-production
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
Environment=ASPNETCORE_URLS=http://localhost:5000

[Install]
WantedBy=multi-user.target

cat /etc/systemd/system/example_dev.service

[Unit]
Description=Example development on .Net Core

[Service]
WorkingDirectory=/var/www/example
ExecStart=/usr/bin/dotnet /var/www/example/example.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=example-development
Environment=ASPNETCORE_ENVIRONMENT=Development
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
Environment=ASPNETCORE_URLS=http://localhost:5001

[Install]
WantedBy=multi-user.target

At the conclusion:
I launched two applications from one path: /var/www/example/example.dll
with different environments: Production and Development
on different ports: localhost:5000 and localhost:5001

And I confugured nginx to reverse proxy:

http://localhost:5000 => http://domain/prod/
http://localhost:5001 => http://domain/dev/
Cementite answered 7/4, 2021 at 11:10 Comment(1)
finally the essential line "rewrite /prod(.*) $1 break;" saves my noobie day!Lynden
K
0

Individual ports on the server is the way to go, in ASP.NET Core 3.0 my program.cs looks like this:

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.ConfigureKestrel(serverOptions =>
                {

                    serverOptions.Listen(IPAddress.Loopback, 5100);

                })
                .UseStartup<Startup>();
            });
}
Kempis answered 18/2, 2020 at 4:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.