【问题标题】:.Net Core 3.0 Nginx not serving static files.Net Core 3.0 Nginx 不提供静态文件
【发布时间】:2020-01-31 07:06:29
【问题描述】:

我有一个 .net core 3.0 Web 应用程序,我想在 Debian Buster 服务上运行。我按照微软的说明找到了Here

我能够让 Nginx 为页面提供服务,但是没有显示任何样式。

配置文件

服务器{ 听 80; server_name yourdomain.com; 返回 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;
    } 
}

我不确定我做错了什么。请把我推向正确的方向。

【问题讨论】:

    标签: c# nginx .net-core


    【解决方案1】:

    如果有人对解决方案感兴趣,我必须为静态文件明确设置一个位置块

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

    【讨论】:

    • 这个额外的位置配置会影响默认位置块中的那些资源吗?
    • @LeiYang 我的理解是它只会覆盖该位置的任何默认值。所以它不会影响任何使用默认值的东西,除非在这种情况下路由匹配“/”。
    【解决方案2】:

    问题的可能原因是您运行应用程序的工作目录没有 wwwroot 文件。注意启动时的日志。您正在寻找“内容根路径”项。

    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
    

    简单的解决方案是从正确的目录运行。

    如果您选择create a service file(强烈推荐),文档指向正确的方向。

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

    不过,您的解决方案非常好,甚至可能更好。它让 NGINX 为静态文件提供服务,让 ASP.NET 专注于生成的文件。不过,万一有人像我一样偶然发现这件事,我想确保人们知道发生了什么。

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 2021-12-02
      • 2020-09-14
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 2022-10-06
      • 1970-01-01
      相关资源
      最近更新 更多