Skip to content

Commit 00f7858

Browse files
committed
Add SSL
1 parent ef55501 commit 00f7858

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

compose.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ services:
3333
start_period: 40s
3434

3535
nginx:
36-
image: nginx:alpine
36+
build:
37+
context: ./server
38+
dockerfile: Dockerfile.nginx
3739
container_name: aft-web
3840
restart: unless-stopped
3941
depends_on:
4042
server:
4143
condition: service_healthy
4244
ports:
4345
- "80:80"
46+
- "443:443"
4447
volumes:
4548
- ./www:/usr/share/nginx/html
4649
- ./server/nginx.conf:/etc/nginx/conf.d/default.conf

server/Dockerfile.nginx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM nginx:alpine
2+
3+
# Install openssl for generating self-signed certificates
4+
RUN apk add --no-cache openssl
5+
6+
# Copy entrypoint script
7+
COPY entrypoint.sh /entrypoint.sh
8+
RUN chmod +x /entrypoint.sh
9+
10+
# Set entrypoint
11+
ENTRYPOINT ["/entrypoint.sh"]

server/entrypoint.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
# Generate self-signed SSL certificates if they don't exist
3+
4+
SSL_DIR="/etc/nginx/ssl"
5+
CERT_FILE="$SSL_DIR/cert.pem"
6+
KEY_FILE="$SSL_DIR/key.pem"
7+
8+
# Create SSL directory if it doesn't exist
9+
mkdir -p $SSL_DIR
10+
11+
# Generate self-signed certificate if it doesn't exist
12+
if [ ! -f "$CERT_FILE" ] || [ ! -f "$KEY_FILE" ]; then
13+
echo "Generating self-signed SSL certificate..."
14+
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
15+
-keyout $KEY_FILE \
16+
-out $CERT_FILE \
17+
-subj "/C=US/ST=State/L=City/O=AFT/CN=localhost" \
18+
2>/dev/null
19+
20+
echo "SSL certificate generated successfully"
21+
else
22+
echo "SSL certificate already exists"
23+
fi
24+
25+
# Start nginx
26+
exec nginx -g 'daemon off;'

server/nginx.conf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
# HTTP server - redirect to HTTPS
12
server {
23
listen 80;
34
server_name localhost;
45

6+
# Optional: redirect all HTTP to HTTPS
7+
# Uncomment the following to force HTTPS:
8+
# return 301 https://$host$request_uri;
9+
10+
# For now, serve HTTP normally
511
location / {
612
root /usr/share/nginx/html;
713
index index.html;
@@ -15,6 +21,42 @@ server {
1521
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
1622
proxy_set_header X-Forwarded-Proto $scheme;
1723

24+
# Increase timeouts for database operations
25+
proxy_read_timeout 300s;
26+
proxy_connect_timeout 300s;
27+
proxy_send_timeout 300s;
28+
}
29+
}
30+
31+
# HTTPS server
32+
server {
33+
listen 443 ssl;
34+
server_name localhost;
35+
36+
# SSL certificate paths
37+
# For self-signed certs, mount these in docker-compose
38+
# For production, use Let's Encrypt or your SSL provider
39+
ssl_certificate /etc/nginx/ssl/cert.pem;
40+
ssl_certificate_key /etc/nginx/ssl/key.pem;
41+
42+
# SSL configuration
43+
ssl_protocols TLSv1.2 TLSv1.3;
44+
ssl_ciphers HIGH:!aNULL:!MD5;
45+
ssl_prefer_server_ciphers on;
46+
47+
location / {
48+
root /usr/share/nginx/html;
49+
index index.html;
50+
try_files $uri $uri/ /index.html;
51+
}
52+
53+
location /api/ {
54+
proxy_pass http://server:5000;
55+
proxy_set_header Host $host;
56+
proxy_set_header X-Real-IP $remote_addr;
57+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
58+
proxy_set_header X-Forwarded-Proto https;
59+
1860
# Increase timeouts for database operations
1961
proxy_read_timeout 300s;
2062
proxy_connect_timeout 300s;

0 commit comments

Comments
 (0)