简体   繁体   English

Nginx提供Django静态文件吗?

[英]Nginx serve Django static files?

I have two EC2 instances. 我有两个EC2实例。

I am using one to deploy Django using Gunicorn . 我正在使用Gunicorn部署Django。

The other one is being used to serve the requests using Nginx which points to Gunicorn server. 另一个用于使用Nginx服务,该服务指向Gunicorn服务器。

Now normally I would run collectstatic and the Nginx configuration for static files would be like: 现在通常我将运行collectstatic,并且静态文件的Nginx配置将如下所示:

server {
    listen 80;
    server_name server_domain_or_IP;

    location /static/ {
        # my static files directory
        root /home/user/myproject;
    }

   # other locations here
}

This configuration works well when both Nginx and Gunicorn are deployed on same server. 当Nginx和Gunicorn都部署在同一服务器上时,此配置效果很好。

But I don not know how to route my static files requests in this case where Gunicorn server is different . 但是在Gunicorn服务器不同的情况下,我不知道如何路由我的静态文件请求。

Either there should be some way to collect my static files on the Nginx server itself, or route my requests to Gunicorn server. 应该有某种方法可以在Nginx服务器本身上收集我的静态文件,或者将我的请求路由到Gunicorn服务器。

I can use S3 to host my static files, but that is not an option as of now. 我可以使用S3托管我的静态文件,但是到目前为止,这不是一个选项。

You will need to keep static files on same server which is running Nginx. 您需要将静态文件保存在运行Nginx的同一服务器上。 So the best option should be to track your Django project on both the servers (also run collectstatic on both the servers although running it on Gunicorn server wouldn't be necessary). 因此,最好的选择应该是在两个服务器上都跟踪您的Django项目(也可以在两个服务器上同时运行collectstatic,尽管没有必要在Gunicorn服务器上运行它)。 So Nginx will keep serving static files from Django static files on local server which it is running on and for other requests it will forward requests to Gunicorn server. 因此,Nginx将继续在其运行的本地服务器上从Django静态文件中提供静态文件,对于其他请求,它将转发请求到Gunicorn服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM