Skip to content
Français
Account: not signed in, sync available My learning
Select theme
Français

Publish an application with Nginx: proxy_pass, headers and certificates

By updated

Improve this page

Nginx logo

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 upstream and 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.

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.

Terminal window
nginx -t
curl -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_name chooses the block, location chooses the path.
  • Forward Host, X-Real-IP, X-Forwarded-For and X-Forwarded-Proto.
  • An open source upstream detects failures passively.
  • The ACME module (in preview, so not a stable status) or certbot renew certificates.
  • nginx -t validates, curl proves.

Knowledge check

Knowledge check

Check what you have learned with this interactive quiz

8 questions
8 min
70% to pass

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

Is this project useful to you?

The guides, courses, quizzes and labs stay free. If you use them regularly, you can contribute to their upkeep and development.

Become a supporting member (new tab) One-time support (new tab) Learn more

Follow the project on LinkedIn