From 9663305f1f887a71780d43dee259ad0bbc472e6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arkadiusz=20Ry=C5=9B?= Date: Sun, 23 Feb 2020 00:45:56 +0100 Subject: [PATCH] Add option to serve media files locally using nginx --- CONTRIBUTORS.rst | 2 ++ README.rst | 7 ++++--- cookiecutter.json | 1 + .../compose/production/nginx/Dockerfile | 2 ++ .../compose/production/nginx/default.conf | 8 ++++++++ .../compose/production/traefik/traefik.yml | 16 ++++++++++++++++ {{cookiecutter.project_slug}}/production.yml | 18 ++++++++++++++++++ 7 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 {{cookiecutter.project_slug}}/compose/production/nginx/Dockerfile create mode 100644 {{cookiecutter.project_slug}}/compose/production/nginx/default.conf diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 691b43ca3..5fb35fc29 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -63,6 +63,7 @@ Listed in alphabetical order. Anuj Bansal `@ahhda`_ Arcuri Davide `@dadokkio`_ Areski Belaid `@areski`_ + Arkadiusz Michał Ryś `@arkadiuszmichalrys`_ Ashley Camba Barclay Gauld `@yunti`_ Bartek `@btknu`_ @@ -233,6 +234,7 @@ Listed in alphabetical order. .. _@apirobot: https://github.com/apirobot .. _@archinal: https://github.com/archinal .. _@areski: https://github.com/areski +.. _@arkadiuszmichalrys: https://github.com/ArkadiuszMichalRys .. _@arruda: https://github.com/arruda .. _@bertdemiranda: https://github.com/bertdemiranda .. _@bittner: https://github.com/bittner diff --git a/README.rst b/README.rst index 0ae86d053..b90177613 100644 --- a/README.rst +++ b/README.rst @@ -47,7 +47,7 @@ Features * Comes with custom user model ready to go * Optional custom static build using Gulp and livereload * Send emails via Anymail_ (using Mailgun_ by default, but switchable) -* Media storage using Amazon S3 or Google Cloud Storage +* Media storage using Amazon S3, Google Cloud Storage or a local nginx instance * Docker support using docker-compose_ for development and production (using Traefik_ with LetsEncrypt_ support) * Procfile_ for deploying to Heroku * Instructions for deploying to PythonAnywhere_ @@ -85,7 +85,7 @@ Optional Integrations .. _PythonAnywhere: https://www.pythonanywhere.com/ .. _Traefik: https://traefik.io/ .. _LetsEncrypt: https://letsencrypt.org/ -.. _pre-commit: https://github.com/pre-commit/pre-commit +.. _pre-commit: https://github.com/pre-commit/pre-commit Constraints ----------- @@ -184,7 +184,8 @@ Answer the prompts with your own desired options_. For example:: Select cloud_provider: 1 - AWS 2 - GCP - 3 - None + 3 - nginx + 4 - None Choose from 1, 2, 3 [1]: 1 custom_bootstrap_compilation [n]: n Select open_source_license: diff --git a/cookiecutter.json b/cookiecutter.json index 4e77d1109..049add5fe 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -31,6 +31,7 @@ "cloud_provider": [ "AWS", "GCP", + "nginx", "None" ], "use_drf": "n", diff --git a/{{cookiecutter.project_slug}}/compose/production/nginx/Dockerfile b/{{cookiecutter.project_slug}}/compose/production/nginx/Dockerfile new file mode 100644 index 000000000..911b16f71 --- /dev/null +++ b/{{cookiecutter.project_slug}}/compose/production/nginx/Dockerfile @@ -0,0 +1,2 @@ +FROM nginx:1.17.8-alpine +COPY ./compose/production/nginx/default.conf /etc/nginx/conf.d/default.conf diff --git a/{{cookiecutter.project_slug}}/compose/production/nginx/default.conf b/{{cookiecutter.project_slug}}/compose/production/nginx/default.conf new file mode 100644 index 000000000..adb969204 --- /dev/null +++ b/{{cookiecutter.project_slug}}/compose/production/nginx/default.conf @@ -0,0 +1,8 @@ +server { + listen 80; + server_name localhost; + location /media { + autoindex on; + alias /usr/share/nginx/media/; + } +} diff --git a/{{cookiecutter.project_slug}}/compose/production/traefik/traefik.yml b/{{cookiecutter.project_slug}}/compose/production/traefik/traefik.yml index 324c62afa..86f1121c9 100644 --- a/{{cookiecutter.project_slug}}/compose/production/traefik/traefik.yml +++ b/{{cookiecutter.project_slug}}/compose/production/traefik/traefik.yml @@ -41,6 +41,16 @@ http: tls: # https://docs.traefik.io/master/routing/routers/#certresolver certResolver: letsencrypt + {%- if cookiecutter.cloud_provider == 'nginx' %} + + web-media: + rule: "Host(`{{ cookiecutter.domain_name }}`) && PathPrefix(`/media/`)" + entryPoints: + - web-secure + middlewares: + - csrf + service: django-media + {%- endif %} middlewares: redirect: @@ -59,6 +69,12 @@ http: loadBalancer: servers: - url: http://django:5000 + {%- if cookiecutter.cloud_provider == 'nginx' %} + django-media: + loadBalander: + servers: + - url: http://nginx:80 + {%- endif %} providers: # https://docs.traefik.io/master/providers/file/ diff --git a/{{cookiecutter.project_slug}}/production.yml b/{{cookiecutter.project_slug}}/production.yml index 62ec9d829..0620928cf 100644 --- a/{{cookiecutter.project_slug}}/production.yml +++ b/{{cookiecutter.project_slug}}/production.yml @@ -4,6 +4,9 @@ volumes: production_postgres_data: {} production_postgres_data_backups: {} production_traefik: {} + {%- if cookiecutter.cloud_provider == 'nginx' %} + production_django_media: {} + {%- endif %} services: django:{% if cookiecutter.use_celery == 'y' %} &django{% endif %} @@ -11,6 +14,10 @@ services: context: . dockerfile: ./compose/production/django/Dockerfile image: {{ cookiecutter.project_slug }}_production_django + {%- if cookiecutter.cloud_provider == 'nginx' %} + volumes: + - production_django_media:{{ cookiecutter.project_slug }}/media + {%- endif %} depends_on: - postgres - redis @@ -76,3 +83,14 @@ services: volumes: - production_postgres_data_backups:/backups {%- endif %} + {%- if cookiecutter.cloud_provider == 'nginx' %} + nginx: + build: + context: . + dockerfile: ./compose/production/nginx/Dockerfile + image: {{ cookiecutter.project_slug }}_local_nginx + depends_on: + - django + volumes: + - production_django_media:/usr/share/nginx/media:ro + {%- endif %}