nginx: Send Custom HTTP Headers

In Nginx, you can send custom HTTP headers to the client by using the add_header directive. Here is an example configuration that adds a custom X-My-Header HTTP header to all responses:

server {
listen 80;
server_name example.com;

location / {
# Set the content of the custom header
add_header X-My-Header "Hello, world!";

# ... other configuration options ...
}
}

In this example, the add_header directive is used to set the value of the X-My-Header HTTP header to “Hello, world!”. The add_header directive can be used within the http, server, and location blocks of the Nginx configuration file.

You can also use variables to set the value of the custom header. For example, to set the value of the X-My-Header header to the client’s IP address, you can use the $remote_addr variable like this:

location / {
add_header X-My-Header "Your IP address is $remote_addr";
}

This will set the value of the X-My-Header header to the client’s IP address.

Note that adding custom headers can have security implications, especially if the headers contain sensitive information. Make sure to only add headers that are necessary for your application, and avoid adding headers that could reveal sensitive information or be used for malicious purposes.

Leave a Comment