Skip to content

CodeCraft Hub

Understanding HTTP Status Codes 200 and 304 - Nginx Configuration Example and Explanation

Programming, Server1 min read

...

You may have experienced receiving response codes 200 or 304 when requesting images from a web browser. HTTP status codes play a crucial role in understanding and indicating the state of communication between web servers and clients. Particularly, status codes 200 and 304 are pivotal in facilitating efficient communication between clients and servers. This article aims to delve into the differences between HTTP status codes 200 and 304, and provide a detailed explanation along with an example of configuring them using Nginx.

HTTP Status Codes 200 and 304

  • 200 OK: The HTTP 200 response code indicates that the client's request has been successfully processed. In this case, the server acknowledges the client's request and successfully returns the requested resource. However, you might often receive this code accompanied by a message like served from memory cache. This signifies that the server provided the requested resource directly from memory cache without actually reading it from disk. This enables faster responses to clients and reduces server load.

  • 304 Not Modified: This status code indicates that the resource the client previously requested has not been modified. It signifies that the client already holds a cached version of the resource and that no new request to the server is necessary. Thus, the server informs the client that there have been no changes, allowing the client to continue using the cached resource.

Nginx Configuration Example and Explanation

To appropriately handle these status codes using Nginx, several measures need to be taken within the configuration file.

In the provided configuration:

  • proxy_cache_path directive specifies the storage location and related settings for image caching.
  • proxy_cache and proxy_cache_valid directives enable image caching and set the duration for keeping responses with 200 and 304 status codes in the cache.
  • proxy_cache_use_stale directive ensures that stale cached data triggers a background request when expired.
  • add_header X-Cached $upstream_cache_status; directive adds a header indicating whether the image was served from the cache.

With these settings, Nginx can cache images and efficiently serve responses to client requests, thereby conserving bandwidth and reducing server load.

© 2024 by CodeCraft Hub. All rights reserved.
Powered by Gatsby