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
Related
Current API end points are like
domain.com/api/rest/[actual_module_routing_path, eg. cms/page/home]
In the new system this has to be something like
domain.com/rest/V1/$1
It has to work regardless of HTTP Method (GET, POST, PUT, DELETE). Trying something like this in nginx host config and I couldn't get it working, some help will be really appreciated
location /api/rest {
#Rewrite $uri=/api/rest/ back to just $uri=/rest/V1/$1
rewrite ^/api/rest/(.*)$ /rest/V1/$1 break;
}
location ~ /api/rest/(.*)
{
rewrite ^(.*)$ /rest/v1/$1 redirect;
}
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;
}
I have the following URLs:
1) http://example.com/downloads/
2) http://example.com/downloads/widgets
3) http://example.com/downloads/gadgets
These URLs need to be redirected rewritten to the following:
1) http://example.com/products/
2 & 3 & etc) http://example.com/products/thingies
I'm currently trying the following nginx code:
location ~* ^/downloads/ {
rewrite ^/downloads/$ /products break;
rewrite ^/downloads/(.*)$ /products/thingies break;
}
It's almost working, however my site's document root is /var/www/example.com/public. So after processing the rewrite rules, nginx tries to literally serve /var/www/example.com/public/products/, whereas I want it to just rewrite to http://example.com/products/ (which then proxies to PHP and so on).
Where am I failing? Is there a different, better way to accomplish this?
Thank you for any help.
-- UPDATE --
I got it to work by using the following rules:
rewrite ^/downloads/?$ $scheme://$host/tools last;
location ~* ^/downloads/ {
rewrite ^/downloads/?$ $scheme://$host/products last;
rewrite ^/downloads/(.*)$ $scheme://$host/products/thingies last;
}
IS this the proper way of doing it in nginx? I haven't seen this rewrite rule format anywhere while researching this. It somehow seems odd.
Your update redirects, not rewrites.
Here is how I would do:
location /downloads/ {
rewrite ^ /products/thingies;
}
location = /downloads/ {
rewrite ^ /products/;
}
# uncomment if you need '/downloads' (without trailing slash) act as '/downloads/'
#location = /downloads {
# rewrite ^ /products/;
#}
The correct syntax appears to be:
location ~* ^/downloads/ {
rewrite ^/downloads/?$ /products permanent;
rewrite ^/downloads/(.*)$ /products/thingies permanent;
}
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/;
}
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;
}