NGINX for static files for dev python server

When you work on the backend part of django or flask project and there are many static files, sometimes the development server becomes slow. In this case it’s possible to use nginx as reverse proxy to serve static. I’m using nginx in docker and the configuration is quite simple.

Put in some directory Dockerfile and default.conf.tmpl.

Dockerfile

FROM nginx:1.9
VOLUME /static
COPY default.conf.tmpl /etc/nginx/conf.d/default.conf.tmpl
EXPOSE 9000 
CMD envsubst '$APP_IP $APP_PORT' < /etc/nginx/conf.d/default.conf.tmpl > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'

default.conf.tmpl

server {
    listen 9000;
    charset utf-8;

    location /site_media {
        alias /static;
    }

    location / {
        proxy_pass http://${APP_IP}:${APP_PORT};
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Build image with docker build -t dev-nginx . command.

To run it:

docker run --rm -it -v `pwd`/static:/static -p 9000:9000 -e APP_IP=<your ip from ifconfig> -e APP_PORT=8000 dev-nginx

Then you can access your development server though http://<localhost|docker-machine-ip>:9000.

comments powered by Disqus