Routing in nginx URI with same prefix as another URI - nginx

Currently trying to use Nginx as a proxy and finding some difficulties with routes having another URI as a prefix. Example :
location /api/route/to/specific/application {
proxy_pass http://proxying.to/site/A;
}
location /api {
proxy_pass http://proxying.to/another/site/B;
}
When visiting http://mysite.io/api/route/to/specific/application, I get a 404 from Nginx while when I visit http://proxying.to/site/A directly I get the expected page.
Any idea how I can make it work ?
Thanks

Related

location prefix matching nginx doesn't work with variables

Hi Earlier I was matching location prefix uri matching to get the redirection to website www.example.com/abc/ from www.example.com/bbb/abc(requested uri).
server {
location /bbb/ {
proxy_pass http://www.example.com/;
}
}
Earlier I was requesting on www.example.com/bbb/abc and getting redirected to www.example.com/abc
as I required
However now the problem is I've resolver and I'm also using variable. This is done to ensure that nginx starts even if one of the upstream services are down and it won't show the error of
[nginx] host upstream not found
server {
location /bbb/ {
resolver 127.0.0.11 valid=some secs;
set $example www.example.com
proxy_pass http://$example/;
}
}
Now that I'm using variable, I think url location prefix match is not working as I am sending request on www.example.com/bbb/abc , and it shows the nginx 404 not found.
Can anyone guide me how redirect properly with variable and resolver included.

Nginx proxy pass with rewrite and regex

Can Nginx rewrite a URL then use it as a proxy pass?
For example when I visit http://localhost/downloads/example-com/pc.html it should proxy to http://example.com/pc.html
Basically, I want to use my domain to download a file from another website that is why I am doing this
Yes you can rewrite url and proxy_pass to rewritten url.
But as per what you want to achieve, this simple solution should also work :
location ~ /example-com/(.*)$ {
proxy_pass http://example.com/$1;
}
if you want you achieve it by rewriting only then you can try following one :
location ~ /example-com/ {
rewrite /example-com/(.*) /$1 break;
proxy_pass http://example.com;
}
UPDATE :
pass another website url as query param like below :
http://yourdomain/downloads/data?data_url=example.com/pc.html
and change your nginx configuration as below.
location ~ /downloads/data {
set $data_url $arg_data_url;
proxy_pass http://$data_url;
}
after this configuration change you might get resolver issue as we are passing hostname in proxy_pass at runtime. To solve resolver issue you can follow this link : https://runkiss.blogspot.com/2020/01/using-nginx-authrequest-to-proxy-to.html

NGINX rewrite location without changing url

When visiting /abc/sitemap.xml I'd like to display the content of /abc/sitemap?output=xml without changing the url.
Currently, it shows the right content, but changes the url to /abc/sitemap?output=xml instead of keeping /abc/sitemap.xml, and here is my config.
location /abc/sitemap.xml {
rewrite ^ /abc/sitemap?output=xml;
}
Thanks
How about a proxy_pass:
location /abc/sitemap.xml {
proxy_pass http://$host/abc/sitemap?output=xml;
}
Note: use HTTP or HTTPS depending on your SSL configurations.

NGINX: proxy_pass for a dynamic url

I have a docker container which serves a web application on:
my.server.domain:8080
When I request that URL with a browser, it is automatically redirected to the login page:
http://my.server.domain:8080/login
I'm trying to proxy the app so that I can avoid to use the port number.
Two possible ways to achieve that are:
1) http://my.server.domain/appname
2) http://appname.my.server.domain
Either way, will work for me.
But I'm struggling with making the right NGINX configuration.
I tried with:
location /appname {
proxy_pass http://my.server.domain:8080/;
}
But when I load http://my.server.domain/appname it gets redirected to http://my.server.domain/login which doesn't exist.
If I use / it works as expected:
location / {
proxy_pass http://my.server.domain:8080/;
}
But that's not what I need.
I can add more directives like:
location /login {
proxy_pass http://my.server.domain:8080/login;
}
But then it will fail on the next redirect and so on ..

Url rewriting and Proxying in Nginx

I'm using nginx to proxy a request to an URL contained in the query string.
Basically my idea is proxying this request:
/proxy?url=http://google.com
to
http://google.com
How can I accomplish this?
I tried with
location /proxy\?url=(.*)$ {
proxy_pass http://$1;
}
but it doees not work.
Suggestions?
The query string is not part of the normalized URI used by the location and rewrite directives. However, all of the arguments are available as $arg_ variables.
For example:
location /proxy {
proxy_pass http://$arg_url;
}

Resources