简体   繁体   中英

.Net Core 3.0 Nginx not serving static files

I have a .net core 3.0 web application that I want to run on a Debian Buster service. I followed the Microsoft instructions found Here .

I was able to get Nginx to serve the pages however none of the styles are showing up.

Config file

server { listen 80; server_name yourdomain.com; return 301 https://$host$request_uri; }

server {
    listen 80;
    server_name demo.cerebral.local;

    #ssl_certificate           /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    #ssl_certificate_key       /etc/letsencrypt/live/yourdomain.com/privkey.pem;

    #ssl on;
    #ssl_session_cache  builtin:1000  shared:SSL:10m;
    #ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    #ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    #ssl_prefer_server_ciphers on;

    gzip  on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_proxied any;
    #gzip_types text/plain text/html text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;
    gzip_buffers 16 8k;
    gzip_disable “MSIE [1-6].(?!.*SV1)”;

    #access_log  /var/log/nginx/demo.access.log;

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

I am not sure what I am doing wrong. Please push me in the right direction.

If anyone is interested in the solution I had to explicitly set a location block for the static files

server {
    listen 80;
    server_name demo.cerebral.local;

    #ssl_certificate           /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    #ssl_certificate_key       /etc/letsencrypt/live/yourdomain.com/privkey.pem;

    #ssl on;
    #ssl_session_cache  builtin:1000  shared:SSL:10m;
    #ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    #ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    #ssl_prefer_server_ciphers on;

    gzip  on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_proxied any;
    #gzip_types text/plain text/html text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;
    gzip_buffers 16 8k;
    gzip_disable “MSIE [1-6].(?!.*SV1)”;

    #access_log  /var/log/nginx/demo.access.log;

    # This location block fixed my issue.
    location ~* /(css|js|lib) {
        root /var/www/demo/wwwroot;
    }

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

the likely reason for the problem is that the working directory where you're running the application doesn't have the wwwroot files. Pay attention to the logs on startup. You're looking for the "content root path" item.

Jan 04 11:14:19 nero dashboard-iot[54792]: info: Microsoft.Hosting.Lifetime[0]
Jan 04 11:14:19 nero dashboard-iot[54792]:       Now listening on: http://localhost:5000
Jan 04 11:14:19 nero dashboard-iot[54792]: info: Microsoft.Hosting.Lifetime[0]
Jan 04 11:14:19 nero dashboard-iot[54792]:       Application started. Press Ctrl+C to shut down.
Jan 04 11:14:19 nero dashboard-iot[54792]: info: Microsoft.Hosting.Lifetime[0]
Jan 04 11:14:19 nero dashboard-iot[54792]:       Hosting environment: Production
Jan 04 11:14:19 nero dashboard-iot[54792]: info: Microsoft.Hosting.Lifetime[0]
Jan 04 11:14:19 nero dashboard-iot[54792]:       Content root path: /var/www/iotui

Simple solution is to run from the right directory.

If you choose to create a service file , which is highly recommended, the docs point in the right direction.

[Service]
WorkingDirectory=/var/www/helloapp
ExecStart=/usr/bin/dotnet /var/www/helloapp/helloapp.dll

Nonetheless, your solution is perfectly fine, and perhaps even better. It's letting NGINX serve the static files and letting ASP.NET focus on the generated files. Still, in case anyone stumbles across this like I did, I wanted to be sure folks know what's going on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM