I have three URLs for which I want to do a reverse proxy. (Also, the URLs can have an optional '/' at the end.)
- /test
- /test/id
- 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;
}
}
} ```
rewrite ^ $request_uri;directive, rewriting any request URI to itself? What value are you expecting from$1variable, not having any capture groups in your regex? Try the following:location /test/ { proxy_set_header Host ${API_HOST}; proxy_pass http://test_host; }