【发布时间】:2022-10-04 22:20:35
【问题描述】:
有一个基本的 hello world ASP.NET Core Web 应用程序,唯一的修改是 program.cs -> 删除了 httpsredirect 和 hsts,因此它是为 http 设置的。
发布到/var/www/hello_world 下的 Ubuntu 服务器,静态文件位于/var/www/hello_world/wwwroot 下。该应用程序位于一个 NGINX 反向代理后面,用于侦听 http://127.0.0.1:5000 的 kestrel 服务器。主端点一切正常,但其他一切(css|js|lib|.ico)返回 404,除非我在单独的位置指令中指定静态文件目录:
location ~* /(css|js|lib) { root /var/www/hello_world/wwwroot; }
我尝试在上游配置中设置我的nginx.conf:
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
ssl_certificate /etc/ssl/certs/hello_world.pem;
ssl_certificate_key /etc/ssl/private/hello_world.key;
location / {
proxy_pass http://dotnet;
proxy_set_header Host $host;
}
}
upstream dotnet {
zone dotnet 64k;
server 127.0.0.1:5000;
}
和一个直接的proxy_pass:
server {
listen 443 ssl;
server_name helloworld.com;
ssl_certificate /etc/ssl/certs/hello_world.pem;
ssl_certificate_key /etc/ssl/private/hello_world.key;
ssl_dhparam /etc/nginx/dhparam.pem;
location / {
proxy_pass http://127.0.0.1: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;
}
# returns 404 for static files unless I have this
location ~* /(css|js|lib|ico) {
root /var/www/hello_world/wwwroot;
}
}
我可以从 dotnet 看到 shell 信息,表明传递给 kestrel 的请求中的目录结构是正确的,但 kestrel 返回 404,除非我在 nginx.conf 中添加位置。由于 NGINX 或 Microsoft 的指南都没有此位置块,我假设我配置错误。我认为它会起作用的方式是,所有到达该服务器块的位置 / 都会传递给 kestrel,ASP.NET Core 应用程序将映射目录结构并返回静态文件。
有任何想法吗?
【问题讨论】:
标签: nginx-reverse-proxy kestrel-http-server asp.net-core-6.0