linux - 在 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 核心应用程序。配置应该如何?
解决方案
我有一个类似的问题。
您的每个应用程序 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;
}
}
希望这可以帮助。
推荐阅读
- python - AttributeError:“NoneType”对象没有属性“sink”
- python - 类型错误:'在
' 需要字符串作为左操作数,而不是元组。错误 - python - 如何将带有逻辑运算符的字符串与字符串列表进行比较?
- linux - 使用 ExecStart /bin/sh 运行 systemd 服务失败
- google-cloud-platform - 访问 Google Cloud DataStore 的 Windows 应用
- android - Android Studio - 在不打开片段的情况下添加回栈
- gradle - 将 Spring Boot 项目添加到本地 Maven 存储库中
- csom - 如果企业资源池包含超过 1000 个资源,则 Project CSOM 无法添加新任务请求
- python - 使用opencv python读取模拟时钟图像并显示时间
- java - 如何在 Android 中设置 cookie 然后加载 url 以便该 url 域服务器读取 cookie