Questions & Answers

Redirecting url using the ip sent as parameter with the api call on nginx. /status/ip -> http://ip:4444/status

I'm trying to get a response back from an api call to the machine.
ex: http://ip:4444/status My internal api looks something like this : /status/ip. I want my api calls from /status/ip to pick up the ip as a parameter and use it as a url like http://ip:4444/status and get a response from there. Currently I have a failed implementation of nginx server that looks like this:

 location /status/ {
            if ($args ~* "/status/param1=val1") {
                    rewrite ^ http://$arg_param1:4444/status redirect;
            proxy_pass              http://ggr;
            add_header Access-Control-Allow-Origin '*' always;
            add_header Access-Control-Allow-Headers 'X-Requested-With,Content-Type' always;
            add_header Cross-Origin-Resource-Policy 'cross-origin' always;
            proxy_http_version      1.1;
            proxy_set_header        Connection          "";
            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_connect_timeout   10;
            proxy_send_timeout      300;
            proxy_read_timeout      300;
            proxy_buffers           32 64m;
            proxy_buffer_size       64k;
            proxy_next_upstream     error timeout http_502 http_503 http_504;
            client_max_body_size    64m;
            client_body_buffer_size 64m;

I'm expecting <localhost:3000/status/ip> to redirect me to <ip:4444/status>.

2023-01-19 00:05:06
The $args string will be empty as it only contains that part of the URL that follows a ? (also known as the query string). Your URI /status/ip can be rewritten just using a rewrite statement - for example: rewrite ^/status/([0-9.]+)$ http://$1:4444/status redirect;
Answers(0) :