简体   繁体   中英

Nginx serve Django static files?

I have two EC2 instances.

I am using one to deploy Django using Gunicorn .

The other one is being used to serve the requests using Nginx which points to Gunicorn server.

Now normally I would run collectstatic and the Nginx configuration for static files would be like:

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.

But I don not know how to route my static files requests in this case where Gunicorn server is different .

Either there should be some way to collect my static files on the Nginx server itself, or route my requests to Gunicorn server.

I can use S3 to host my static files, but that is not an option as of now.

You will need to keep static files on same server which is running 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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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