Linux(Nginx)部署Blazor Server连接MSSQL失败及路径修改求助
我们基于Blazor Server构建内网Web应用,技术栈如下:
- VisualStudio 2022开发的Blazor Server App
- .NET 7.0
- 部署于Debian 11.6(IP:
192.168.123.230)的nginx v1.18.0 - 运行于Windows Server 2012 R2的Microsoft SQL 2014 Express(IP:
192.168.123.4,主机名powdersql01,数据库powder,已创建用户blazor)
本地运行时SQL连接功能正常,连接字符串:
Data Source=powdersql01\\POWSQL;Initial Catalog=powder;User ID=blazor;Password=password;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False
部署到Nginx后,基础页面可正常访问,但点击/getSQLdata链接时出现错误,执行sudo dotnet /var/www/test1.mydomain.local/BlazorAppNoAuth.dll的错误日志:
warn: Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer[100] Unhandled exception rendering component: Connection String 'PowderConnection' not found System.InvalidOperationException: Connection String 'PowderConnection' not found ...(省略重复栈信息)
同时应用CSS样式失效,运行日志显示应用内容根路径为/home/johndoe,而非部署目录/var/www/test1.mydomain.local:
info: Microsoft.Hosting.Lifetime[14] Now listening on: http://localhost:5000 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Production info: Microsoft.Hosting.Lifetime[0] Content root path: /home/johndoe
已通过UDL文件验证blazor用户可正常连接SQL Server,当前Nginx配置文件如下:
/etc/nginx/nginx.conf:
user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { #Blazor map $http_connection $connection_upgrade { \"~*Upgrade\" $http_connection; default keep-alive;} server { listen 80; server_name test1.mydomain.local *.test1.mydomain.local; # Configure the SignalR Endpoint location / { # App server url proxy_pass http://localhost:5000; # Configuration for WebSockets proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_cache off; # WebSockets were implemented after http/1.0 proxy_http_version 1.1; # Configuration for ServerSentEvents # proxy_buffering off; # Configuration for LongPolling or if your KeepAliveInterval is longer than 60 seconds proxy_read_timeout 100s; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } ## # Basic Settings ## sendfile on; tcp_nopush on; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }
/etc/nginx/sites-available/test1.mydomain.local.conf:
server { listen 80; listen [::]:80; server_name test1.mydomain.local; 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; } }
需求:解决SQL连接失败问题,修改Blazor Server应用的内容根路径。
一、修复内容根路径错误(解决CSS失效+配置文件读取问题)
内容根路径错误是因为启动dotnet进程时的工作目录不是部署目录,导致应用无法找到wwwroot(CSS资源)和appsettings.json(连接字符串)。
方法1:启动时指定内容根路径
执行dotnet命令时,通过--content-root参数明确指定部署目录:
sudo dotnet /var/www/test1.mydomain.local/BlazorAppNoAuth.dll --content-root /var/www/test1.mydomain.local
方法2:切换到部署目录再启动
先进入部署目录,再执行启动命令:
cd /var/www/test1.mydomain.local sudo dotnet BlazorAppNoAuth.dll
方法3:配置systemd服务(生产环境推荐)
创建持久化运行的服务文件/etc/systemd/system/blazor-test1.service:
[Unit] Description=Blazor Server Test1 App After=nginx.target [Service] Type=simple User=www-data WorkingDirectory=/var/www/test1.mydomain.local ExecStart=/usr/bin/dotnet /var/www/test1.mydomain.local/BlazorAppNoAuth.dll Restart=always RestartSec=10 [Install] WantedBy=multi-user.target
执行命令启用并启动服务:
sudo systemctl daemon-reload sudo systemctl enable blazor-test1.service sudo systemctl start blazor-test1.service
此方式会让服务在部署目录下运行,内容根路径自动指向该目录。
二、修复SQL连接字符串问题
报错Connection String 'PowderConnection' not found的核心原因是内容根路径错误导致无法读取配置文件,修复内容根路径后通常可解决。若仍存在问题,检查以下几点:
1. 确认配置文件部署正确
检查/var/www/test1.mydomain.local/appsettings.json是否存在,且包含正确的连接字符串:
{ "ConnectionStrings": { "PowderConnection": "Data Source=powdersql01\\POWSQL;Initial Catalog=powder;User ID=blazor;Password=password;Integrated Security=False;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" } }
注意:原连接字符串中Integrated Security=True是Windows身份验证,使用SQL用户blazor需改为Integrated Security=False,否则Linux环境下会优先使用进程身份导致连接失败。
2. 检查生产环境配置文件
若生产环境使用appsettings.Production.json,确认该文件中的连接字符串配置正确,生产环境会优先读取此文件。
3. 验证服务器网络连通性
在Debian服务器上测试与SQL Server的端口连通性:
telnet 192.168.123.4 1433
或使用nc命令:
nc -zv 192.168.123.4 1433
确保SQL Server的1433端口在防火墙中允许Debian服务器访问。
三、Nginx配置优化(可选)
当前Nginx存在重复的server配置(nginx.conf和sites-available中的配置),建议保留其中一个即可,避免冲突。推荐保留sites-available的配置,删除nginx.conf中的Blazor server块,然后重启Nginx:
sudo systemctl restart nginx
内容的提问来源于stack exchange,提问作者powdercoder

