简体   繁体   English

在 unbuntu 和 nginx 上托管多个 ASP NET Core 站点作为反向代理

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

I am trying to host multiple ASP NET Core sites with different domains on Linux, Unbunt 18.04 and using nginx as reverse proxy.我正在尝试在 Linux、Unbunt 18.04 上托管多个具有不同域的 ASP NET Core 站点,并使用 nginx 作为反向代理。

These are the steps:这些是步骤:

1) Creating new.conf files in /etc/nginx/sites-available 1) 在 /etc/nginx/sites-available 中创建 new.conf 文件

2) Creating folders in /var/www/ and uploadin the .net app 2) 在 /var/www/ 中创建文件夹并在 .net 应用程序中上传

3) Creating new.service files for each.conf file 3) 为每个.conf 文件创建 new.service 文件

The default nginx.conf is unchanged.默认的 nginx.conf 没有改变。

The.conf files look like this: .conf 文件如下所示:

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: .service 文件如下所示:

[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.我的目标是在同一台服务器上托管多个 .net 核心应用程序。 How the configuration should look like?配置应该如何?

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.您的每个应用程序 nginx 配置文件都应该指向正确的端口号,.Net Core 应用程序设置为在其上运行。

This is determined in each of your.Net Core applications program.cs in the .UseUrls() extension, eg这是在.UseUrls()扩展名中的每个.Net Core 应用程序program.cs中确定的,例如

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:每个应用程序都需要有不同的端口号,并将其反映在其 nginx 配置文件中,如下所示:

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.希望这可以帮助。

Individual ports on the server is the way to go, in ASP.NET Core 3.0 my program.cs looks like this:服务器上的各个端口是 go 的方式,在 ASP.NET Core 3.0 我的 program.cs 看起来像这样:

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

                    serverOptions.Listen(IPAddress.Loopback, 5100);

                })
                .UseStartup<Startup>();
            });
}

If you want to host two or more applications on one server如果您想在一台服务器上托管两个或更多应用程序
you need to configure nginx something like this:你需要像这样配置 nginx :

cat /etc/nginx/conf.d/domain.conf猫 /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:两个 .Net Core 应用程序的配置将如下所示:

cat /etc/systemd/system/example_prod.service猫 /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猫 /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我从一个路径启动了两个应用程序:/var/www/example/example.dll
with different environments: Production and Development不同环境:生产和开发
on different ports: localhost:5000 and localhost:5001在不同的端口上:localhost:5000 和 localhost:5001

And I confugured nginx to reverse proxy:我将 nginx 配置为反向代理:

http://localhost:5000 => http://domain/prod/
http://localhost:5001 => http://domain/dev/

You'll need a server section for each site.每个站点都需要一个服务器部分。 So if you want domain1.com to proxy to localhost:5000 and domain2.com to proxy to localhost:5001 then you create 2 server sections因此,如果您希望 domain1.com 代理到 localhost:5000 和 domain2.com 代理到 localhost:5001 那么您创建 2 个服务器部分

server {
    listen        80;
    server_name   domain1.com;
    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;
    }
}

server {
    listen        80;
    server_name   domain2.com;
    location / {
        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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM