HTTP Responses in Nginx: Status Codes, Headers and Content
Read the result of a request
After receiving a request, a server sends a response indicating what happened. An HTTP/1.1 response contains a status line and fields followed by an empty line and, when allowed, content. This example represents a text file containing four characters and a line feed. It is an illustrative transcript, not captured traffic; ordinary date and server fields are omitted for clarity.
HTTP/1.1 200 OKContent-Type: text/plainContent-Length: 5
Oak!200 means the request succeeded. Content-Type describes the representation; Content-Length counts the content bytes here. A real server may provide additional fields. Do not equate 200 with correct application behavior: check that the body matches what the client requested.
Distinguish status families
The first digit gives the broad class: 1xx informational, 2xx successful, 3xx redirection-related, 4xx client-error class and 5xx server-error class. A 204 has no response content. A 304 is a conditional-cache response, not an ordinary redirect to a new URL. A 401 concerns missing or invalid authentication and requires an authentication challenge; 403 means the request was understood but refused. A 404 means the target was not found or the server chooses not to disclose it. A 502 indicates an invalid upstream response when acting as a gateway, while 504 concerns an upstream timeout. Status alone rarely establishes a root cause.
location = /notes.txt { }location = /old-notes { return 301 /notes.txt; }location = /missing.txt { return 404 "Not found\n"; }The first location uses the static handler with the server document root. The second tells the client to use a new location; the third deliberately sends a 404 with a small text body. A client following the redirect issues another request. A 301 is permanent and can be cached; a 302 is temporary. For method preservation, 307 and 308 have stronger semantics than 301 and 302, which may turn POST into GET. Do not use redirect status codes interchangeably.
curl --noproxy '*' --max-time 2 -i http://127.0.0.1:18086/old-notescurl --noproxy '*' --max-time 2 -i http://127.0.0.1:18086/missing.txtThe first is expected to return 301 with a Location identifying /notes.txt (Nginx can construct an absolute URL). The second should return 404 with Not found followed by a line feed. curl -i prints response fields and content; unlike -I, it does not switch the request to HEAD. Do not follow an unknown external redirect automatically.
Response metadata and negotiation
Accept describes the types a client prefers; Content-Type describes the representation actually sent. A server may offer several representations and select one, or may ignore an Accept preference where permitted. The static lab has only one representation for the notes file. A server that varies a cacheable response by Accept should normally advertise this using Vary: Accept. Content negotiation is not automatic application translation.
add_header X-Lab-Chapter 6 always;add_header affects response fields. The always option lets this marker appear even on the deliberate 404. Without always, the directive adds the field only for its documented set of statuses. On the baseline inheritance model, adding a new add_header in a nested location can replace the inherited list; newer builds have additional inheritance controls. This marker is for the synthetic lab, not a security guarantee.
curl --noproxy '*' --max-time 2 -I http://127.0.0.1:18086/notes.txtExpect a 200 and text/plain metadata without content. The Content-Length may still describe the corresponding GET content. HEAD is useful for checking status and metadata, not for proving that the body is correct.
Never return authentication headers, session cookies or private configuration merely to debug a response. Avoid adding conflicting Content-Length or Content-Type fields by hand. Let the server handle framing and verify the actual returned values.
Practice: check three paths
Design checks for /notes.txt, /old-notes and /missing.txt: include status, any required Location, and the expected marker. Explain why checking the body matters. Next: learn when requests share a connection.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTTP response interpretation
Check the concepts taught in this lesson.
What does a 301 normally communicate?
What is the key difference between curl -I and curl -i?
Which field helps caches distinguish representations selected by Accept?
Why use always in add_header X-Lab-Chapter 6 always?
Which response is not an ordinary redirect to a new URL?