未找到django静态文件的Nginx 404文件错误

未找到django静态文件的Nginx 404文件错误,nginx,django-staticfiles,Nginx,Django Staticfiles,我在尝试访问静态文件时遇到以下错误,尽管我尝试访问static/js/main.js文件,nginx正在查找static/js/main.js/index.html。。下面是相同的错误 2016/01/20 03:31:01[错误]2410#0:*1 “/home/ubuntu/vnv/near/near/static/js/main.js/index.html”不是 找到(20:不是目录),客户端:162.111.50.122,服务器:my.domain.com,请求:“GET/static/

我在尝试访问静态文件时遇到以下错误,尽管我尝试访问
static/js/main.js
文件,
nginx
正在查找
static/js/main.js/index.html
。。下面是相同的错误

2016/01/20 03:31:01[错误]2410#0:*1 “/home/ubuntu/vnv/near/near/static/js/main.js/index.html”不是 找到(20:不是目录),客户端:162.111.50.122,服务器:my.domain.com,请求:“GET/static/js/main.js/HTTP/1.1”,主机:“my.domain.com”

下面是我的服务器块

server {
    listen 80;
    server_name my.domain.com;

    # location ~ ^/(static/.*) {
    location ~ /static {
        root /home/ubuntu/vnv/near/near; # near consist of static directory
    }

    location ~ / {
        proxy_headers_hash_max_size 51200;
        proxy_headers_hash_bucket_size 6400;
        proxy_pass http://near_server;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
此外,它还在URI的末尾添加了
/
,这里会出现什么问题


注意:我已经阅读了与此相关的大部分答案,但我找不到适合这种情况的解决方案。

如果请求URI以
/
结尾,
nginx
将其视为目录并应用索引规则。除非重写,
nginx
查找
index.html
文件

从错误消息中,我们看到GET请求是针对
/static/js/main.js/
。所以问题是谁将
/
附加到请求URI

如果现在查看访问日志,您将看到一行包含HTTP响应代码的
GET/static/js/main.js/HTTP/1.1404

如果上面的行包含
GET/static/js/main.js HTTP/1.1 3xx
,则
nginx
添加了额外的
/
。如果没有这样的线路,那么客户机要求错误的URL,您需要在其他地方寻找问题的解决方案

与您的问题无关:当前缀
location
块看起来更合适时,为什么要使用正则表达式
location

您可能应该使用:

location /static { ... }
location / { ... }
您现有的配置效率低下,并且将URI与嵌入字符串中任何位置的
/static
相匹配,例如:
/something/static.php


有关
位置
语法和处理规则,请参阅。

如果您包含html代码或尝试访问html的任何地方,都会有更好的帮助file@AlexJolig我只是想直接从客户端访问静态文件,但这里的问题是,如果我启用
自动索引
,那么它只会给我
静态
目录。对于静态目录中的剩余文件,它给了我
404
。顺便说一句,这些静态文件是my
django
项目的一部分当然
/home/ubuntu/vnv/near/near/static/js/main.js
存在吗?