我试图在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核心应用程序。 配置看起来如何?
答案 0 :(得分:2)
我有一个类似的问题。
每个应用程序的nginx配置文件应指向.Net Core应用程序设置为运行的正确端口号。
这是由program.cs
扩展名中的每个.Net Core应用程序.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;
}
}
希望这会有所帮助。
答案 1 :(得分:0)
要使用服务器上的单个端口,在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>();
});
}
答案 2 :(得分:0)
如果您想在一台服务器上托管两个或多个应用程序
你需要像这样配置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/