![](/img/trans.png)
[英]Configure NGINX with Asp.Net Core Application Reverse Proxy as SubFolder like IIS
[英]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 中創建 new.conf 文件
2) 在 /var/www/ 中創建文件夾並在 .net 應用程序中上傳
3) 為每個.conf 文件創建 new.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 應用程序設置為在其上運行。
這是在.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();
每個應用程序都需要有不同的端口號,並將其反映在其 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;
}
}
希望這可以幫助。
服務器上的各個端口是 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>();
});
}
如果您想在一台服務器上托管兩個或更多應用程序
你需要像這樣配置 nginx :
貓 /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 應用程序的配置將如下所示:
貓 /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
貓 /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/
每個站點都需要一個服務器部分。 因此,如果您希望 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.