AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 793410
Accepted
Vee
Vee
Asked: 2025-04-07 10:24:19 +0800 CST2025-04-07 10:24:19 +0800 CST 2025-04-07 10:24:19 +0800 CST

无法访问 nginx 中的子域名的静态文件

  • 772

我有一个静态网站和一个 Django 应用程序后端,使用 gunicorn 在服务器的 8000 端口上运行。

我将静态网站的 CSS 文件收集到 /mywebsite-deployment/staticDir/homepage/css/static_website.css 中,并将 Django 模板的 CSS 收集到 /mywebsite-deployment/staticDir/dashboard/css/dashboard.css 中;

我能够使用 mywebsite.com 和www.mywebsite.com以及 CSS 访问静态网站,并使用 app.mywebsite.com 访问 Django 后端,但其 CSS 无法加载。

我尝试访问的模板如下所示:

{% load static %}

<html>

<head>
    <link href={% static '/dashboard/css/dashboard.css' %} rel="stylesheet" type="text/css" media="all" />
</head>

我的 Django 应用程序 settings.py 如下所示:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'staticFiles'),
]

STATIC_ROOT = '/mywebsite-deployment/staticDir/'

STATIC_URL = 'static/'

这是我的 nginx 配置:

    ssl_certificate         /etc/letsencrypt/live/mywebsite.co/fullchain.pem;    # managed by Certbot
    ssl_certificate_key     /etc/letsencrypt/live/mywebsite.co/privkey.pem;      # managed by Certbot
    
    root            /mywebsite-deployment/staticDir/;
    index           /homepage/index.html;
    error_page 404 500 502 503 504  /error.html;
    proxy_intercept_errors on;      # If proxy errors, let nginx process it
    
    # error_log /var/log/nginx/error.log info;
    
    server {
        # If host isn't a mywebsite host, close the connection to prevent host spoofing
        server_name _;
        listen 80 default_server;
        listen 443 ssl default_server;
    
        return 444;
    }
    
    # Redirect HTTP requests to HTTPS
    server {
        server_name mywebsite.co www.mywebsite.co app.mywebsite.co;
        listen 80;
        return 301  https://$host$uri;  # managed by Certbot
    }
    
    # Handle HTTPS static webserver requests
    server {
        server_name mywebsite.co www.mywebsite.co;
        listen 443  ssl;    # managed by Certbot
    }
    
    # Proxy HTTPS app requests to backend gunicorn service
    server {
        server_name     app.mywebsite.co;
        listen 443  ssl;    # managed by Certbot
    
        location / {    # Catch all - Match everything else not matched in any above location blocks within this server block
            proxy_redirect off;    # Stop redirects from proxy
    
            proxy_connect_timeout 3;    # Abort if service is unreachable
            proxy_read_timeout 3;       # Abort if the service is unresponsive
    
            include     proxy_params;
            proxy_pass  http://localhost:8000;
        }
    
        # If Gunicorn Django proxy app throws an error (like 404), nginx will handle it and show custom error page
        location /error.html {
            internal;
        }
    }

当我在浏览器中检查模板 HTML 时,我看到它尝试加载的 URL 是: https://app.mywebsite.co/static/dashboard/css/dashboard.css,但它没有加载。

我可以按如下方式访问主页 CSS:https://www.mywebsite.co/homepage/css/static_website.css 按如下方式访问仪表板 CSS: https://www.mywebsite.co/dashboard/css/dashboard.css

如何在仪表板中使用正确的路径查看 CSS?我感觉这是 Nginx 配置问题,但我是 Nginx 新手,不知道如何解决。请帮忙。

nginx
  • 1 1 个回答
  • 33 Views

1 个回答

  • Voted
  1. Best Answer
    Continuous Improvement
    2025-04-07T13:51:04+08:002025-04-07T13:51:04+08:00

    因为我们可以通过以下方式访问主页 CSS:https://www.mywebsite.co/homepage/css/static_website.css以及通过以下方式访问仪表板 CSS https://www.mywebsite.co/dashboard/css/dashboard.css:。

    我认为 CSS 文件存储在里面,/mywebsite-deployment/staticDir/因为 nginxroot指令设置为/mywebsite-deployment/staticDir/。

    简单的修复方法是在服务器块内添加一个location专门的块/static/

    # Serve static files
    location /static/ {
        alias /mywebsite-deployment/staticDir;
    }
    

    因此server块变成

        # Proxy HTTPS app requests to backend gunicorn service
        server {
            server_name     app.mywebsite.co;
            listen 443  ssl;    # managed by Certbot
            
            # Serve static files
            location /static/ {
                alias /mywebsite-deployment/staticDir/;
            }
    
            location / {    # Catch all - Match everything else not matched in any above location blocks within this server block
                proxy_redirect off;    # Stop redirects from proxy
        
                proxy_connect_timeout 3;    # Abort if service is unreachable
                proxy_read_timeout 3;       # Abort if the service is unresponsive
        
                include     proxy_params;
                proxy_pass  http://localhost:8000;
            }
        
            # If Gunicorn Django proxy app throws an error (like 404), nginx will handle it and show custom error page
            location /error.html {
                internal;
            }
        }
    

    这种配置的一个主要好处是 Nginx 直接处理静态文件,无需通过后端服务器路由这些请求。

    • 0

相关问题

  • nginx 在 Debian 稳定版中安装失败

  • Nginx gzip_types - 在某些情况下是冗余指令?

  • Nginx 版本不可知的 php-fpm 配置

  • Nginx 位置 ~ /\.ht

  • 在 Nginx Ubuntu 14.04 上托管多个应用程序

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve