
To publish an application with Nginx, you declare a server that listens under the public name, a location that sends requests to the application with proxy_pass, and four headers that tell the application who is really calling it. Without these headers, the application believes everything comes from 127.0.0.1 over HTTP. This lesson is the optional Nginx module of the training: it takes up the publication gestures already seen with Traefik, in Nginx syntax.
What you will learn
The lesson follows the order of a publication: route, forward, spread, then secure with a certificate.
- Publish an application under a name with
proxy_pass. - Preserve the path and the host of the original request.
- Forward the
X-Forwarded-*headers the application expects. - Declare an
upstreamand understand its passive probes. - Obtain a certificate with Nginx's ACME module (in preview) or with certbot.
Prerequisites
This lesson assumes you understand the path of a request and have already seen an Nginx configuration file.
- The path from the DNS name to the backend, SNI and the
Hostheader: Publish a web service. - Automatic certificates and ACME: Automatic certificates.
- Nginx installation and basics: Nginx: practical guide (in French).
How does Nginx route a request to an application?
Nginx first chooses the server block from the requested name, then the location block from the path, and proxy_pass sends the request to the backend. It is the same split as Traefik's router, with one difference: everything is written in configuration files that you test then reload, with no automatic discovery of containers.
server { listen 80; server_name app.lab.test;
location / { proxy_pass http://127.0.0.1:8080; }}This file publishes the application listening on port 8080 of the machine under the name app.lab.test. The server_name serves to choose the right block when several names share the same address, and location / catches all paths.
Which headers should you forward to the application?
Forward the original host, the client's address, the chain of relays and the protocol used. By default, Nginx rewrites Host with the name and port of the backend and says nothing about the real client. The application can then neither generate the right absolute addresses, nor log the right origin, nor know that the client arrived over HTTPS.
location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;}The $proxy_add_x_forwarded_for variable appends the client's address to the chain already received instead of overwriting it: this is what preserves the trace when another relay sits upstream. Keep in mind that a client can send an X-Forwarded-For header itself: the application must only trust it behind a proxy it knows.
How do you spread the load with upstream?
An upstream block names a group of servers, and proxy_pass designates it by its name. The default distribution goes round the servers of the group in turn. With open source Nginx, failure detection is passive: a server is set aside when requests fail, according to max_fails and fail_timeout, but Nginx does not send a probe request on its own.
upstream app_backend { server 127.0.0.1:8080 max_fails=2 fail_timeout=10s; server 127.0.0.1:8081 max_fails=2 fail_timeout=10s;}This difference matters in operations: with Traefik, an active probe removes a backend before a user meets it; with open source Nginx, it is the first failing requests that trigger the removal. Active probes (health_check) belong to Nginx's commercial offer.
How do you obtain a certificate?
Two paths exist: Nginx's ACME module (in preview), which renews by itself, or the external tool certbot, which modifies the configuration and schedules renewal. In both cases, the principle is that of the lesson on automatic certificates: prove to the authority that you control the name, then retrieve a certificate renewed before it expires.
The ngx_http_acme_module module is configured in Nginx itself: an acme_issuer block describes the authority (ACME directory address, contact, state_path where the account key and the certificates are kept), an acme_shared_zone shares the state between processes, and the HTTPS server declares acme_certificate then reads the certificate in $acme_certificate and $acme_certificate_key. A server on port 80 must remain reachable for the HTTP-01 challenge. The module also accepts the tls-alpn-01 challenge.
How do you validate before reloading?
Always test the configuration with nginx -t before reloading it, then check the real response with curl. Reloading an invalid configuration is refused and keeps the old one, but only a test request proves that the path, the host and the headers arrive as planned at the backend.
nginx -tcurl -sS -H 'Host: app.lab.test' http://127.0.0.1/If the response is not the expected one, read Nginx's error log: a 502 indicates an unreachable backend, a 504 a backend that is too slow, and a 404 a server or location block that does not match the name or the path.
Practice: publish an application with Nginx
The lab starts from a small application running on the machine, with an Nginx container and its starting configuration. It asks you to configure Nginx as a reverse proxy of app.lab.test to the local application, to preserve the path and the host of the request and to forward the headers expected from an application behind a proxy.
Key points
Nginx publishes an application with a server, a location and proxy_pass.
server_namechooses the block,locationchooses the path.- Forward
Host,X-Real-IP,X-Forwarded-ForandX-Forwarded-Proto. - An open source
upstreamdetects failures passively. - The ACME module (in preview, so not a stable status) or certbot renew certificates.
nginx -tvalidates,curlproves.
Knowledge check
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Nginx upstream: spread the load and remove failures
A new full quiz with random questions
Work again only on the questions you missed