nginx url rewrite rule - nginx

I have URL like this
http://domain/PROD_SHEP_PDF_Downloader/DownloadPdf?favorId=10100018565295&lang=ru
I want to rewrite this part PROD_SHEP_PDF_Downloader to this SHEP_PDF_Downloader so the result will be
http://domain/SHEP_PDF_Downloader/DownloadPdf?favorId=10100018565295&lang=ru
This rule doesn't work
location /PROD_SHEP_PDF_Downloader/ {
rewrite ^/PROD_SHEP_PDF_Downloader/(.*) SHEP_PDF_Downloader/$1 break;
proxy_pass http://localhost:85;
}

Docs say that if you just want to change the URI prefix, you can use this:
location /name/ {
proxy_pass http://127.0.0.1/remote/;
}

Related

Nginx Configuration for Redirecting URL

I want to redirect something like this /atpf/xyz -> /atp/xyz, I have a configuration like this, but doesn't seem to work
location /atpfe {
rewrite ^/atp/(.*)$ /$1 break;
proxy_pass http://backend-atp;
}
Can someone suggest?
I changed it to this and worked:
location /atpfe {
rewrite ^/atpfe/(.*)$ /atp/$1 break;
proxy_pass http://backend-atppub;
}
Wasn't fully aware of the rewrite functionality

How to write this nginx rewrite rule

I have a nginx rewrite rule like this
location ~* /question\-(.*)\.html$ {
rewrite "^/question-([0-9]+).html$" "/question/$1.html";
rewrite "^/question-([0-9]+).html$" /question.php?id=$1&lm=&pn= break;
}
This rule mean is:
if URI is /question-123456.html so rewrite to /question/123456.html
/question/123456.html is static file, and via question.php to create.
So when I visit http://example.com/question-123456.html HTTP rewrite to http://example.com/question/123456.html if not exist, I want to execute next rewrite rewrite "^/question-([0-9]+).html$" /question.php?id=$1&lm=&pn= break;
Other than return 404 to user.
I would write it this way:
location ~ /question-(.*)\.html$ {
try_files /question/$1.html /question.php?id=$1;
}

nginx proxy_pass url with GET params conditionally

I have a url http://foo.com/banana and I have another url http://foo.com/banana?a=1&b=2
I like that all /banana routes are handled by my local nginx, but I'd like any banana routes with GET params to be proxied to http://bar.com
so:
http://foo.com/banana -> http://foo.com/banana
http://foo.com/banana?a=1 -> (proxy) -> http://bar.com/banana?a=1
I should note this is not for production. I'm trying to redirect api calls to redirect to another server during development.
I've tried to do an 'if args' block but I can't do a proxy_pass in an if block.
I thought about doing a rewrite of:
http://foo.com/banana?a=1 -> http://foo.com/proxy?a=1
location /proxy {
proxy_pass http://bar.com;
}
But I don't have the right logic for above because bar.com is expecting the /banana route.
Any ideas?
Since this is not for production, you could stick with your original "if" solution. You only need to escape from the "if" block to be able to proxy_pass, which can be easily done with the traditional trick:
location /banana {
error_page 418 = #good_old_fallback;
if ($args) {
return 418;
}
}
location #good_old_fallback {
proxy_pass http://bar.com;
}
Your idea of using another location will also work, so if you prefer it better, you can go with something like this:
location /banana {
if ($args) {
rewrite ^/banana(.*)$ /proxy$1 last;
}
}
location /proxy {
internal;
rewrite ^/proxy(.*)$ /banana$1 break;
proxy_pass http://bar.com;
}

nginx rewrite with the value of the query string only

I want to redirect from
www.setup.com/view.php?id=213
to
www.setup.com/213
this is my nginx redirect
location ^~ view.php {
if ($query_string ~ "^id=([0-9-]+)$"){
rewrite ^/view.php$ http://$server_name/%1? break;
}
}
For some reason it appends
?id= to the url
so it becomes
www.setup.com?id=213
how can I remove
?id= from the url?
Use :
location / {
rewrite ^/view.php$ /$arg_id? redirect;
}

nginx - how to combine redirects?

In my nginx.conf file, I have something like this:
...
location ^~ /path1/ {
root /usr/local/html;
index path1.html;
}
location ^~ /path2/ {
root /usr/local/html;
index path2.html;
}
...
Is it possible to combine the two "redirects" into one by using wildcards, rewrites or something else?
If so, how can I do it?
Have you had a look at the nginx wiki? What you are after is the HttpRewriteModule
An example from said wiki that is pretty damn close to what you are after:
location /download/ {
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break;
return 403;
}

Resources