Skip to content

Commit c343577

Browse files
committed
Refactor Nginx configuration for frontend and load balancer; remove default site, enhance SPA handling, and improve caching headers. #30
1 parent 09362ab commit c343577

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

frontend/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ RUN npm run build
2121
# Use official nginx image as the base image
2222
FROM nginx:alpine
2323

24-
# Remove default Nginx website
25-
RUN rm -rf /usr/share/nginx/html/*
24+
# Remove default Nginx website and configuration
25+
RUN rm -rf /usr/share/nginx/html/* /etc/nginx/conf.d/default.conf
2626

2727
# Copy the build output to replace the default nginx contents.
2828
COPY --from=builder /app/dist/contact-portal/browser /usr/share/nginx/html
2929

3030
# Copy the nginx file to fix fallback issue on refreshing.
31-
COPY /nginx.conf /etc/nginx/conf.d/default.conf
31+
COPY /nginx.conf /etc/nginx/nginx.conf
3232

3333
# Expose port 80
3434
EXPOSE 8080

frontend/nginx.conf

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,47 @@
1-
server {
2-
listen 8080;
3-
server_name localhost;
1+
events {
2+
worker_connections 1024;
3+
}
44

5-
root /usr/share/nginx/html;
6-
index index.html;
5+
http {
6+
include /etc/nginx/mime.types;
7+
default_type application/octet-stream;
78

8-
location / {
9-
# Handles Angular routing
10-
try_files $uri $uri/ /index.html;
11-
}
9+
sendfile on;
10+
keepalive_timeout 65;
1211

13-
# Optional: Static file caching
14-
location ~* \.(?:ico|css|js|gif|jpg|jpeg|png|svg|woff|woff2|ttf|otf|eot|ttf|ttc|map)$ {
15-
expires 6M;
16-
access_log off;
17-
add_header Cache-Control "public";
18-
}
12+
server {
13+
listen 8080;
14+
server_name localhost;
15+
16+
# Disable automatic trailing slash redirects
17+
server_name_in_redirect off;
18+
port_in_redirect off;
19+
20+
root /usr/share/nginx/html;
21+
index index.html;
1922

20-
error_page 404 /index.html;
23+
location / {
24+
# Handles Angular routing
25+
try_files $uri $uri/ /index.html;
26+
27+
# Additional headers for SPA
28+
add_header Cache-Control "no-cache, no-store, must-revalidate";
29+
add_header Pragma "no-cache";
30+
add_header Expires "0";
31+
}
32+
33+
# Handle Angular routes without trailing slash
34+
location ~ ^/([^/]+)$ {
35+
try_files $uri $uri/ /index.html;
36+
}
37+
38+
# Optional: Static file caching
39+
location ~* \.(?:ico|css|js|gif|jpg|jpeg|png|svg|woff|woff2|ttf|otf|eot|ttf|ttc|map)$ {
40+
expires 6M;
41+
access_log off;
42+
add_header Cache-Control "public";
43+
}
44+
45+
error_page 404 /index.html;
46+
}
2147
}

loadbalancer/nginx.conf

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ http {
1818
listen 80;
1919
# Route all other requests to the Angular frontend
2020
location / {
21-
proxy_pass http://frontend/;
21+
proxy_pass http://frontend;
2222
proxy_set_header Host $host;
2323
proxy_set_header X-Real-IP $remote_addr;
2424
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
2525
proxy_set_header X-Forwarded-Proto $scheme;
26-
27-
# Serve Angular routes correctly
28-
# try_files $uri /index.html;
26+
27+
# Additional headers for SPA
28+
add_header Cache-Control "no-cache, no-store, must-revalidate";
29+
add_header Pragma "no-cache";
30+
add_header Expires "0";
2931
}
3032

3133
location /swagger/ {

0 commit comments

Comments
 (0)