Simple Nginx Proxy Pass and Regex - nginx

I want a simple nginx rule to pass the following example.
request http://myserver:8888/application/external/testUrl_1
redirect to
http://myglassfishserver:8080/application/external/testUrl_1
Then say I sent in
http://myserver:8888/application/external/testUrl_2
it would redirect to
http://myglassfishserver:8080/application/external/testUrl_2
I should also keep all post data if I were to send a http POST.
To me this seems like it should be simple.
I'm trying
location ^/application/external {
proxy_pass http://myglassfishserver:8080/$1;
allow all;
}
I'm getting *1 access forbidden by rule, client: which I know is because it didn't match a rule. I've tried numerous combinations. What is it I've done wrong ? I'm guessing its the $1

location /application/external/ {
proxy_pass http://myglassfishserver:8080;
}

Related

Can a response from an http request alter the base address in the next client request?

I have an octoprint server running at http://192.168.1.205. I also have an nginx server hosting myDomain. I want to be able to use the nginx server to pass on a request for http://myDomain/octo to http://192.168.1.205 using a reverse proxy. Here is my nginx code...
server {
server_name myDomain;
location /octo/ {
rewrite ^/octo/(.*) /$1 break; #strip /octo from url
proxy_pass http://192.168.1.205;
}
}
The first http://myDomain/octo request is passed on to http://192.168.1.205 correctly. But after the first response the code in the client makes another request to http://myDomain/moreUri. Since this uri doesn't have /octo nginx doesn't know to send it to http://192.168.1.205/moreUri. Is there a way to have nginx change something in the first response so that the client then makes following requests to http://myDomain/octo/moreUri?
I was able to accomplish this for a case where the octoprint server responded with a redirect. I used ...
proxy_redirect http://192.168.1.205/ http://myDomain/octo/;
and it worked. But that only works on redirects and the following requests were wrong again.
Is there a way to have nginx change something in the first response so
that the client then makes following requests to
http://myDomain/octo/moreUri?
I am not aware that this is possible.
What about to adjust the nginx configuration accordingly ? using location / to process all requests within that block and add an additional redirect directive to address the "Since this uri doesn't have /octo nginx doesn't know to send it to http://192.168.1.205/moreUri" should do the trick.
server {
server_name myDomain;
location / {
rewrite ^/octo/(.*) /$1 break; #strip /octo from url
rewrite ^/(.*)/(.*) /octo/$2 break; #rewrite /moreURI to /octo/moreURI
proxy_pass http://192.168.1.205;
}
}
No matter if the above nginx reconfiguration fixes your issue, i assume the root cause why you need to configure the nginx as reverse proxy in this way might be a misconfigured (or not optimally configured) application. Check the config file if it is possible to configure the applications base path. If so, set it to /octo/ (so the application itself prepends /octo/ to all the links it presents to the user and all requests to the backend automatically) and adjust the rewrite rules accordingly.

How to escape "?" in nginx rewrite rule output

I am trying to reverse proxy in nginx, rewriting a front end page like "/a/b?page=2" into a backend request to "/a/b%3fpage=2"
I cannot figure out how to get nginx to make reverse proxy requests which include "%3f".
With the following config:
rewrite ^/one$ /a%3fb;
rewrite ^/two$ /a?b;
rewrite ^/three$ /a\?b;
/one makes a backend request like GET /a%253fb HTTP/1.0
/two makes a backend request like GET /a?b HTTP/1.0
/three makes a backend request like GET /a\?b HTTP/1.0
How can I get a backend request like GET /a%3fb HTTP/1.0?
Thanks to #Richard Smith's comment, I was able to fix this for my specific case with the following code:
location / {
set $backend_uri $request_uri;
if ($args ~* "page=(\d+)") {
set $page $1;
set $backend_uri $uri%3fpage=$1;
}
proxy_pass http://example.com$backend_uri;
}
I think that I might also have been able to do something more general with the lua rewrite directive, but I was unable to install mod-lua on an Amazon Linux 2 machine, see https://serverfault.com/questions/961337/how-to-install-nginx-mod-lua-on-amazon-linux-2

Redirect post request using nginx

I am currently getting post request like this:
POST /api/x/y HTTP/1.1
With request body: a=x&b=y etc.
I want to redirect the request to another server in any of these two ways:
1. GET x.x.x.x:8888/xy/abc?a=x&b=y
2. POST x.x.x.x:8888/xy/abc with body a=x&b=y
I am trying these two redirect options:
1.rewrite ^(.*) http://server/api$request_body redirect;
//this is not sending body params
2. return 307 http://server/api?$request_body;
//this is giving me 400
If you do this:
location /api/ {
proxy_pass http://x.x.x.x:8888;
}
Then a request to example.com/api/x/y/ will be proxied to http://x.x.x.x:8888/api/x/y/.
If you do this:
location /api/x/y/ {
proxy_pass http://x.x.x.x:8888/xy/abc/;
}
Then a request to example.com/api/x/y/ will be proxied to http://x.x.x.x:8888/xy/abc/.
Request method will be unchanged, unless you tell Nginx to change it. Some headers will not be passed, unless you tell Nginx to pass them.

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;
}

How nginx rewrite and proxy? http://sa.com/rabbitmq/api/#/queues/%2F/somequeue

I try to use a single domain to proxy several programs like this:
http://sa.com/rabbitmq/ ---> http://localhost:15672/
http://sa.com/zabbix/ ---> http://localhost:10000/
and my conf is blow:
location /rabbitmq {
rewrite /rabbitmq(.*) $1 break;
proxy_pass http://localhost:15672;
It works well until I click a queue name to watch the detail,
which url is as the title said:
http://sa.com/rabbitmq/api/#/queues/%2F/somequeue
an 404 error occured, I saw an request in dev-tools of chrome:
http://rabbitmq.testing.gotokeep.com:15672/api/queues/%2F/dailyNewLike?lengths_age=60&lengths_incr=5&msg_rates_age=60&msg_rates_incr=5
this request returned 404.
I guess that when rewrite processed, the uri was decoded (.../%2F/... -> ...///...) and the extra slashes will be removed...
Is my guess right? Is there a solution?
Your guess is good, but no, the real problem is that nginx converts %2F into %252F (% -> %25).
%2F is vhost name (/). I don't found the real solution for this problem, and my workaround was to use other vhost name which do not contains / symbol (e.g. pool1).
You can use $request_uri to prevent nginx decode the uri.
use conf like below
location /rabbitmq {
if ($request_uri ~* "/rabbitmq/(.*)") {
proxy_pass http://localhost:15672/$1;
}
}

Resources