【问题标题】:NGINX reverse proxy to ASP.NET Core web app 404 static filesNGINX 反向代理到 ASP.NET Core Web 应用程序 404 静态文件
【发布时间】: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


    【解决方案1】:

    对于遇到此问题的其他任何人,问题是因为我正在运行dotnet hello_world.dll 来自/etc/nginx 目录中的 ssh shell,该目录在 Linux 中使其成为该进程的工作目录,进而成为 ASP.NET 应用程序的内容根路径。修复方法是从/var/www/hello_world 目录运行dotnet hello_world.dll 或在制作服务时指定工作目录。

    感谢@marc_s 的编辑。我会记得下一个问题做得更好。

    【讨论】:

      猜你喜欢
      • 2016-08-19
      • 2018-08-04
      • 1970-01-01
      • 2021-08-25
      • 2016-11-26
      • 2019-09-23
      • 2017-09-09
      • 2019-04-17
      • 2023-04-09
      相关资源
      最近更新 更多