Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Nginx integration #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 MasterProject/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ RUN apt-get update && \

COPY . /django

RUN python manage.py collectstatic --noinput
RUN python manage.py collectstatic --noinput --clear

RUN chmod +x docker-entrypoint.sh

Expand Down
11 changes: 10 additions & 1 deletion MasterProject/MasterProject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('DEBUG')

ALLOWED_HOSTS = []
ALLOWED_HOSTS = os.environ.get(
'ALLOWED_HOSTS_DJANGO',
'django,localhost,127.0.0.1,0.0.0.0,[::1]'
).split(',')


# Configurations for nginx implementation

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
CSRF_TRUSTED_ORIGINS = ('localhost')


# Application definition
Expand Down
20 changes: 19 additions & 1 deletion MasterProject/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ services:
ports:
- ${DJANGO_PROD_PORT}:${DJANGO_EXPOSE_PORT}
container_name: snapspeak_server
volumes:
- staticfiles_vol:/django/staticfiles
command: sh -c "/django/docker-entrypoint.sh"

mongodb:
Expand All @@ -37,6 +39,22 @@ services:
- .env
container_name: snapspeak_celery_worker
command: celery -A MasterProject worker --loglevel=info

nginx:
depends_on:
- app
build:
context: ./nginx
dockerfile: Dockerfile
image: snapspeak-nginx:v1
container_name: snapspeak_nginx
env_file:
- .env
ports:
- ${NGINX_PROD_PORT}:${NGINX_EXPOSE_PORT}
volumes:
- staticfiles_vol:/django/staticfiles

volumes:
mongo_data:
mongo_data:
staticfiles_vol:
7 changes: 7 additions & 0 deletions MasterProject/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM nginx:1.25-alpine

RUN mkdir -p /etc/nginx/templates

COPY ./default.conf.template /etc/nginx/templates

CMD ["/bin/sh", "-c","envsubst '${DJANGO_EXPOSE_PORT} ${NGINX_EXPOSE_PORT}' < /etc/nginx/templates/default.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"]
18 changes: 18 additions & 0 deletions MasterProject/nginx/default.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
upstream django {
server app:${DJANGO_EXPOSE_PORT};
}

server {
listen ${NGINX_EXPOSE_PORT};

location / {
proxy_pass http://django;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}

location /static/ {
alias /django/staticfiles/;
}
}
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ SnapSpeak can also be deployed using Docker for easier setup and portability. Fo
REDIS_EXPOSE_PORT=6379

CELERY_BROKER_URL = 'redis://redis:6379/0'

NGINX_EXPOSE_PORT=80
NGINX_PROD_PORT=80
```

Replace `<your-secret-key>` with a securely generated secret key for your Django application. You can use online tools or Django's `django.core.management.utils.get_random_secret_key()` method to generate a new key. Make sure to keep this key confidential and never share it publicly.
Expand All @@ -152,7 +155,7 @@ SnapSpeak can also be deployed using Docker for easier setup and portability. Fo

This command will build the Docker image and start the SnapSpeak application along with its dependencies.

6. Access the SnapSpeak application by visiting [http://localhost:8001/](http://localhost:8001/) in your web browser.
6. Access the SnapSpeak application by visiting [http://localhost:80](http://localhost:80/) or [http://localhost](http://localhost/) in your web browser.

The application should now be running in a Docker container, providing a convenient and isolated environment for SnapSpeak.

Expand All @@ -164,4 +167,4 @@ SnapSpeak can also be deployed using Docker for easier setup and portability. Fo

This will stop and remove the containers.

**Note**: Ensure that port 8001 on your local machine is available and not occupied by another service before running Docker Compose. Adjust the `docker-compose.yml` file if you need to change the port configuration.
**Note**: Ensure that ports 8001 & 80 on your local machine are available and not occupied by another service before running Docker Compose. Adjust the `docker-compose.yml` file if you need to change the port configuration.