Nginx proxy pass with rewrite and regex - nginx

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

Related

Correct way to remove path from location in nginx with proxy pass

I currently have the following configuration to proxy requests off a single domain to multiple backends:
server {
listen 80;
location /team/app1/location/region/ {
proxy_pass https://this.is.the.backend.app.example/path1/healthcheck;
}
location /team/app2/location/region/ {
proxy_pass https://this.is.the.backend.app.example/path2/healthcheck;
}
location /team/app3/location/region/ {
proxy_pass https://this.is.the.backend.app.example/path3/healthcheck;
}
}
The paths are pretty arbitrary, essentially I just want to be able to proxy from:
https://proxydomain.com/team/app1/location1/region
To:
https://this.is.the.backend.app.example/path3/healthcheck
So
/team/app1/location1/region
Would need to be stripped from the request and just proxy the request to the intended backend. I assume the path is being appended in someway as I just get 404s...
I can pass to a domain without trailing path like so - but when I try and proxy to a domain with trailing paths it gets complicated:
server {
listen 80;
location /one {
proxy_pass http://x.x.x.x/;
}
location /two {
proxy_pass http://x.x.x.x/;
}
}
I have tried the following configuration too - to try and rewrite the url:
rewrite ^/team/app3/location/region/(.*)$ $1 break;
Hopefully it makes sense what I am trying to achieve - any guidance would be greatly appreciated.
Thanks
You were on the right track. The rewrite will look like:
location /team/app3/location/region/ {
rewrite ^/team/app1/location/region/(.*)$ /path3/$1 break;
proxy_pass https://this.is.the.backend.app.example/;
}
You should add the new path path3 in the rewrite rule. With this NGINX will rewrite the $uri and append it to the proxy_pass. In case app3 and path3 are a fixed pattern you can tune the location block as well as the rewrite to simplify your configuration. But I would generally start with the approach mentioned above.
AND we keep in mind. Regex matching in locations will consume CPU. So sometimes it is better to have them fixed / static. Especially if you are using them in health checks and query them every 5s or so.
Thanks for that - definitely helped getting me down the correct track:
In the end the working config was:
location /team/app3/location/region {
rewrite ^/team/app3/location/region(.*) /path3/healthcheck$1 break;
proxy_pass https://this.is.the.backend.app.example;
}
Which correctly proxied through to:
https://this.is.the.backend.app.example/path3/healthcheck

Nginx How do i route to reverse proxy by location

Currently i'm using nginx server and using nodejs server as reverse proxy.
I want to make the nginx server proxy_pass different server by location.
So, lets say my domain is subdomain.domain.com.
When loading subdomain.domain.com/, this should serve static files.
When loading subdomain.domain.com/example1/req1/req2, this should route to 127.0.0.1:3000/req1/req2.
When loading subdomain.domain.com/example2/req1/req2, this should route to 127.0.0.1:8080/req1/req2.
But my configuration routes subdomain.domain.com/example1/req1/req2 to 127.0.0.1:3000/example1/req1/req2 resulting error. (nodejs returns Cannot GET /example1)
How should I write this nginx conf file properly?
Try use rewrite directive in location block.
Something like:
location /example1/ {
rewrite ^/example1/(.*)$ /$1 break;
proxy_pass http://xxxxxx;
}
You can check documents related to rewrite module at http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
You need to add below code snippet to your nginx.conf.
server subdomain.domain.com;
location / {
rewrite ^/example1/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:3000;
}
}

Nginx pass_proxy with variables

I'm having trouble making nginx proxy an url with variable to a service within kubernetes.
Url looks like this:
http://localhost/user?username=Dave
I expect this url to take me to a subpage /user, which will read ?username=Dave and then fetch data from a database. However this takes me to the home page of the application(/ instead of /user) and does not read the variable even though url includes /user?username=Dave.
My current nginx config file looks like this:
server {
listen 0.0.0.0:80;
server_name localhost;
location / {
proxy_pass http://${FLASK_APP}:8080/;
}
location /user {
proxy_pass http://${GO_APP}:8000/;
}
}
I have read that location /user will match the url I'm passing. What is wrong with it? Or do I need to add something to proxy_pass http://${GO_APP}:8000/; or location /user?
As noted in the comments, the issue arises because you are using a variable in the proxy_pass target. As also noted in the comments, this question is related. As the answer referencing the docs states:
A special case is using variables in the proxy_pass statement: The
requested URL is not used and you are fully responsible to construct
the target URL yourself.
This means that you either need to use a static proxy_pass target, such as
// note that I added the forward slash
location /user/ {
proxy_pass http://destination:8000/;
}
Or as an alternative, I believe you can do it this way also
location /user/ {
proxy_pass http://${GO_APP}:8000/user$is_args$args;
}

using proxy_pass with dynamic variables nginx

I'm trying to use proxy_pass with nginx to mask redirects to my image CDN. I'd like to be able to go to a path like:
myserver.com/images/12345/whatever-name-goes-here.jpg
I'd like that to proxy to
http://imagecdn.com/12345.jpg
i've tried the following
location ~ /images/(.*)/(.*) {
proxy_pass http://imagecdn.com/$1.jpg;
}
But i keep getting 502 errors. Any idea if this is even possible ?
I would suggest using an actual redirect, such as:
location ~ ^/images/(.*)/(.*)$ {
return 301 $scheme://imagecdn.com/$1.jpg;
}

Routing in nginx URI with same prefix as another URI

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

Resources