0

I have three URLs for which I want to do a reverse proxy. (Also, the URLs can have an optional '/' at the end.)

  1. /test
  2. /test/id
  3. test/id/{id_number}

So far I am writing the nginx config for the first one, but it seems I cannot get it right. This is my first time writing the nginx configurations. Can someone help me ?

I am currently getting this error currently -

2025/04/07 11:33:08 [error] 17#17: *18 rewrite or internal redirection cycle while processing "/test", client: 172.17.51.128, server: , request: "GET /test HTTP/1.1", host: "https://www.google.com/"

Here is my config file

http {


    upstream test_host {
        keepalive 25;
        server ${API_HOST};
    }

    server {
        listen 80;
        root /usr/share/nginx/html;
        index index.html;
        add_header 'Access-Control-Allow-Origin' '*' always;

       

      location ~ ^/test$ {
            gzip_types *;
            rewrite ^ $request_uri;
            proxy_set_header Host ${API_HOST};
            proxy_pass http://test_host/$1;
        }
    }
} ```
1
  • What are you trying to do with the rewrite ^ $request_uri; directive, rewriting any request URI to itself? What value are you expecting from $1 variable, not having any capture groups in your regex? Try the following: location /test/ { proxy_set_header Host ${API_HOST}; proxy_pass http://test_host; } Commented Apr 8 at 22:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.