gpt4 book ai didi

django - 无法让 nginx 为收集的静态文件提供服务

转载 作者:行者123 更新时间:2023-12-05 00:42:15 25 4
gpt4 key购买 nike

我正在尝试让 nginx 与 gunicorn 一起工作。我有一个目录 /project/static/ 静态文件所在的位置。使用显示的 settings.py 配置将这些文件收集到目录 /project/livestatic/ 中:

STATIC_ROOT = '/project/livestatic'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/project/static',
)

我正在使用以下 nginx 配置:

worker_processes 1;
user nobody nogroup;
pid /tmp/nginx.pid;
error_log /tmp/nginx.error.log;

events {
worker_connections 1024;
accept_mutex off;
}

http {
include mime.types;
default_type application/octet-stream;
access_log /tmp/nginx.access.log combined;
sendfile on;

upstream app_server {
server 127.0.0.1 fail_timeout=0;
}

server {
listen 80 default;
client_max_body_size 4G;
server_name domain.org;

keepalive_timeout 5;

# path for static files
location /static/ {
autoindex on;
root /var/www/startupsearch_live/livestatic/;
}

location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

proxy_pass http://127.0.0.1:8888;
}
}
}

在开发服务器下(忽略 nginx),此配置工作正常,我可以通过以 /static/file.extension 格式链接到静态文件来提供静态文件。但是,当 nginx/gunicorn 开始发挥作用时,这不起作用,并且尝试访问 domain.org/static/ 会给出 django 404 页面,这表明 nginx 直接没有服务于文件。我怎么错了?

最佳答案

这个问题在这里被问了很多......

location /static/ {
alias /var/www/startupsearch_live/livestatic/;
}

使用 root 的方式将请求 /static/foo.jpg 解析为 /var/www/startupsearch_live/livestatic/static/foo.jpg

alias 不会将位置附加到它。它按原样一对一映射。

关于django - 无法让 nginx 为收集的静态文件提供服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12031217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com