I got a ask of doing a redirect to 404 page if there is a trailing slash at the end of the URL.
Eg:
https://www.example.com/test/
If the user enters a trailing slash at the end means we need to redirect it to 404 page. Previously I have redirected the same URL by removing the trailing slash, Now the requirement has changed to redirect to the 404 page.
Here is the nginx conf file for it. Please let me know what change I have to make here to achieve the scenario.
nginx.conf
server {
listen 80;
rewrite ^/(.*)/$ /$1 permanent;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri.html $uri/index.html $uri/index =404;
error_page 404 /404.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Updated Conf:
Default Nginx 404 error page is displayed after this config. What's the update required here to display the custom 404 page?.
server {
listen 80;
location ~ ^/(.*)/$ {
return 404;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri.html $uri/index.html $uri/index =404;
error_page 404 404 /404.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}