designwithfocus

Domains, hosting & DNS

What Is Nginx Used For? Files, Proxy, and TLS

By Design With Focus Team 5 min read
A server block listing root, proxy_pass, and ssl_certificate

Short answer

Nginx is an HTTP web server, a reverse proxy, a content cache, and a load balancer. The site also calls it a TCP and UDP proxy and a mail proxy. The beginner's guide serves files with root and proxies with proxy_pass. The SSL module adds HTTPS and is not built unless you enable it. Load balancing defaults to round-robin.

Last verified against the sources listed below.

Key takeaways

  • nginx.org calls nginx an HTTP server, reverse proxy, content cache, and load balancer.
  • The beginner's guide serves files by adding the request URI to a root path.
  • proxy_pass sets the protocol and address of the server that receives the request.
  • The SSL module provides HTTPS and requires OpenSSL. It is not built by default.
  • An upstream block plus proxy_pass distributes requests. The default method is round-robin.

What is nginx used for is a question about jobs, not about a single setting. The nginx site says nginx is an HTTP web server, reverse proxy, content cache, load balancer, TCP/UDP proxy server, and mail proxy server. It was originally written by Igor Sysoev and is distributed under the 2-clause BSD license.

The beginner’s guide assumes nginx is already installed and shows three tasks: serve static files, proxy requests, and pass work to a FastCGI application. Web hosting is the service that usually runs a program like this. The guide is about the program’s config file.

What does the server process look like?

Nginx runs one master and several workers. The beginner’s guide says the master reads and evaluates configuration and maintains the workers. The workers process requests. The number of workers is set in the config and can be fixed or adjusted to the number of CPU cores with worker_processes.

You start nginx by running the executable. After that, nginx -s sends a signal: stop, quit, reload, or reopen. quit is a graceful shutdown.

reload applies a new config. The master tests the new file first. If the file is valid, it starts new workers and tells the old ones to stop accepting connections and finish the requests they already have.

If the file is invalid, it keeps the old config.

The default config path is nginx.conf in /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx. A line after # is a comment. A simple directive ends with a semicolon. A block directive ends with braces.

How does it serve files?

Static files are a root path plus the request URI. The beginner’s guide builds a server that reads HTML from /data/www and images from /data/images.

A location / block with root /data/www takes the URI and appends it to that root. A request for /some/example.html maps to /data/www/some/example.html. A longer prefix wins.

location /images/ with root /data maps /images/example.png to /data/images/example.png. If the file is not there, nginx sends a 404.

IANA registers 404 as Not Found. MDN says that status means the server cannot find the resource. The beginner’s guide points at error.log under /usr/local/nginx/logs or /var/log/nginx when a request does not do what you expected.

A favicon generator makes the icon file. Nginx’s part is the static mapping above: put the file where root and location say it is. The generator does not write nginx.conf.

What is the proxy job?

proxy_pass forwards the request to another server. The proxy module says the directive sets the protocol and address of a proxied server and an optional URI. The protocol may be http or https.

The beginner’s sample listens on port 80, sends ordinary requests to http://localhost:8080, and still serves files ending in .gif, .jpg, or .png from disk. A regular-expression location starts with ~. Nginx checks prefix locations, remembers the longest prefix, then checks regular expressions. A matching expression wins.

FastCGI is the same idea with different directives. The guide uses fastcgi_pass and fastcgi_param when the upstream speaks FastCGI, for example a process on localhost:9000. proxy_pass is for an HTTP upstream. fastcgi_pass is not a synonym you can swap in without changing the parameters.

How do load balancing and TLS fit?

Load balancing is an upstream group. TLS is a separate module. The table ties each job to a directive the docs actually show.

Job Directive the docs show What that directive does
Static files root inside location The URI is appended to the root path to form a file path
Reverse proxy proxy_pass Sets the protocol and address of the proxied server
Load balancing upstream and proxy_pass Sends requests to the named group. The default method is round-robin
HTTPS listen 443 ssl, ssl_certificate, ssl_certificate_key The SSL module turns on HTTPS for that server

The load-balancing guide lists round-robin, least-connected, and ip-hash. least_conn and ip_hash are written inside the upstream block. least_time is a further method, with header or last_byte as its argument. Reverse proxy load balancing also covers HTTPS, FastCGI, uwsgi, SCGI, memcached, and gRPC, using the matching *_pass directive.

The SSL module says it provides HTTPS, is not built by default, and requires the OpenSSL library. OpenSSL’s download page publishes that library. The nginx sample sets ssl_protocols to TLSv1.2 and TLSv1.3, then points ssl_certificate and ssl_certificate_key at certificate and key files.

Copying those lines into a build without the module will not create HTTPS.

How the internet works is the path a request takes before it reaches this server. Nginx’s config decides what happens next: a file from disk, a proxied response, a choice among upstreams, or a TLS handshake when the SSL module is present.

Frequently asked questions

What is an nginx proxy?

A proxy receives requests, passes them on, and sends the responses back. The proxy_pass directive sets the protocol and address of that server, plus an optional URI. The protocol can be http or https. The beginner's sample sends ordinary requests to localhost port 8080 and keeps some image files on local disk.

How does nginx load-balance requests?

List the servers in an upstream block and point proxy_pass at the group. With no method set, the default is round-robin. least_conn picks the server with the fewest active connections. ip_hash uses the client address so a client tends to stay on one server. The guide's weight example sends 3 of every 5 requests to the heavier server.

Does every nginx build speak HTTPS?

Not unless the SSL module was compiled in. The module page says it provides HTTPS, is not built by default, and requires the OpenSSL library. A sample listens on port 443 with the ssl parameter, plus certificate and key files. Check that the build includes the module before you copy those lines.

Where does the configuration file live?

The default file is nginx.conf, placed in /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx. A simple directive ends with a semicolon. A block ends with braces. Server sits inside http, and location sits inside server. A reload tests the new file, starts new workers if it is valid, and keeps the old config if it is not.

What does a missing file look like to the client?

The beginner's guide says a missing file produces a 404 response. IANA registers 404 as Not Found. MDN says 404 means the server cannot find the resource, and that the code does not say whether the gap is temporary or permanent. A wrong root path fails this way even when nginx is running. Check location and root, then read the error log the guide names.

Sources