Skip to content

Commit

Permalink
add nginx to the mix
Browse files Browse the repository at this point in the history
  • Loading branch information
tobru committed May 31, 2024
1 parent 03faa7d commit 7ad5fa1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ RUN --mount=type=cache,target=$POETRY_CACHE_DIR poetry install --no-root
### RUNTIME
FROM python:3.12-bookworm as runtime

RUN apt-get update && \
apt-get install -y nginx && \
rm -rf /var/lib/apt/lists/*

ENV VIRTUAL_ENV=/app/.venv \
PATH="/app/.venv/bin:$PATH" \
PYTHONPATH="/contactform" \
GUNICORN_CMD_ARGS="--workers=1 --bind=0.0.0.0:8000 --access-logfile=-"
GUNICORN_CMD_ARGS="--workers=1 --bind=unix:/app/gunicorn.sock --access-logfile=-"

COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}

COPY contactform ./contactform
COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 8000
EXPOSE 8080

CMD ["gunicorn", "contactform.app:app"]
CMD ["sh", "-c", "nginx && gunicorn contactform.app:app"]
48 changes: 48 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
worker_processes 1;

user nobody nogroup;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

error_log /dev/stdout info;

events {
worker_connections 1024;
accept_mutex off;
}

http {
include mime.types;

default_type application/octet-stream;
access_log /dev/stdout combined;
sendfile on;

upstream app_server {
server unix:/app/gunicorn.sock fail_timeout=0;
}

server {
listen 8080 deferred default_server;
client_max_body_size 4G;

keepalive_timeout 5;

# path for static files
root /contactform;

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

location @proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
}

0 comments on commit 7ad5fa1

Please sign in to comment.