3

I would like to ask about some configurations for Nginx; How to setup regular expression in Nginx location block? this is my configuration

location ~ ^/web/api/v1/([A-Za-z]+) {
    proxy_pass http://localhost:5000/$1;
}

So, the use case for this config is when i type localhost/web/api/v1/apple it will routed to localhost:5000/apple, localhost/web/api/v1/pineapple it will routed to localhost:5000/pineapple, and so on. Note: the apple and pineapple only example path name.

Thank You

Alexander Tolkachev
  • 4,608
  • 3
  • 14
  • 23
Tetsu
  • 61
  • 1
  • 5
  • Have you encountered a problem with the above? You should place a `$` on the end of the regular expression, as `$1` should contain the entire tail of the URI. – Richard Smith Mar 05 '20 at 12:44
  • Hi Richard, yes i encountered error 502, i am still confuse how to handle this challenge – Tetsu Mar 05 '20 at 12:50
  • The error log should contain details. – Richard Smith Mar 05 '20 at 12:51
  • Hi Richard, here is my error log: [error] 11296#4028: *1006 no resolver defined to resolve localhost, client: 127.0.0.1, server: localhost, request: "GET /web/api/v2/apple HTTP/1.1", host: "localhost" – Tetsu Mar 05 '20 at 12:53
  • You should try using `127.0.0.1` instead of `localhost`, or alternatively, define a [`resolver`](http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver) which is usually required when the `proxy_pass` statement contains a variable. – Richard Smith Mar 05 '20 at 12:55
  • Alright, i will do it first, i will let you know the result – Tetsu Mar 05 '20 at 12:57
  • Big thanks for you Richard, you've been very helpful – Tetsu Mar 05 '20 at 13:00

1 Answers1

3

This answer credit to @Richard Smith;

I am change the config become:

location ~ ^/web/api/v1/([A-Za-z]+)$ {
   proxy_pass http://127.0.0.1:5000/$1;
}

localhost become 127.0.0.1

Tetsu
  • 61
  • 1
  • 5