Home:ALL Converter>Serving static and media files on a distant server with Django and Nginx

Serving static and media files on a distant server with Django and Nginx

Ask Time:2017-04-20T23:01:59         Author:Arthur Pesah

Json Formatter

I am configuring a server with Nginx that redirect subdomains to websites (made with Django) on distant servers (on the same local network). It is working fine to serve the content of each site, but I have trouble to serve static and media files (for instance css). Here is the content of the configuration 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 in the Django website (which listen at 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 websites are on the same server than nginx, I just have to add these lines to the nginx conf file:

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

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

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

Thanks in advance for your help!

Author:Arthur Pesah,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43523208/serving-static-and-media-files-on-a-distant-server-with-django-and-nginx
yy