Arpan3t
Arpan3t

Reputation: 71

NGINX reverse proxy to ASP.NET Core web app 404 static files

Have a basic hello world ASP.NET Core web app with the only modifications being to program.cs -> removed httpsredirect and hsts so it's set up for http.

Published to an Ubuntu server under /var/www/hello_world with static files under /var/www/hello_world/wwwroot. The app sits behind a NGINX reverse proxy to the kestrel server listening on http://127.0.0.1:5000. Everything works fine for the main endpoint, but everything else (css|js|lib|.ico) returns a 404 unless I specify the static files directory in a separate location directive:

location ~* /(css|js|lib) { root /var/www/hello_world/wwwroot; }

I've tried setting up my nginx.conf in both an upstream configuration:

    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;
}

and a straight-forward 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;
    }
}

I can see the shell info from dotnet that the directory structure is correct in the request that is getting passed to kestrel, but kestrel returns a 404 unless I add the location in the nginx.conf. Since none of the guides either from NGINX or Microsoft have this location block I'm assuming I configured something incorrectly. The way I assumed it would work is everything going to that server block with the location / would get passed to kestrel which the ASP.NET Core app would have the directory structure mapped and return the static files.

Any ideas?

Upvotes: 2

Views: 1686

Answers (2)

Santokh SIngh
Santokh SIngh

Reputation: 57

Apart from the above configuration I think I have set the working directory in my service but still my css/js files are not loading. Following is my service

WorkingDirectory=/var/www/BlissTechMind/ 

ExecStart=/usr/bin/dotnet /var/www/BlissTechMind/BlissTechMind.dll

enter image description here

Upvotes: 0

Arpan3t
Arpan3t

Reputation: 71

For anyone else that runs into this, the issue was because I was running dotnet hello_world.dll from an ssh shell in the /etc/nginx directory which in Linux makes it the working directory for that process and in turn, the content root path for the ASP.NET application. The fix is to run the dotnet hello_world.dll from the /var/www/hello_world directory or specify the working directory when making the service.

Thanks @marc_s for the edit. I'll remember to do better next question.

Upvotes: 1

Related Questions