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

4

使用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配置文件中添加此内容后,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


通常情况下,您会从nginx配置中的路径提供静态文件,而不是基于扩展名。对于标准的Django设置,您将接受任何针对/static/的请求,并直接从磁盘上提供collectstatic收集的文件。文档中有更多信息https://docs.djangoproject.com/en/3.0/howto/static-files/deployment/ - Iain Shelvington
可以这样做,但由于我正在使用Docker容器,即nginx和Python(Django),执行在Python容器中的collectstatic是使用uwsgi提供的,nginx将请求传递给uwsgi。因此,静态文件存在于Python容器中。 - rakesh kotian
1
即使您在Docker容器中,最好通过卷将这些静态文件暴露给Nginx,以便整个静态目录直接由Nginx提供服务。 - SebCorbin
1个回答

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;
    }

...而且这通过了测试!


在使用上述代码块更新nginx.conf文件后,错误信息不再出现,但是我的UI界面无法加载静态文件,请问有什么建议吗? - rakesh kotian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接