Django / Nginx:在生产中提供静态文件

时间:2019-05-22 05:32:06

标签: django nginx

我正在尝试使用NGINX服务器在Django应用程序中提供CSS和其他静态文件。所以我尝试配置它。这是我的/etc/nginx/sites-enabled/mydomain

server {
    listen 80;
    server_name redpillers.net;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/limited/REDPILLERS;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/limited/REDPILLERS/redpillers.sock;
    }

    location /static/ {
       alias /home/limited/REDPILLERS/static/;
    }

}

但是当我重新启动服务时,错误日志文件中出现了错误:

2019/05/22 07:26:44 [emerg] 11589#11589: duplicate location "/static/" in /etc/nginx/sites-enabled/mydomain:15

2 个答案:

答案 0 :(得分:1)

server {
    listen 80;
    server_name redpillers.net;

    location = /favicon.ico { access_log off; log_not_found off; }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/limited/REDPILLERS/redpillers.sock;
    }

    location /static/ {
       alias /home/limited/REDPILLERS/static/;
    }

}

您已两次配置location /static/。只需删除一个即可。

答案 1 :(得分:0)

请移除第二个/static/位置并保留第一个位置 因此,最终的配置:

server {
    listen 80;
    server_name redpillers.net;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/limited/REDPILLERS;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/limited/REDPILLERS/redpillers.sock;
    }

}