Skip to content

Commit

Permalink
Merge pull request #187 from lvalics/main
Browse files Browse the repository at this point in the history
HTTPS working version.
  • Loading branch information
davidsmithdevops committed Oct 25, 2023
2 parents b1ebadf + 625f3d0 commit 7b31deb
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
.aider.tags.cache.v1/cache.db-shm
.aider.tags.cache.v1/cache.db-wal
dj_backend_server/.aider.conf.yml
dj_backend_server/.aider.conf.yml~
dj_backend_server/nginx/ssl/privkey.pem
dj_backend_server/nginx/ssl/cert.pem
6 changes: 5 additions & 1 deletion dj_backend_server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ COPY . /app/
RUN pip install --no-cache-dir -r requirements.txt

# Run migrations on startup
CMD ["sh", "-c", "python manage.py sync_models && python manage.py runserver 0.0.0.0:8000"]
CMD ["sh", "-c", "python manage.py sync_models && python manage.py runserver 0.0.0.0:8000"]

COPY ./entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
3 changes: 0 additions & 3 deletions dj_backend_server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,14 @@ endif
# celery -A dj_backend_server worker --loglevel=info &
dev-start:
$(DOCKER_COMPOSE) -f docker-compose.linux.yaml up -d

@echo $(shell tput setaf 3)"Waiting for 20 seconds before opening the browser..."$(shell tput sgr0)
sleep 20

$(OPEN_COMMAND) http://0.0.0.0:8000/

dev-stop:
$(DOCKER_COMPOSE) down --remove-orphans
kill -9 $$(pgrep -f "celery -A dj_backend_server")
kill -9 $$(pgrep -f "python3 manage.py runserver")

@echo $$(tput setaf 3)"Services stopped."$$(tput sgr0)

force_migrate:
Expand Down
8 changes: 7 additions & 1 deletion dj_backend_server/dj_backend_server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
'django.contrib.staticfiles',
'web',
'api',
'management'
'management',
'corsheaders'
]

MIDDLEWARE = [
Expand All @@ -67,6 +68,7 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
]

ROOT_URLCONF = 'dj_backend_server.urls'
Expand Down Expand Up @@ -187,3 +189,7 @@
#]
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '0.0.0.0').split(',')
APP_URL = os.environ.get('APP_URL', 'http://0.0.0.0:8000')

CORS_ALLOWED_ORIGINS = [
APP_URL,
]
39 changes: 23 additions & 16 deletions dj_backend_server/docker-compose.linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,38 @@ services:
networks:
- openchat_network

# nginx:
# restart: unless-stopped
# container_name: oc_nginx
# build: ./docker/nginx
# ports:
# - 80:80
# - 443:443
# volumes:
# - ./app:/var/www/html
# - ./nginx/ssl:/etc/nginx/ssl
# working_dir: /etc/nginx
# links:
# - python
# networks:
# - openchat_network
nginx:
image: nginx
container_name: oc_nginx
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/ssl/cert.pem:/etc/nginx/ssl/cert.pem
- ./nginx/ssl/privkey.pem:/etc/nginx/ssl/privkey.pem
- ./static:/app/web/static/
networks:
- openchat_network
depends_on:
- qdrant
- mysql
- web
- redis
- celery_worker

web:
build:
context: .
dockerfile: Dockerfile
container_name: oc_web
ports:
- "8000:8000"
- "8001:8000"
volumes:
- ./website_data_sources:/app/website_data_sources
- ./:/app/
# - ./entrypoint.sh:/app/entrypoint.sh
# - ./llama-2-7b-chat.ggmlv3.q4_K_M.bin:/app/llama-2-7b-chat.ggmlv3.q4_K_M.bin:ro
depends_on:
mysql:
Expand All @@ -66,6 +72,7 @@ services:
condition: service_healthy
env_file:
- .env.docker
entrypoint: ["/entrypoint.sh"]
command: >
sh -c "python manage.py migrate auth &&
python manage.py migrate &&
Expand Down
25 changes: 25 additions & 0 deletions dj_backend_server/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# Define the file path as a variable
CHAT_JS_FILE="/app/web/static/chat.js"

# Actual replacement
sed -i "s|http://0.0.0.0:8000|${APP_URL}|g" $CHAT_JS_FILE

# Check if the pattern with APP_URL already exists
if grep -q '("${APP_URL}/chat/init")' $CHAT_JS_FILE; then
echo "Pattern with APP_URL already exists, doing nothing."

# Check if the pattern with the default URL exists
elif grep -q '("http://0.0.0.0:8000/chat/init")' "$CHAT_JS_FILE"; then
echo "Replacing default URL with APP_URL."
sed -i "s|http://0.0.0.0:8000|${APP_URL}|g" "$CHAT_JS_FILE"

# If none of the above conditions are met, append APP_URL to /chat/init
else
echo "Appending APP_URL to /chat/init."
sed -i "s|/chat/init|${APP_URL}/chat/init|g" "$CHAT_JS_FILE"
fi

# Start your app normally
exec "$@"
111 changes: 106 additions & 5 deletions dj_backend_server/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,135 @@ http {

server {
listen 80;
server_name your_domain.com; # Replace with your domain name or IP address

server_name yourdomain.com; # Replace with your domain name or IP address

# location /static {
# proxy_pass http://web:8000;
# alias /app/web/static;
location /static {
proxy_pass https://web:8000;
expires -1; #dev env
}

#FOR ONLY HTTP:// USE THIS
# location / {
# proxy_pass http://web:8000; # Forward requests to the Django web container
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# expires -1; #dev environment
# proxy_cache_bypass 1;
# proxy_no_cache 1;
# }

#FOR HTTPS:// USE THIS
location / {
return 301 https://$host$request_uri;
}

location /adminer {
proxy_pass http://adminer:8080; # Forward requests to the Adminer container
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

location /qdrant {
proxy_pass http://qdrant:6333; # Forward requests to the Qdrant container
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

location /qdrant-storage {
proxy_pass http://qdrant:6334; # Forward requests to the Qdrant container
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

server {
listen 443 ssl;
server_name yourdomain.com; # Replace with your domain name or IP address

ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/privkey.pem;

ssl_protocols TLSv1.2 TLSv1.3;
#ssl_prefer_server_ciphers on;
ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384';
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;

types {
text/css css;
text/html html;
}

# location /static/ {
# alias /app/web/static/; # The trailing slash is important
# # 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_ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
# expires -1; #dev
# # proxy_cache_bypass 1;
# # proxy_no_cache 1;
# add_header Cache-Control "public, max-age=2592000";
# proxy_set_header X-Forwarded-Proto $scheme;
# add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; # HSTS header
# add_header Content-Security-Policy "default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'";
# }

location /static {
proxy_pass http://web:8000;
#alias /app/web/static/;
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_ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
expires -1; #dev environment
proxy_no_cache 1; #dev environment
proxy_cache_bypass 1; #dev environment
proxy_set_header X-Forwarded-Proto $scheme; # Forward the original scheme (HTTP or HTTPS)
add_header Cache-Control "public, max-age=2592000";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; # HSTS header
add_header Content-Security-Policy "default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'";
}

location / {
proxy_pass http://web:8000; # Forward requests to the Django web container
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-Proto $scheme; # Forward the original scheme (HTTP or HTTPS)
proxy_set_header Origin ""; # Optionally forward the Origin header
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
add_header Cache-Control "public, max-age=2592000";
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; # HSTS header
expires -1; #dev environment
proxy_cache_bypass 1; #dev environment
proxy_no_cache 1; #dev environment
}

location /adminer {
proxy_pass http://adminer:8080; # Forward requests to the Adminer container
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

location /qdrant {
proxy_pass http://qdrant:6333; # Forward requests to the Qdrant container
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

location /qdrant-storage {
proxy_pass http://qdrant:6334; # Forward requests to the Qdrant container
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
Expand Down
1 change: 1 addition & 0 deletions dj_backend_server/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ urllib3==1.26.16
vine==5.0.0
wcwidth==0.2.6
yarl==1.9.2
django-cors-headers

0 comments on commit 7b31deb

Please sign in to comment.