在 ubuntu 和 nginx 上托管多个 ASP NET Core 站点作为反向代理
Hosting multiple ASP NET Core sites on unbuntu and nginx as reverse proxy
我正在尝试在 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 核心应用程序。
配置应该如何?
我遇到了类似的问题。
您的每个应用程序 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;
}
}
希望这对您有所帮助。
服务器上的单个端口是可行的方法,在 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>();
});
}
如果您想在一台服务器上托管两个或多个应用程序
你需要像这样配置 nginx:
cat /etc/nginx/conf.d/domain.conf
server {
listen 80;
server_name domain;
location /prod {
rewrite /prod(.*) 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(.*) 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/
我正在尝试在 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 核心应用程序。 配置应该如何?
我遇到了类似的问题。
您的每个应用程序 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;
}
}
希望这对您有所帮助。
服务器上的单个端口是可行的方法,在 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>();
});
}
如果您想在一台服务器上托管两个或多个应用程序
你需要像这样配置 nginx:
cat /etc/nginx/conf.d/domain.conf
server {
listen 80;
server_name domain;
location /prod {
rewrite /prod(.*) 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(.*) 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/