首页 > 解决方案 > 在 unbuntu 和 nginx 上托管多个 ASP NET Core 站点作为反向代理

问题描述

我正在尝试在 Linux、Unbunt 18.04 上托管多个具有不同域的 ASP NET Core 站点,并使用 nginx 作为反向代理。

这些是步骤:

1) 在 /etc/nginx/sites-available 中创建新的 .conf 文件

2) 在 /var/www/ 中创建文件夹并在 .net 应用程序中上传

3) 为每个 .conf 文件创建新的 .service 文件

默认的 nginx .conf 没有改变。

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

.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

使用这种配置,即使我部署了几个站点,它们都被重定向到相同的内容。我的目标是在同一台服务器上托管多个 .net 核心应用程序。配置应该如何?

标签: linuxnginxasp.net-corehostingnginx-reverse-proxy

解决方案


我有一个类似的问题。

您的每个应用程序 nginx 配置文件都应指向 .Net Core 应用程序设置为在其上运行的正确端口号。

program.cs这是在.UseUrls()扩展中的每个 .Net Core 应用程序中确定的,例如

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

每个应用程序都需要有不同的端口号,并将其反映在其 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;
    }
}

希望这可以帮助。


推荐阅读