uwsgi的Nginx缓存提供的静态文件

时间:2020-04-23 00:06:16

标签: django docker nginx docker-compose uwsgi

使用uwsgi作为应用程序服务器部署django项目,它还提供来自指定目录的静态文件(如以下命令所示),而nginx用作反向代理服务器。这是使用docker部署的。

用于运行服务器的uwsgi命令如下:

uwsgi -b 65535 --socket :4000 --workers 100 --cpu-affinity 1 --module wui.wsgi --py-autoreload 1 --static-map /static=/project/static;

此时应用程序运行正常。我想将静态文件缓存到nginx服务器中。所以我引用了博客https://www.nginx.com/blog/maximizing-python-performance-with-nginx-parti-web-serving-and-caching,并且在我的nginx.conf中包括以下配置:

location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
          |jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
          |midi|wav|bmp|rtf)$ {
   expires max;
   log_not_found off;
   access_log off;
}

将其添加到我的Nginx conf中后,Nginx服务器容器退出并出现以下错误:

[emerg] 1#1: invalid number of arguments in "location" directive in /etc/nginx/nginx.conf:43

这是uwsgi提供的静态文件可以如何缓存到nginx中吗?如果是,请告诉我这里出了什么问题。

我完整的nginx.conf如下:

events {
  worker_connections  1024;  ## Default: 1024
}

http {
    include     conf/mime.types;

    # the upstream component nginx needs to connect to
    upstream uwsgi {
        server backend:4000; # for a web port socket (we'll use this first)
    }

    # configuration of the server
    server {
        # the port your site will be served on
        listen      8443 ssl http2 default_server;

        # the domain name it will serve for
        server_name _; # substitute your machine's IP address or FQDN
        charset     utf-8;

        ssl_certificate     /secrets/server.crt;
        ssl_certificate_key /secrets/server.key;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;
        add_header Strict-Transport-Security "max-age=31536000" always;

        # Redirect HTTP to HTTPS
        error_page 497 https://$http_host$request_uri;

        # max upload size
        client_max_body_size 75M;   # adjust to taste
        uwsgi_read_timeout 600s;

        # Finally, send all non-media requests to the Django server.
        location / {
            uwsgi_pass  uwsgi;
            include     /config/uwsgi_params; # the uwsgi_params file you installed
        }

        location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
                  |jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
                  |midi|wav|bmp|rtf)$ {
        expires max;
        log_not_found off;
        access_log off;
        }
    }
}

Nginx版本:1.16

1 个答案:

答案 0 :(得分:2)

您的配置存在的问题是,位置块在文件名列表中包含换行符。我尝试对nginx -t -c <filename>进行修改,以修改您的位置限制:

        location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        expires max;
        log_not_found off;
        access_log off;
    }

...这通过了测试!

相关问题