ASP.NET Core 部署 Linux 服务器:Nginx 反向代理与 SSL 证书配置
ASP.NET Core 部署 Linux 服务器:Nginx 反向代理与 SSL 证书配置
在本指南中,我将一步步解释如何在 Linux 服务器(如 Ubuntu 20.04)上部署 ASP.NET Core 应用程序,使用 Nginx 作为反向代理,并配置 SSL 证书以实现 HTTPS 加密。Nginx 反向代理能处理静态内容、负载均衡,并提高安全性;SSL 证书则确保数据传输安全。整个过程基于真实可靠的最佳实践,假设您已具备基本的 Linux 命令行知识。以下是详细步骤:
前提条件
在开始前,确保满足以下条件:
- Linux 服务器(本指南以 Ubuntu 20.04 为例)。
- 已安装 ASP.NET Core 运行时或 SDK(版本 6.0 或更高)。
- 服务器拥有公网 IP 和域名(用于 SSL 证书)。
- 基本工具:
sudo权限、curl、systemd。 - 端口开放:$80$(HTTP)和 $443$(HTTPS)。
步骤 1: 部署 ASP.NET Core 应用
首先,将您的应用发布到服务器并设置为后台服务。
-
发布应用:
- 在开发机,使用 .NET CLI 发布应用(例如 Release 配置):
dotnet publish -c Release -o ./publish - 将
publish文件夹上传到服务器(如/var/www/myapp),使用 SCP 或 FTP:scp -r ./publish user@your-server-ip:/var/www/myapp
- 在开发机,使用 .NET CLI 发布应用(例如 Release 配置):
-
创建 systemd 服务:
- 在服务器上,创建服务文件(如
/etc/systemd/system/kestrel-myapp.service):[Unit] Description=My ASP.NET Core App running on Kestrel [Service] WorkingDirectory=/var/www/myapp ExecStart=/usr/bin/dotnet /var/www/myapp/YourApp.dll Restart=always RestartSec=10 SyslogIdentifier=dotnet-myapp User=www-data Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target - 启用并启动服务:
sudo systemctl enable kestrel-myapp.service sudo systemctl start kestrel-myapp.service sudo systemctl status kestrel-myapp.service # 验证运行状态 - 应用默认运行在端口 $5000$(HTTP),可通过
curl http://localhost:5000测试。
- 在服务器上,创建服务文件(如
步骤 2: 配置 Nginx 反向代理
Nginx 将作为前端代理,转发请求到 Kestrel。
-
安装 Nginx:
sudo apt update sudo apt install nginx -y -
配置反向代理:
- 创建 Nginx 配置文件(如
/etc/nginx/sites-available/myapp):server { listen 80; server_name your-domain.com www.your-domain.com; # 替换为您的域名 location / { proxy_pass http://localhost:5000; # 转发到 Kestrel 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; } } - 启用配置并测试:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/ sudo nginx -t # 检查语法 sudo systemctl reload nginx - 访问
http://your-domain.com应显示您的应用。
- 创建 Nginx 配置文件(如
步骤 3: 配置 SSL 证书
使用 Let's Encrypt 免费证书自动配置 HTTPS。
-
安装 Certbot:
sudo apt install certbot python3-certbot-nginx -y -
获取并安装证书:
- 运行 Certbot 自动配置:
sudo certbot --nginx -d your-domain.com -d www.your-domain.com- 按照提示输入邮箱并同意条款,Certbot 会自动修改 Nginx 配置。
- 验证证书续订(可选):
sudo certbot renew --dry-run # 测试自动续订
- 运行 Certbot 自动配置:
-
手动配置(如果 Certbot 失败):
- 上传自定义证书(如
fullchain.pem和privkey.pem)到/etc/ssl/certs/。 - 修改 Nginx 配置文件,添加 SSL 部分:
server { listen 443 ssl; server_name your-domain.com www.your-domain.com; ssl_certificate /etc/ssl/certs/fullchain.pem; ssl_certificate_key /etc/ssl/certs/privkey.pem; ssl_protocols TLSv1.2 TLSv1.3; location / { proxy_pass http://localhost:5000; # ... 其他代理设置同步骤 2 } } server { listen 80; server_name your-domain.com www.your-domain.com; return 301 https://$host$request_uri; # HTTP 重定向到 HTTPS } - 重载 Nginx:
sudo nginx -t sudo systemctl reload nginx
- 上传自定义证书(如
测试和验证
- 访问
https://your-domain.com,浏览器应显示锁图标(表示 HTTPS 有效)。 - 使用
curl -I https://your-domain.com检查响应头。 - 监控日志:
- Nginx 日志:
/var/log/nginx/access.log和error.log。 - Kestrel 日志:
journalctl -u kestrel-myapp.service。
- Nginx 日志:
注意事项
- 安全优化:定期更新证书(Certbot 自动续订),配置防火墙(如 UFW)只允许端口 $80$ 和 $443$。
- 性能:Nginx 可缓存静态文件,减轻 Kestrel 负载;调整
proxy_buffer_size避免大请求问题。 - 错误排查:
- 如果应用不可达,检查 Kestrel 服务状态和端口绑定。
- SSL 错误时,使用
openssl s_client -connect your-domain.com:443诊断证书链。
- 扩展性:支持多实例部署时,在 Nginx 中添加
upstream块实现负载均衡。
通过以上步骤,您的 ASP.NET Core 应用将在 Linux 服务器上安全高效运行。如有问题,参考官方文档:ASP.NET Core 部署指南 和 Certbot 文档。









