Antony Budianto

Antony Budianto

Nginx Proxy Pass

So you already have frontend application running, then you need to access your backend server for data. You want to avoid CORS on the browser so you need to create an endpoint for the backend.

For example, you have and want to expose endpoint for backend via

With nginx, you can do it like this:

location ~ ^/api/ {
    proxy_pass http://hellobackend/;

For example, there is request, it will get proxied to hellobackend/ping

Notice the missing api on proxied request? Yes, anything in the location block will get cut, the rest will be sent to proxy pass.

Request URI

If you want the api part, you can manually add /api on the proxy pass, or use $request_uri

location ~ ^/api/ {
    proxy_pass http://hellobackend$request_uri;

This will proxy to hellobackend/api/ping

Capturing group

You can also use capturing group and use it to pass to proxy:

location ~ ^/api/(.*) {
    proxy_pass http://hellobackend/$1;


~ -> for case-sensitive string match

hellobackend is your backend host/IP

$1 -> the matched string from RegExp, this one -> (.*). It can be multiple, such as $2, $3, and so on.