From db282479b02504afefc1a938cba0f8bdcf89b4c8 Mon Sep 17 00:00:00 2001 From: Lukas Wingerberg Date: Sun, 22 Oct 2023 13:05:03 +0200 Subject: [PATCH] switch uvicorn and nginx to communicate via unix socket instead of tcp --- docker/Dockerfile | 4 ++-- docker/init_scripts/init | 4 ++-- docker/init_scripts/init_back | 2 +- docker/nginx/default.conf | 10 +++++++--- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 51bc2368a..7a5160d1b 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -6,7 +6,7 @@ RUN npm install RUN npm run build # Setup frontend -FROM nginx:1.24-alpine3.17 as production-stage +FROM nginx:1.24-alpine3.17-slim as production-stage ARG WEBSERVER_FOLDER=/var/www/html COPY --from=front-build-stage /front/dist ${WEBSERVER_FOLDER} COPY ./frontend/assets/default_avatar.png ${WEBSERVER_FOLDER}/assets/ @@ -31,6 +31,6 @@ COPY ./docker/init_scripts/* / COPY ./docker/nginx/default.conf /etc/nginx/nginx.conf # Expose ports and start -EXPOSE 80 +EXPOSE 8080 WORKDIR /romm CMD ["/init"] diff --git a/docker/init_scripts/init b/docker/init_scripts/init index 13db5ed53..72ab16956 100755 --- a/docker/init_scripts/init +++ b/docker/init_scripts/init @@ -1,13 +1,13 @@ #!/bin/bash # Run migrations and start uvicorn -#/init_back & +/init_back & # Start nginx /init_front & # Start rq worker -#/init_worker & +/init_worker & # Wait for any process to exit wait -n diff --git a/docker/init_scripts/init_back b/docker/init_scripts/init_back index 52eda1af5..72c3950ab 100755 --- a/docker/init_scripts/init_back +++ b/docker/init_scripts/init_back @@ -1,4 +1,4 @@ #!/bin/bash cd /back -alembic upgrade head && uvicorn main:app --proxy-headers --host 0.0.0.0 --port 5000 --workers 2 \ No newline at end of file +alembic upgrade head && uvicorn main:app --proxy-headers --host 0.0.0.0 --port 5000 --uds /tmp/uvicorn.sock --workers 2 \ No newline at end of file diff --git a/docker/nginx/default.conf b/docker/nginx/default.conf index fa6665587..8221910ba 100644 --- a/docker/nginx/default.conf +++ b/docker/nginx/default.conf @@ -32,6 +32,10 @@ http { # include /etc/nginx/conf.d/*.conf; # include /etc/nginx/sites-enabled/*; + upstream uvicorn { + server unix:/tmp/uvicorn.sock; + } + server { root /var/www/html; listen 8080; @@ -49,16 +53,16 @@ http { # OpenAPI for swagger and redoc location /openapi.json { - proxy_pass http://localhost:5000; + proxy_pass http://uvicorn; } # Backend api calls location /api { rewrite /api/(.*) /$1 break; - proxy_pass http://localhost:5000; + proxy_pass http://uvicorn; } location /ws { - proxy_pass http://localhost:5000; + proxy_pass http://uvicorn; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade";