D
D
DnDwarf2015-11-03 13:57:03
Nginx
DnDwarf, 2015-11-03 13:57:03

Redirect in Nginx any requests and links to the site to the main page?

UPDATE: I forgot to add that location / happens to proxy_pass on apache, so the solution below will most likely not take off.
Good afternoon, please tell me, there is a site example.com. It is
necessary that any requests to it be redirected to the main page of the site, and no matter what type of request:
example.com/blabla.php , example.com/blabla.html , example. com/blabla.pdf ,
example.com/blabla and so on.
Standard php, html, htm links can be redirected by extension (or by universal mask "any name - dot - any extension"):
location ~* \.(html|htm|php)$ {
return 301 http://example.com ;
}
But what to do with links like example.com/blabla
Thanks in advance for your help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DnDwarf, 2015-11-03
@DnDwarf

location = / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 120;
proxy_send_timeout 120;
proxy_read_timeout 180;
}
location / {
rewrite ^ / permanent;
}
That's how it all worked, thanks! )

L
luxter, 2020-10-28
@luxter

Here's another option:

location ~* (?<=\/).* {
        return 301 /;
}

location = / {
        proxy_pass http://127.0.0.1:8887/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header HTTPS YES;
        proxy_connect_timeout 120;
        proxy_send_timeout 120;
        proxy_read_timeout 180;
}

rewrite works, but is extremely redundant here. you can specify a regular expression for location, in which there will be 301 redirects to the main (root /), and for the main one specify location with an equal sign - in this nginx stops all further searches if there is a match and avoids cyclic redirects (too many redirects), if any .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question