Nginx regex and wildcard for location

12,657

Solution 1

You don't need to make it this complex. You could just it as simple as below

server {
    listen 80 default_server;
    server_name _;
    location /api/ {
    proxy_pass       http://localhost:8081/api/;
   }

}

And that should pass anything starting with /api to http://localhost:8081/api/, with the request uri after /api/ appended

Solution 2

/ should be escaped
.* is anything. If you know there should be names and / between them say it

  ^https?:\/\/\w+\/api(?:\/\w+)*$

http or https then : then //, then a word, then /api, then maybe some /word

test

Share:
12,657
Nikit Swaraj
Author by

Nikit Swaraj

Updated on June 27, 2022

Comments

  • Nikit Swaraj
    Nikit Swaraj almost 2 years

    I am stuck with one issue. I am trying to hit http://localhost/api/hello/somename

    Now somename could be anything sam or phil,

    now my config file of nginx is below.

    server {
        listen 80 default_server;
        server_name _;
        location ~ ^/api/(.*)$ {
        proxy_pass       http://localhost:8081/api/hello/$1;
       }
    
    }
    

    Where I am wrong ? Can you pls help me to fix. Actually on 8081 container is running.