Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use nginx as reverse proxy in production #50

Merged
merged 12 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .envs/.production/.django
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# DJANGO_READ_DOT_ENV_FILE=True
DJANGO_SETTINGS_MODULE=config.settings.production
DJANGO_ADMIN_URL=admin
DJANGO_ALLOWED_HOSTS=osler.umkc.edu
DJANGO_ALLOWED_HOSTS=localhost

# Security
# ------------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions compose/production/django/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ RUN chmod +x /start
RUN chown django /start
COPY --chown=django:django . /app

RUN mkdir -p /app/staticfiles
RUN chown django:django /app/staticfiles

USER django

WORKDIR /app
Expand Down
4 changes: 4 additions & 0 deletions compose/production/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM nginx:1.19.0-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
38 changes: 38 additions & 0 deletions compose/production/nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# nginx.conf

upstream app {
server django:5000;
}

server {
# use 'listen 80 deferred;' for Linux
# use 'listen 80 accept_filter=httpready;' for FreeBSD
listen 80;
charset utf-8;

# Handle noisy favicon.ico messages in nginx
location = /favicon.ico {
return 204;
access_log off;
log_not_found off;
}

location / {
# checks for static file, if not found proxy to app
try_files $uri @proxy_to_app;
}

location /static/ {
alias /app/static/;
}

# django app
location @proxy_to_app {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_pass http://app;
}
}
29 changes: 15 additions & 14 deletions production.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
version: '3'
version: "3"

volumes:
production_postgres_data: {}
production_postgres_data_backups: {}
production_traefik: {}
production_static_files: {}

services:
django:
Expand All @@ -17,7 +17,14 @@ services:
env_file:
- ./.envs/.production/.django
- ./.envs/.production/.postgres
- ./.envs/.production/.secrets
command: /start
# ports:
# - 5000:5000
expose:
- 5000
volumes:
- production_static_files:/app/staticfiles

postgres:
build:
Expand All @@ -28,22 +35,16 @@ services:
- production_postgres_data:/var/lib/postgresql/data
- production_postgres_data_backups:/backups
env_file:
- ./.envs/.production/.postgres
- ./.envs/.production/.secrets

traefik:
build:
context: .
dockerfile: ./compose/production/traefik/Dockerfile
image: osler_production_traefik
nginx:
build: ./compose/production/nginx
ports:
- 1337:80
depends_on:
- django
volumes:
- production_traefik:/etc/traefik/acme
ports:
- "0.0.0.0:8084:80"
- "0.0.0.0:443:443"
- production_static_files:/app/static

redis:
image: redis:5.0