I have a issue with my NGINX setting with redirecting to a custom error page on another location (incl. css, images, js) if a error page should be thrown.
At first I would like to block access to an folder (like .git
). This can be easily done via (inside the server block)
location ~ /(.git) {
deny all;
return 404;
}
Then i created a custom error_page element (inside the server block) with a custom 404.html file on a different location than the root directory of the website.
error_page 404 /404.html;
location = /404.html {
root /var/data/websites/error-page;
internal;
}
After these changes, my custom 404 page will be shown - but without css, js and images.
If i inspect the website, the reason is simple: the path of the files are wrong - they are based on the location (in my example .git
).
https://it.dmetzler1988.io/.git/css/main.css net::ERR_ABORTED 404
.
Here is the complete NGINX config file for this page (only removed the ssl certificate paths):
server {
listen 443 ssl;
listen [::]:443 ssl http2;
ssl_certificate <path>;
ssl_certificate_key <path>;
server_name it.dmetzler1988.io;
root /var/data/websites/dmetzler1988.io/it.dmetzler1988.io;
index index.html index.php;
error_page 404 /404.html;
location = /404.html {
root /var/data/websites/error-page;
internal;
}
location ~ /(.git) {
deny all;
return 404;
}
}
So my questions on this place:
- How can i fix the issue with the wrong path (remove the
.git
from path)? - Is this the correct way for such an use case or is there a better solution?