How to proxy_pass in NGINX

How to proxy_pass in NGINX

NGINX is a web server that can be used as a reverse proxy. The request that is received by NGINX is sent to the proxied server, then the response is sent back to the client. To do reverse proxy, NGINX uses proxy_pass directive inside a specified location. This article will show you how.

How to Use proxy_pass

Specifiy a location inside a server directive. Use proxy_pass with proxied server URI as parameter. For example:

server {
	server_name  localhost;
	listen 80;

    location /my_api {
		proxy_pass http://192.168.100.17:5005;
	}
}

This example defines http://192.168.100.17:5005 as the proxied server. It means that all requests to the NGINX with request URI start with /my_api will be proxied to this address. The address can be a domain name or an IP address. See below for more examples of proxy_pass usage. Give comments if you have more examples.

How to proxy_pass to an Address

	location /my_api {
		proxy_pass http://192.168.100.17:5005;
	}

This will proxy request that start with /my_api to the address. For example:
/my_api proxied to http://192.168.100.17:5005/my_api
/my_api_suffix proxied to http://192.168.100.17:5005/my_api_suffix
/my_api/something proxied to http://192.168.100.17:5005/my_api/something

You can define the address with a URI or address like this:

	location /my_api {
		proxy_pass http://192.168.100.17:5005/some_path;
	}

If the address has a path, the request URI in the location directive will be replaced with the URI in the proxied address. For example:
/my_api proxied to http://192.168.100.17:5005/some_path

How to proxy_pass to a File

We can proxy the request to a file.

	location /page {
		root /path/to/your/directory;
		try_files /mypage.html 404;
	}

When client send a request to /page, NGINX will respond with file mypage.html under /path/to/your/directory. It will respond 404 if file is not found.

How to proxy_pass With location Match Regex

The location can be defined to match a regex pattern. For example:

	location ~ ^/(my_api|our_api) {
		proxy_pass http://192.168.100.17:5005;
	}

The request with URI that match with the pattern will be proxied to the address.
/my_api proxied to http://192.168.100.17:5005/my_api
/our_api proxied to http://192.168.100.17:5005/our_api

How to proxy_pass With Regex Search in location

We can use regex to search some part of the request URI and put it in proxied address URI.

	location ~ ^/some_prefix/(.*)$ {
        proxy_pass http://192.168.100.17:5005/$1$is_args$args;
    }

This will get the URI after /some_prefix/. The idea is to remove /some_prefix/ in the destination URI. Example:
/some_prefix/my_api proxied to http://192.168.100.17:5005/my_api

Note that we put $is_args$args on the address. It is to pass parameters such as ?param=value on request URI to the destination address. Without this, the parameters will not be passed.

Whitelist Client IP Address

We can whitelist an IP address in a location directive.

	location /my_api {
        allow 192.168.0.0/16;
        deny all;
		proxy_pass http://192.168.100.17:5005;
	}

In the example, only a client with IP ranged between 192.168.0.0 - 192.168.255.255 can access the api. Other IP address will get 403.

Blacklist Client IP Address

We can also blacklist an IP address.

	location /my_api {
        deny 192.168.1.17;
		proxy_pass http://192.168.100.17:5005;
	}

A client with IP 192.168.1.17 will get 403.


See also

comments powered by Disqus