Add option to serve media files locally using nginx

This commit is contained in:
Arkadiusz Ryś 2020-02-23 00:45:56 +01:00
parent b1fb6ed2d0
commit 9663305f1f
No known key found for this signature in database
GPG Key ID: 8557F612E405EF84
7 changed files with 51 additions and 3 deletions

View File

@ -63,6 +63,7 @@ Listed in alphabetical order.
Anuj Bansal `@ahhda`_ Anuj Bansal `@ahhda`_
Arcuri Davide `@dadokkio`_ Arcuri Davide `@dadokkio`_
Areski Belaid `@areski`_ Areski Belaid `@areski`_
Arkadiusz Michał Ryś `@arkadiuszmichalrys`_
Ashley Camba Ashley Camba
Barclay Gauld `@yunti`_ Barclay Gauld `@yunti`_
Bartek `@btknu`_ Bartek `@btknu`_
@ -233,6 +234,7 @@ Listed in alphabetical order.
.. _@apirobot: https://github.com/apirobot .. _@apirobot: https://github.com/apirobot
.. _@archinal: https://github.com/archinal .. _@archinal: https://github.com/archinal
.. _@areski: https://github.com/areski .. _@areski: https://github.com/areski
.. _@arkadiuszmichalrys: https://github.com/ArkadiuszMichalRys
.. _@arruda: https://github.com/arruda .. _@arruda: https://github.com/arruda
.. _@bertdemiranda: https://github.com/bertdemiranda .. _@bertdemiranda: https://github.com/bertdemiranda
.. _@bittner: https://github.com/bittner .. _@bittner: https://github.com/bittner

View File

@ -47,7 +47,7 @@ Features
* Comes with custom user model ready to go * Comes with custom user model ready to go
* Optional custom static build using Gulp and livereload * Optional custom static build using Gulp and livereload
* Send emails via Anymail_ (using Mailgun_ by default, but switchable) * 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) * Docker support using docker-compose_ for development and production (using Traefik_ with LetsEncrypt_ support)
* Procfile_ for deploying to Heroku * Procfile_ for deploying to Heroku
* Instructions for deploying to PythonAnywhere_ * Instructions for deploying to PythonAnywhere_
@ -85,7 +85,7 @@ Optional Integrations
.. _PythonAnywhere: https://www.pythonanywhere.com/ .. _PythonAnywhere: https://www.pythonanywhere.com/
.. _Traefik: https://traefik.io/ .. _Traefik: https://traefik.io/
.. _LetsEncrypt: https://letsencrypt.org/ .. _LetsEncrypt: https://letsencrypt.org/
.. _pre-commit: https://github.com/pre-commit/pre-commit .. _pre-commit: https://github.com/pre-commit/pre-commit
Constraints Constraints
----------- -----------
@ -184,7 +184,8 @@ Answer the prompts with your own desired options_. For example::
Select cloud_provider: Select cloud_provider:
1 - AWS 1 - AWS
2 - GCP 2 - GCP
3 - None 3 - nginx
4 - None
Choose from 1, 2, 3 [1]: 1 Choose from 1, 2, 3 [1]: 1
custom_bootstrap_compilation [n]: n custom_bootstrap_compilation [n]: n
Select open_source_license: Select open_source_license:

View File

@ -31,6 +31,7 @@
"cloud_provider": [ "cloud_provider": [
"AWS", "AWS",
"GCP", "GCP",
"nginx",
"None" "None"
], ],
"use_drf": "n", "use_drf": "n",

View File

@ -0,0 +1,2 @@
FROM nginx:1.17.8-alpine
COPY ./compose/production/nginx/default.conf /etc/nginx/conf.d/default.conf

View File

@ -0,0 +1,8 @@
server {
listen 80;
server_name localhost;
location /media {
autoindex on;
alias /usr/share/nginx/media/;
}
}

View File

@ -41,6 +41,16 @@ http:
tls: tls:
# https://docs.traefik.io/master/routing/routers/#certresolver # https://docs.traefik.io/master/routing/routers/#certresolver
certResolver: letsencrypt 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: middlewares:
redirect: redirect:
@ -59,6 +69,12 @@ http:
loadBalancer: loadBalancer:
servers: servers:
- url: http://django:5000 - url: http://django:5000
{%- if cookiecutter.cloud_provider == 'nginx' %}
django-media:
loadBalander:
servers:
- url: http://nginx:80
{%- endif %}
providers: providers:
# https://docs.traefik.io/master/providers/file/ # https://docs.traefik.io/master/providers/file/

View File

@ -4,6 +4,9 @@ volumes:
production_postgres_data: {} production_postgres_data: {}
production_postgres_data_backups: {} production_postgres_data_backups: {}
production_traefik: {} production_traefik: {}
{%- if cookiecutter.cloud_provider == 'nginx' %}
production_django_media: {}
{%- endif %}
services: services:
django:{% if cookiecutter.use_celery == 'y' %} &django{% endif %} django:{% if cookiecutter.use_celery == 'y' %} &django{% endif %}
@ -11,6 +14,10 @@ services:
context: . context: .
dockerfile: ./compose/production/django/Dockerfile dockerfile: ./compose/production/django/Dockerfile
image: {{ cookiecutter.project_slug }}_production_django image: {{ cookiecutter.project_slug }}_production_django
{%- if cookiecutter.cloud_provider == 'nginx' %}
volumes:
- production_django_media:{{ cookiecutter.project_slug }}/media
{%- endif %}
depends_on: depends_on:
- postgres - postgres
- redis - redis
@ -76,3 +83,14 @@ services:
volumes: volumes:
- production_postgres_data_backups:/backups - production_postgres_data_backups:/backups
{%- endif %} {%- 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 %}