How to set Nginx 404 page not affecting other directory? - nginx

I need to set these two Urls redirect to a custom 404 page:
https://example.com/frontend/#/,
https://example.com/frontend/
the Nginx config below works fine:
server{
listen 80;
server_name example.com;
error_page 404 /404.html;
location = /404.html {
root /opt/app/nginx/html;
internal;
}
location = /frontend/ {
return 404;
}
}
However this setting makes URLs with the same prefixes go to 404 as well, which is a pain in the ass!
https://example.com/frontend/#/abc,
https://example.com/frontend/#/123
all redirect to 404.
I use exact matching (= modifier) for the 404 settings,
why does it still affect others and how can I fix it?
Thank you for any help! :D
Edited
I found the reason thanks to the answers given down there! So the # sign is processed by the browser only, the browser never passes # to the server, that's why Nginx treats https://example.com/frontend/#/abc,
https://example.com/frontend/#/123 all as https://example.com/frontend/.
In this case, if I wanna set https://example.com/frontend/#/ to the 404 page, what I need to do is to create an index page under /frontend folder in the project, and put 404 content inside the index page, There's nothing Nginx could do with redirecting URL contains hash sign.

You can't do that because the hash part isn't sent to the server. It's for client side processing only.

Try using two location without the "/" in each.
location = /frontend {
return 404; }
location = /frontend/# {
return 404; }

Related

Nginx redirect being ignored

I'm trying to create a redirect for all URLs on a domain that contains /comfy/ to another domain, but keeping the rest of the request.
For example
https://www.example.org/system/comfy/cms/files/files/000/002/012/profile/AdobeStock_194204879.jpeg
to
https://www2.example.org/system/comfy/cms/files/files/000/002/012/profile/AdobeStock_194204879.jpeg
In nginx in the server block for example.org I have
location ~ ^/comfy/(.*) {
return 301 $scheme://www2.example.org/$request_uri;
}
but i'm still getting a 404, with the original domain, ie it's still accessing
https://www.example.org/system/comfy/cms/files/files/000/002/012/profile/AdobeStock_194204879.jpeg
I have also tried
rewrite ^(/comfy/)(.*)$ http://www2.example.org/$2 permanent;
and
rewrite ^/comfy/(.*) http://www2.example.org/$1 break;
but these are failing too. What am I missing?
Thanks!
Ok, turns out I needed to do
location ~ ^/system/comfy/cms/(.*) {
return 301 $scheme://www2.example.org/$request_uri;
}

Nginx URL rewrite

I have a website that has laravel setup to run under http://www.example.com/lara/
So, most Laravel pages have URLs of type http://www.example.com/lara/page/23 OR http://www.example.com/lara/category/23 etc.
Nginx is the underlying server and it has the following configuration to handle these requests:
location /lara/ {
try_files $uri $uri/ /lara/index.php?$query_string;
}
Everything works ok.
Now, I need to setup a special page with the URL http://www.example.com/mystuff/ which actually is handled by
http://www.example.com/lara/category/29
To get this working I added the following rewrite right below location /lara/, that is:
rewrite ^/mystuff/(.*)$ /lara/category/29/$1 last;
Unfortunately, I get a page not found error. Any insights?
Further investigation & research:
1)
location /mystuff/ {
return 301 /lara/index.php/category/29;
}
worked although that's not (browser address bar changes to) what I actually want.
2) Looks like Laravel is not seeing the updated REQUEST_URI.
Try this
server {
...
rewrite ^/lara/(.*)\..*$ $1/mystuff last;
return 403;
...
}

Nginx 301 redirection options

I have some 100 or so URLs that need to be permanently redirected from static HTML to dynamic pages served by Wordpress. All examples for Nginx 301 redirection I found suggest that each redirect should be defined as its own server block.
However, I have found out that multiple redirects within one server block work as well such as in this simplified configuration:
server {
listen 80;
server_name www.example.com;
root /var/www/;
location = /subdir/red1.html { return 301 /subdir/?p=1; }
location = /subdir/red2.html { return 301 /subdir/?p=2; }
location = /subdir/red3.html { return 301 /subdir/?p=3; }
}
Curl -I confirms the 301 code. The redirects take place. I prefer this configuration to the one with one server block per redirect because human readability is better. But does this configuration lack something that I'd otherwise have if each redirect was in its own server block?
Well, execrpts you found are probably dealing with http to https redirections where it makes sense not to have anything more in the server block because the vhost is only there to redirect to an other domain. That's completely wrong to pick random examples and take them as a rules of thumb instead of refering to the official documentation.
100 redirects is not a huge count, you could even use permanent rewrites in one unique server block and group them with regexs.
server {
server_name www.example.com;
root /var/www/;
location /subdir/ {
rewrite ^/subdir/red([1-3])\.html /subdir/?p=$1 permanent;
}
}

Drop unwanted connections

I want to block unwanted Bots from accessing sites on the server.
Can nginx drop / kill the connection right away when a certain Bot is detected?
if ($http_user_agent ~ (agent1|agent2) ) {
**KILL CONNECTION**;
}
Something like example above.
Use return 444; This non-standard status code of 444 causes nginx to simply close the connection without responding to it.
if ($http_user_agent ~ (agent1|agent2) ) {
return 444;
}
Reference documentation
More elaborative documentation
Yes, it can. See the question below - this redirects based on an agent string, but you can really do whatever you want (error page or whatever).
Nginx proxy or rewrite depending on user agent
However, please note - that a decent bot will fake its user-agent string to look just like a normal browser, so this is by no means a robust way to deter bots from sweeping your site.
server {
listen 8443 default ssl;
error_page 404 403 = 444; #disconnect if 404 or 403
root /srv/empty; #Empty forder
...
...
location /summary_report{
root /srv/www;
index index.html index.htm;
}
}
https://127.0.0.1/ Disconnect.https://127.0.0.1/summary_report Not disconnect.

Multiple 404 error pages in nginx

I am running nginx server. I want to serve a custom error page for a particular request only. For-example for request
http://localhost/abc1 & http://localhost/abc2
if these pages are not there I want to serve a custom error page. This custom error page should appear only for above two mentioned links, rest of the page errors can show the default error page. I have tried different configuration but nothing seems to work. Thoughts
Regards,
Farrukh Arshad.
Your answer is quite correct, but as you said, you only defined them for the html's, remove the extension and it should work for the whole directory, no need to repeat the root, just define it in the server block scope
server {
listen 80;
root /var/www/nginx-default;
location /abc1 {
error_page 404 /special_error.html;
}
location /abc2 {
error_page 404 /special_error2.html;
}
}
Ok, I found the answer. The trick is you have to define error_page explicitly for all those special locations. Here is the configuration which worked for me.
location / {
root /var/www/nginx-default;
index index.html index.htm;
error_page 404 /404.html;
}
location /abc1.html {
root /var/www/nginx-default;
error_page 404 /special_error.html;
}
location /abc2.html {
root /var/www/nginx-default;
error_page 404 /special_error2.html;
}
I am not good with nginx, but I have noticed it depends on the search pattern you give in "location" tag. I have tried different things and those failed . Forexample the above rules will ONLY work for
http://localhost/abc1.html
and fail for
http://localhost/abc1
so your "location" search pattern should be good if you want to cover second case. Probably some nginx guru can shed some more light on this. Thanks.

Resources