【问题标题】:Hosting multiple ASP NET Core sites on unbuntu and nginx as reverse proxy在 ubuntu 和 nginx 上托管多个 ASP NET Core 站点作为反向代理
【发布时间】:2020-02-13 14:55:09
【问题描述】:

我正在尝试在 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 核心应用程序。 配置应该是什么样子?

【问题讨论】:

  • 您是否将新的 .confsites-available 链接到 sites-enabled 然后 reloading nginx
  • 是的,我将每个 .conf 文件从可用站点链接到已启用站点,为每个服务重新加载 Nginx 和 .service。
  • @S.K.你在这方面成功吗?我正在寻找类似问题stackoverflow.com/questions/66194115/… 的解决方案,并且需要一种通过代码管道线发送 nginx.conf 的方法,以便在重新发布时自动设置所有内容
  • 我有一个类似的issue...有人可以帮忙吗?

标签: linux nginx asp.net-core hosting nginx-reverse-proxy


【解决方案1】:

我也有类似的问题。

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

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

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

希望这会有所帮助。

【讨论】:

  • 这是正确答案,谢谢。我唯一错过的是在程序文件中指定 url。
  • @Jack 谢谢,这解决了我的问题
【解决方案2】:

服务器上的单独端口是要走的路,在 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>();
            });
}

【讨论】:

    【解决方案3】:

    如果您想在一台服务器上托管两个或多个应用程序
    您需要像这样配置 nginx:

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

    两个 .Net Core 应用程序的配置如下所示:

    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
    

    结论:
    我从一个路径启动了两个应用程序:/var/www/example/example.dll
    不同环境:生产和开发
    在不同的端口上:localhost:5000 和 localhost:5001

    我将 nginx 配置为反向代理:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-11
      • 2021-12-19
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      相关资源
      最近更新 更多