HTTP Headers

HTTP headers are strings of name:value pairs (name and value separated by a colon) that are used to communicate additional information and metadata between a client (usually a web browser) and an application on a server. This information may include how data is encoded, how the server should handle data, the document’s age, and more.

Response headers (from the application to the client) are part of the application’s response and may contain information like the server location, server name, etc.

Request headers (from the client to the application) are helpful if you want to customize your application. A few examples are:

  • Reporting different types of content per the client region using CF-IPCountry.
  • Using CF-Connecting-IP to record the client IP in the application’s activity log.
  • Performing debugging or monitoring user sessions using X-Request-ID.

Request Headers Added by Kinsta

When you deploy an application on Kinsta, the following headers are used to send information to the application:

  • Host: The host header specifies the hostname and port number of the server that is being requested.
  • X-Request-ID: Used to identify a request between the client and the server. This header can be useful for debugging purposes.
  • X-Real-IP: Specifies the real IP address of the client that made the request. This header is commonly used by reverse proxies and load balancers to indicate the original client IP address.
  • X-Forwarded-For: Used to specify the client’s IP address and can also contain a list of IP addresses if the request passes through multiple proxies.
  • X-Forwarded-Host: Specifies the original host requested by the client.
  • X-Forwarded-Port: Specifies the port used by the client to make the request.
  • X-Forwarded-Proto: Specifies the protocol the client uses to make the request, such as “HTTP” or “HTTPS.”
  • X-Forwarded-Scheme: Specifies the scheme the client uses to make the request, such as “HTTP” or “HTTPS.”
  • X-Scheme: The header is similar to X-Forwarded-Scheme, and is used to indicate the original scheme requested by the client.
  • X-Original-Forwarded-For: Specifies the original client IP address in a request that has passed through multiple proxies.
  • Accept-Encoding: Specifies the encoding types that the client can accept in the response, such as gzip or deflate.
  • CF-Ray: A unique identifier generated by the Cloudflare service for each request.
  • CF-Visitor: Used by Cloudflare to transmit information about the client’s browser and device to the server.
  • CF-EW-Via: Used by the Cloudflare Edge Workers service to indicate that an Edge Worker processed a request.
  • CDN-Loop: Used by some content delivery networks to indicate that a request is stuck in a loop and needs to be terminated.
  • Accept: Specifies the media types that the client can handle in the response, such as text/html or application/json.
  • User-Agent: Specifies the client’s user agent string, which can be used to identify the client’s browser and operating system.
  • CF-Connecting-IP: Specifies the IP address of the client that is connecting to the server through the Cloudflare network.
  • CF-Worker: Used to indicate that a Cloudflare worker processed a request.
  • CF-IPCountry: Specifies the country code of the client’s IP address, as determined by Cloudflare.

Viewing Request Headers

While response headers can be viewed with tools like Kinsta’s HTTP Status and Redirect Checker, curl, Postman, Insomnia, etc., viewing request headers is a bit different. To view request headers, you can use your application’s code to see and report the headers sent to it. Here’s an example of this using Python:

from http.server import BaseHTTPRequestHandler, HTTPServer
import loggingclass RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
logging.info("GET request,nPath: %snHeaders:n%sn",
str(self.path), str(self.headers))
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
message = f"{self.headers}"
self.wfile.write(bytes(message, 'utf-8'))
returndef run(server_class=HTTPServer, handler_class=RequestHandler, port=8080):
logging.basicConfig(level=logging.INFO)
server_address = ('', port)
httpd = server_class(server_address, handler_class)
logging.info('Starting httpd...n')
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
logging.info('Stopping httpd...n')if __name__ == '__main__':
from sys import argvif len(argv) == 2:
run(port=int(argv[1]))
else:
run()

Was this article helpful?