What is the purpose of the add_header directive in nginx?

`add_header` directive is used to add custom HTTP headers in the HTTP response of an Nginx server.

Its effects can be seen in several aspects:

By using the `add_header` directive, you can add custom header fields to the HTTP response. This allows you to pass on custom information to clients or other servers. For example, you could add an `X-Custom-Header` header field to identify a specific application or version number.

   location / {

       add_header X-Custom-Header "Custom Value";

       ...

   }

2. Managing Cache Behavior: By adding specific header fields in the response header, you can control the caching behavior of the client or browser. For example, you can use the `add_header Cache-Control` directive to specify caching policies.

   location / {

       add_header Cache-Control "public, max-age=3600";

       ...

   }

Security settings: The `add_header` directive can also be used to enhance the security of the server. For example, you can add the `Strict-Transport-Security` header to enable strict transport security mechanisms (HTTP Strict Transport Security).

   location / {

       add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";

       ...

   }

In conclusion, the `add_header` directive allows you to add custom header fields to Nginx HTTP responses for purposes such as custom information passing, cache control, and security settings.

bannerAds