Nginx rewrite rule not works when using proxy_pass - nginx

I am using nginx, and is configured to proxy_pass the requests
proxy_pass http://127.0.0.1:3000/;
So when user asks for
https://example.com/salon/1234
every thing works fine.
Now I need this URL to works either
https://example.com/salon/1234/anystringt
So I wrote this
rewrite ^(.*)/salon/([0-9].*)/(.+)$ /salon/$2 last;
and it does hot work.
When I change the rewrite rule to
rewrite ^(.*)/salon/([0-9].*)/(.+)$ /salon/$2 redirect;
everything works fine but it redirecting the user.
Any one knows why the first rewrite rule does not work?

Related

nginx how to redirect all pagination

I need help with my nginx configuration.
The goal is to have all requests to my site like site/page1 site/smth/page1 be redirected to just site/ and site/smth/ basically for all requests ending like page[number]
I tried some examples that I found like rewrite ^/page/(.*)$ /$1; still wasn't able to get the redirection. Maybe I misplaced it, not quite sure where I should put the sting. Tried location and server blocks.
The nginx documentation examples for redirecting were a bit too hard to understand for me, so a little explanation would be great.
If you need a 301 HTTP redirection, try this rewrite rule (before the first location block):
rewrite ^(.*/)page\d+$ $1 permanent;
You can try something like this (not tested)
location ~ ^/(.+)/page[0-9]+$ {
rewrite ^/(.+)/page[0-9]+$ /$1 last;
}

nginx rewrite all trailing / to /index.html with proxy pass

Using nginx as a reverse proxy, I'd like to mimic the index directive with proxy_pass. Therefore I'd like nginx to query /index.html instead of /, /sub/index.html instead of /sub/.
What would be the best approach to do this ?
Not sure if it's relevant, but the proxied server does answer HTTP 200 on /, but I'd still like to rewrite it to /index.html.
As the / request leaks some information by listing the directory content, I'd also like to be sure that no one will be capable of accessing it (like doing something like /sub/..).
Thanksies
Just add :
rewrite (.*)/$ $1/index.html last;
rewrite (.*)/..$ $1/../index.html last;
Should works

Split request_uri in nginx

I'm new to Nginx, I'm trying to do proxy_pass for a specific format of URL.
For example "http://example.com/sync/test" is the URL accessed by the user, internally the URL has to do proxy_passs to apache server like, "http://localhost:8000/test" need to exclude sync, but now it's` redirecting has "http://localhost:8000/sync/test".
Is there any option to split and get parameters after sync,
Suggestions or solution is appreciated
Rewrite the url by removing /sync/ from the url
location / {
rewrite ^/sync/(.*) /$1 break;
proxy_pass http://localhost:8000;
}

Nginx rewrite makes me crazy

the URL is:
http://xxxxxx/index.php?route=product/category&path=565
and why the heck the rules below dont work?!?!
THIS - dont work at all:
if ($request_uri ~* "^(.*)product/category&path=565"){
rewrite ^(.*)$ /index.php?route=product/category&path=174&filter=sale? last;
}
THIS - aint work too! =(
if ($args ~* "^(.*)product/category&path=565"){
rewrite ^(.*)$ /index.php?route=product/category&path=174&podbor=m:feniks? last;
}
Both of your rewrite rules work correctly (for me). The problem maybe that your application requires external redirection rather than internal redirection in order to see the change.
Consider using the redirect modifier rather than the last modifier. Of course, this will mean that the client will also see the rewritten URL.
See this document for more.

Nginx replace // with / in URL

We are currently getting web requests with www.domain.com//index.php which are resolving, but causing issues with google. How can we rewrite the request to catch these and redirect to www.domain.com/index.php
Thanks
By default ngingx merges double-slashes and urls like www.domain.com//index.php works well.
You can turn off merging and make rewrite rule with redirection:
merge_slashes off;
rewrite (.*)//(.*) $1/$2 permanent;

Resources