Host Multiple ASP.NET Core Web Application Under a Single Linux Server

Host multiple asp.net core web application under a single linux server

The official document shows us a way to use Apache as a reverse proxy :

<VirtualHost *:*>
RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
</VirtualHost>

<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
ServerName www.example.com
ServerAlias *.example.com
ErrorLog ${APACHE_LOG_DIR}helloapp-error.log
CustomLog ${APACHE_LOG_DIR}helloapp-access.log common
</VirtualHost>

Basically, this configuration will make Apache listens on *:80 and proxy any HttpRequest whose ServerName equals www.example.com to http://127.0.0.1:5000/.

This is how Apache used as an proxy to ASP.NET Core works.

As for your question, suppose you have two asp.net core web applications:

  1. the first one is called WebApp1 and listens on 0.0.0.0:5000 .
  2. and the other is called WebApp2 and listens on 0.0.0.0:6000 .

Your Apache server listens on 0.0.0.0:80. For any incoming http request,

  • when Host equals www.webapp1.org, proxy this request to 0.0.0.0:5000
  • when Host equals www.webapp2.org, proxy this request to 0.0.0.0:6000

So you could add two proxies :

proxy 1 :

<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
ServerName www.webapp1.org
ServerAlias *.webapp1.org
ErrorLog ${APACHE_LOG_DIR}webapp1-error.log
CustomLog ${APACHE_LOG_DIR}webapp1-access.log common
</VirtualHost>

proxy 2 :

<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:6000/
ProxyPassReverse / http://127.0.0.1:6000/
ServerName www.webapp2.org
ServerAlias *.webapp2.org
ErrorLog ${APACHE_LOG_DIR}webapp1-error.log
CustomLog ${APACHE_LOG_DIR}webapp2-access.log common
</VirtualHost>

How to host multiple .NET Core apps under the same URL?

You could use a reverse proxy for serving multiple ASP Net Core application on the same domain.

Using IIS I'm not quite sure but you need to install URL Rewrite and then follow the documentation provided by Microsoft (also this one should be helpful).

You can also use nginx using location and proxy_pass as follow:

...
Some nginx Configuration
location /App1 {
proxy_pass http://127.0.0.1:443;
}
location /App2 {
proxy_pass http://127.0.0.1:444;
}
location /App3 {
proxy_pass http://127.0.0.1:445;
}
Other configurations...

And then, each time you want add another ASP Net application to your domain you'll need to add another location, run the application on a different point and restart nginx.

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

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.

Hosting Multiple Asp.net Core projects under one domain

You can create different applications under your web site in IIS and then deploy different ASP.NET core projects to the different applications.

More information about how to create application you can refer to this link:
application

Running multiple ASP.NET Core (3.1x/Latest) websites on port 80 with Kestrel

To run multiple domains on one server on port 80 and/or 443 with Kestrel you'll need to put a reverse proxy in front of it.

Microsoft has now its own really fast reverse proxy called YARP which is actually another Kestrel instance, see: Getting Started With Yarp

To use it with TLS/HTTPS you'll need Kestrel's SNI feature, see this GitHub discussion for more info

Here's a sample appsettings.Development.json GIST how I did it.

How do I run more than one asp.net core application on Nginx server

The point of a reverse proxy like nginx is that you can host multiple applications on it, regardless of what internal port they use. So you can easily host applications on ports 5000, 5001, 5002, etc. and then make nginx expose all those applications on different domains on port 80, or even as subpaths of the same domain (if the applications are set up properly to support that).

So an nginx configuration could look like this:

server {
server_name host1.example.com;

location / {
proxy_pass http://localhost:5000/;
# …
}
}

server {
server_name host2.example.com;

location / {
proxy_pass http://localhost:5001/;
# …
}
}

server {
server_name host3.example.com;

location / {
proxy_pass http://localhost:5002/;
# …
}
}

This would host three different applications, using internal ports 5000, 5001, and 5002, on the three different subdomains host1.example.com, host2.example.com, and host3.example.com.

Of course, this requires you to actually run your applications on different ports. You can use the ASPNETCORE_URLS environment variable to set the url dynamically at which the app is being hosted, e.g. using:

$ ASPNETCORE_URLS=http://localhost:5001 dotnet run


Related Topics



Leave a reply



Submit