用 Nginx 部署 ASP.NET Core 应用程序
用 Nginx 部署 ASP.NET Core 应用程序步骤如下:
- 在Linux中安装.NET Core运行时和Nginx:
a) 在Linux中打开终端。
b) 确认您的Linux发行版是否支持.NET Core并安装它。
c) 根据自己的版本安装.NET Core运行时和SDK:
sudo apt-get update
sudo apt-get install dotnet-runtime-5.0
sudo apt-get install dotnet-sdk-5.0
d) 安装Nginx:
sudo apt-get install nginx
- 创建基本的ASP.NET Core Web应用程序:
a) 创建一个新的ASP.NET Core Web应用程序:
dotnet new web -n MyAspNetCoreApp
b) 导航到应用程序目录:
cd MyAspNetCoreApp
- 构建应用程序并发布:
a) 构建应用程序:
dotnet build
b) 发布应用程序:
dotnet publish -c Release
- 配置Nginx服务器:
a) 打开Nginx默认站点配置:
sudo nano /etc/nginx/sites-available/default
b) 将以下内容添加到Nginx配置文件中:
server {
listen 80;
server_name mydomain.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;
}
}
c) 将"mydomain.com"替换为您的域名或IP地址。
- 重启Nginx:
sudo systemctl restart nginx
- 启动应用程序:
a) 转到应用程序发布目录:
cd bin/Release/net5.0/publish/
b) 启动应用程序(nohup 启动可以在断开终端时不影响程序运行):
nohup dotnet MyAspNetCoreApp.dll &
- 访问应用程序:
a) 在Web浏览器中输入您的IP地址或域名:
http://<server-ip-address-or-domain-name>
b) 您应该看到一个ASP.NET Core Web应用程序页面。
经过上述步骤后,您的ASP.NET Core应用程序应该已经部署并且可以通过 Nginx Web 服务器进行访问。