nginx url rewrite mainting GET parameters - nginx

I would like to rewrite a path destination to another address and port but I want to maintain all the GET parameters.
The number of GET parameters is variabile and I want to keep all of them.
Something like this
redirect this
https://test.com/customPath/file.php?test1=1&test2=dsa&....
to
https://testrewrite.com:5302?test1=1&test2=dsa&....
In the test.com directive I wrote something like this.
location /customPath/file.php {
rewrite /(.*) /(.*) break;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Origin http://$host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://testrewrite.com:5302;
proxy_pass_header Server;
}
But obviusly it doesn't works. Any help?

Related

How to use proxypass with only a port?

I'm trying to use proxypass for one of my torrent client(qbittorrent) web-ui.
The HTTP link of it is like this :
http://user.server.host.com:port/
And I tried using a proxypass guide from my host with the following config :
location /qbit {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_x_host;
proxy_set_header X-NginX-Proxy true;
rewrite /(.*) /user/$1 break;
proxy_pass http://localIP:port/;
proxy_redirect off;
}
The issue is that this only works if the software is in the following format :
http://user.server.host.com:port/username/software
I do not have root access.
To have a HTTPS page instead of a HTTP on my qbittorrent web-ui.

nginx proxy_pass with rewrite in url not keeping the original paths

I'm trying to setup nginx to forward incoming requests in the following way:
http://localhost/service -> http://localhost:8080
http://localhost/service/foo -> http://localhost:8080/foo
Now I am able to achieve the first line with the following config:
...
upstream service {
server service:8080;
}
...
location /service/ {
server_name_in_redirect off;
proxy_pass http://service;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
but when I add extra paths (e.g. /foo) the url is rewritten in this way
http://localhost/foo/
therefore I will never be able to reach localhost:8080/foo. Any ideas on how to make this work?
Thanks in advance

How to configure nginx to redirect with subpaths matching given pattern

I am using nginx as a reverse proxy. I want to redirect locations matching some patterns keeping exactly the same subpaths. For example, this configuration does not work for me:
location ~ ^/bpmn/?(.*)$ {
resolver 127.0.0.11 ipv6=off;
set $upstream activiti:8080;
proxy_pass http://$upstream/$1;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
I would like to redirect the location matches the path "/bpmn/a/b/c" to "http://$upstream/a/b/c". My configuration works only for one level (e.g. "bpmn/a").
Thanks a lot for your help,
Regards.

Proxy all subdomain to other domain path

How to proxy all subdomain to other domain path?
For example
SUBDOMAIN.abcxyz123.com
To be proxied to
myapp.otherdomain.com/SUBDOMAIN
Making sure that all header/path and query parameters in the request is kept.
Update:
I've tried and have a working config but still not the one I need:
server {
listen 80;
server_name ~^(?<subdomain>.+)\.abcxyz123\.com$;
location / {
proxy_set_header Host "myapp.otherdomain.com";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
# this worked:
proxy_pass http://myapp.otherdomain.com/somepath/;
# this does not work:
#proxy_pass http://myapp.otherdomain.com/$subdomain$request_uri;
}
}
Try this
server {
server_name ~^(?<subdomain>.*)\.abcxyz123\.com$;
resolver 8.8.8.8;
rewrite ^/(.*)$ /$subdomain/$1;
location / {
proxy_set_header Host "myapp.otherdomain.com";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_pass http://myapp.otherdomain.com;
}
}
This should proxy all your traffic with original query parameters(query strings, request body, request method, etc), I changed the host header to the proxied "myapp.otherdomain.com" incase the server of 'myapp.otherdomain.com' has more than one virtual hosts. If you don't want the change, use $host instead.
This answer might need another edit since your question isn't very clear. If you have further question, comment and i will edit in my answer.

nginx rewrite rule for redirection

I have two apps running on host1:7000 and host2:7000. I am fronting the two hosts by an nginx reverse proxy, where I want mydomain.com/admin to point to host1:7000/portal and mydomain.com/user to host2:7000/portal.
I have written the following config
listen 80;
server_name mydomain.com *.mydomain.com;
location ~ ^/admin/(.*)$ {
proxy_pass $scheme://<IP-ADDRESS>/$1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
I can get to mydomain.com/admin to be redirected to host1:7000/portal but when the app redirects from host1:7000/portal on to host1:7000/login via relative path, in the browser I see mydomain.com/login. What do I need to do to get the second redirect go mydomain/admin/login?
Why do people use regexps for no reason and have all kind of problems with it?…
location /admin/ {
proxy_pass http://host1:7000/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
This will automatically strip /admin/ from proxied request and prepend it in Location header (which is used in redirect).
See proxy_pass and proxy_redirect docs.

Resources