Recently, I’ve had chances to work on nginx-related stuff and I find it quite interesting and fun to work with. At first, it was cool that there is another layer between an incoming request and backend server and it can act as a reverse proxy. I don’t know too much about nginx but I’d like to leave a record of what I’ve learned so far.
NGINX as a reverse proxy
- reverse proxy: A proxy server is a go‑between or intermediary server that forwards requests for content from multiple clients to different servers across the Internet. A reverse proxy server is a type of proxy server that typically sits behind the firewall in a private network and directs client requests to the appropriate backend server.
- nginx can act as a reverse proxy, thus directing an incoming request to a specified backend server.
- it can help us hide the backend server directly interacting with incoming request.
Practical things I’ve tried with nginx
- Set a limit of incoming request size: This is helpful to prevent potential DDos attack with large request or file size. There is nginx configuration keyword
client_max_body_size
that we can set. - Proxy an incoming request to the specified backend server: There is nginx keyword
proxy_pass <URL>
. Specifically, it looks like this:When there is an incoming request at an API endpointlocation /some/path/ { proxy_pass http://127.0.0.1; }
/some/path/
, it’ll get proxied tohttp://127.0.0.1
. - Use
try_files
. There is another way to direct an incoming request to specific files or a named location.When an incoming request comes tolocation / { try_files /system/maintenance.html $uri $uri/index.html $uri.html @mongrel; } location @mongrel { proxy_pass http://mongrel; }
/
endpoint, it checks the existence of files (/system/maintenance.html
,$uri
,$uri/index.html
,$uri.html
) in the order first, and then if the files cannot be found, it redirects the incoming request to the named location (@mongrel
). - Named location. A named location has a
@
prefix and we can define a specific location with its configurations by using a named location.A named location seems often used withlocation @app { proxy_pass http://app; proxy_pass_request_headers on; proxy_set_header ... }
upstream
which definesapp
that’s used in the above location definition.upstream
defines a group of servers that can listen on different ports.upstream app { server 127.0.0.1:8080; }
NGINX as a static server
We can use nginx to serve static content. Recently (February 2025), I deployed this blog to an AWS EC2 instance and used nginx server to deploy static contents of this blog.
A few key points are:
The root directive specifies the root directory that will be used to search for a file.
To obtain the path of a requested file, NGINX appends the request URI to the path specified by the root directive.
An example nginx configuration:
# Example of nginx config file from nginx document
server {
location / {
root /data/www;
}
}
This
location
block specifies the “/” prefix compared with the URI from the request. For matching requests, the URI will be added to the path specified in the root directive.
This example serves static files from
/data/www
directory.