Serving static and media files on a remote server using Django and Nginx

I am setting up a server with Nginx that redirects subdomains to websites (built with Django) on remote servers (on the same local network). It works great to serve the content of each site, but I find it hard to serve static and media files (like css). Here is the content of the config file:

server {
            listen 80;
            server_name myaddress.fr

            location / {
                    proxy_pass              http://192.168.0.85:8000;
            }
}

      

And here is the end of settings.py on the Django website (which is listening on 192.168.0.85:8000):

STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'


MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'

      

Usually when my sites are on the same server than nginx, I just need to add these lines to the nginx config file:

location /media/  {
    alias /local/path/to/media/;
}

location /static/ {
    alias /local/path/to/static/;
}

      

How can I generalize this configuration to serve media and static files on a remote server (here at 192.168.0.85)? Should I install another web server on the remote machine?

Thanks in advance for your help!

+3


source to share


1 answer


You need a way to provide a route to display these files.

One way to do this is to install nginx on remote servers and proxies just like you would with the application itself.



Alternative, since you say it's all on the same local network. it would be to use something like NFS to mount static directories on a proxy so that existing nginx can serve them directly.

A similar option would be to configure the staticfiles application, as well as store the files uploaded by the user to save their files directly to the proxy.

+3


source







All Articles