I have two Single Page Applications (SPAs) served by Nginx. One is located in /usr/share/nginx/html
and the other in a subdirectory /usr/share/nginx/html/app2
. Both applications have identical routing and custom 404 page implementations. The first application correctly serves the custom 404 page, but the second application (app2) redirects to the default Nginx welcome page when a non-existent route is accessed.
Here are the relevant parts of my Nginx configuration:
server {
listen 8443 ssl;
server_name 127.0.0.1;
.....
location / {
expires 0;
add_header Cache-Control "no-cache, no-store, must-revalidate";
proxy_read_timeout 300s;
proxy_pass_request_headers on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://app1-svc:8080;
proxy_buffering off;
}
location /app2/ {
expires 0;
add_header Cache-Control "no-cache, no-store, must-revalidate";
proxy_read_timeout 300s;
proxy_pass_request_headers on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://app2-svc:8080;
proxy_buffering off;
}
error_page 404 405 406 411 500 502 503 504 /index.html;
location = /index.html {
root /usr/share/nginx/html;
}
location = /keepalive.html {
root /usr/share/nginx/html;
}
}
}
Problem:
- When accessing a non-existent route in the main application (
/usr/share/nginx/html
), the custom 404 page is served correctly. - When accessing a non-existent route in the subdirectory application (
/app
), it redirects to the default Nginx welcome page instead of the custom 404 page.
What I’ve tried:
- Ensured both applications have identical routing configurations.
- Checked file and directory permissions.
Questions:
- What could be causing the Nginx welcome page to appear instead of the custom 404 page for the subdirectory application?
- How can I modify my Nginx configuration to correctly serve the custom 404 page for the application in
/app2
?