在Linux Nginx上的子目录中托管多个.NET Core Web应用程序静态文件未加载(CSS,JS等)

fv2wmkja  于 2023-06-21  发布在  Nginx
关注(0)|答案(1)|浏览(110)

我有几个.NET Core应用程序(.NET Core 7),我想在自己的子目录(subdomain.domain.co.za/API,subdomain.domain.co.za/auth和subdomain.domain.co.za/proxy)中托管。我一直在试图弄清楚为什么静态文件(托管在wwwroot文件夹中的css,js文件)会返回HTTP 404。从控制台,我看到Web应用程序试图从根目录加载资源,我不明白,因为文件都在每个子目录中。我在Linux Ubuntu 22.04 VPS上托管所有内容。
下面是我的3个应用程序的nginx服务器块文件:

server {
        server_name subdomain.domain.co.za www.subdomain.domain.co.za;
        error_log /var/apps/domain.co.za/logs/domain.log;
        
        root /var/www/subdomain.domain.co.za;

        location /proxy/ {
         root /var/www/subdomain.domain.co.za/proxy;
         proxy_pass         http://localhost:8886/;
         proxy_http_version 1.1;
         proxy_set_header   Upgrade $http_upgrade;
         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;
        }

        location /auth/ {
         root /var/www/subdomain.domain.co.za/auth;
         proxy_pass         http://localhost:8888/;
         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;  
        }
    
        location /api/ {
         root /var/www/subdomain.domain.co.za/api;
         proxy_pass         http://localhost:8887/;
         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;  
        }
        
        location / {
         root /var/www/subdomain.domain.co.za;
         index index.html;
        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/subdomain.domain.co.za/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/subdomain.domain.co.za/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}server {
    if ($host = www.subdomain.domain.co.za) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = subdomain.domain.co.za) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

        
        server_name subdomain.domain.co.za www.subdomain.domain.co.za;
    listen 80;
    return 404; # managed by Certbot

在其中一个应用程序(auth应用程序)中,我试图明确说明请求路径,以及在哪里可以找到css,js相关文件:

app.UseStaticFiles(new StaticFileOptions()
        {
            FileProvider = new PhysicalFileProvider("/var/www/subdomain.domain.co.za/auth/wwwroot"),
            RequestPath = "/auth"
        });
        app.UsePathBase("/auth");

这是显示在开发工具控制台部分的内容:

我不知道我还能尝试什么,或者这里会发生什么。

k2arahey

k2arahey1#

首先,你不需要为你的. net核心应用程序配置根路径。它甚至可能在你的Docker容器中。我们正在通过http访问文件。不是从nginx中的静态路径。

location /auth/ {
     proxy_pass         http://localhost:8888/;
     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;  
    }

其次,您已经使用UsePathBase方法设置了路径前缀。没有必要复制。我假设你的wwwroot已经在你的认证服务。所以你也不必指定物理路径。

app.UseStaticFiles();
app.UsePathBase("/auth");

让我知道它是否有效,或者我的假设是错误的。

相关问题