HTTP Requests in Nginx: Methods, Paths and Headers
Read a request from the browser
When a browser asks for a resource, it sends an HTTP request. Nginx can use the destination, request method, path and header fields to decide what to do. In this example a client asks for a text file from the Chapter 6 loopback lab. The following transcript is original, illustrative HTTP/1.1 text; real message lines use CRLF and end their header section with an empty line.
GET /notes.txt?edition=2 HTTP/1.1Host: localhost:18086Accept: text/plainGET is the method, /notes.txt?edition=2 is the request target and HTTP/1.1 identifies the message format. The path is /notes.txt; the query is edition=2. Host identifies the intended host and optional port, while Accept expresses a preference for a plain-text representation. The blank line ends the fields. There is no request body here. A URL fragment, such as #summary, stays with the client and is not included in the normal HTTP request target.
What common methods mean
GET asks for a representation. HEAD asks for corresponding response metadata without response content. POST asks the target to process supplied content. PUT requests creation or replacement of the target state; DELETE requests removal of the target association. OPTIONS asks about communication options; PATCH expresses partial modification where supported. The server or application must actually implement the requested behavior; naming a route does not make it accept every method.
POST /draft HTTP/1.1Host: localhost:18086Content-Type: application/jsonContent-Length: 15
{"title":"Oak"}The JSON text is 15 ASCII bytes and the Content-Length describes those bytes, not the header lines. Content-Type tells the receiving application how to interpret the content. The path does not contain the JSON. Nginx can enforce a request size limit, but the application still needs to validate the input.
Here 16k is 16 × 1024 bytes. For a request with a known body length greater than this limit, Nginx can reject it with 413 before application processing. The documented default is 1m; choose a size suited to the endpoint rather than removing the limit.
Read Nginx request metadata
location = /inspect { default_type text/plain; return 200 "method=$request_method\npath=$uri\nquery=$args\n";}This endpoint uses the rewrite module’s return directive to generate text, not to invoke a shell. $request_method identifies the method; $uri is the current normalized path and can change during processing; $args holds current query arguments. $request_uri instead represents the original URI including arguments. In the lab, only synthetic inputs are used. The output shows why a query string does not automatically change the file selected by the static handler.
curl --noproxy '*' --max-time 2 'http://127.0.0.1:18086/inspect?edition=2'A Host or X-Forwarded-For value can be supplied by a client. In the baseline with no address-rewriting module configured, $remote_addr represents the network peer, not necessarily the ultimate end user. Never make an authorization decision from an arbitrary forwarding header or echo real request data into public diagnostics.
Practice: annotate a request
Given the first transcript, identify the method, target, path, query and host. Predict what happens to the path when edition changes to 3. Then identify which header describes what the client wants rather than what it sends. Check the companion manual task and quiz. Next: interpret response statuses and headers.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTTP request anatomy
Check the concepts taught in this lesson.
What is the path in /notes.txt?edition=2?
Which field expresses the preferred response media type?
How many bytes does the ASCII body {"title":"Oak"} occupy?
What can happen when a declared body exceeds client_max_body_size?
What should a server conclude from an arbitrary X-Forwarded-For value?