add response header in nginx

How to Add Response Header in NGINX

Often you may need to add or modify response headers in NGINX, for your website or web application. NGINX allows you to easily modify or set response headers using add_header directive. In this article we will look at how to add response header in NGINX. Updating response headers is very important if you want to perform key tasks such as enable CORS, or disable iframe on your site.


How to Add Response Header in NGINX

Here are the steps to add response header in NGINX.


1. Open NGINX configuration file

Open terminal and run the following command to view NGINX configuration file. If your NGINX configuration file is located somewhere else, please update the path below as per your requirement.

$ sudo vi /etc/nginx/nginx.conf


2. Add response header

We will use add_header directive to add/modify/set response header in NGINX. It is already installed along with NGINX, by default, and does not need additional configuration. Here is the syntax for adding headers using add_header directive.

add_header header_name header_value

For example, if you want to set header X-Frame-Options to SAMEORIGIN, then here is the add_header statement for it.

add_header X-Frame-Options SAMEORIGIN;

You can place the above statement in http, server or location block.

Server Block

server {
   listen 80;
   ...
   add_header X-Frame-Options SAMEORIGIN;
   ...
}

Location Block

location / {
   ...
   add_header X-Frame-Options SAMEORIGIN;
   ...
}

Please note, if you use add_header directive, the response header will not be updated in case the request is sent to other servers via proxy_pass directive. For that purpose, you need to use more_set_header directive. Since it is not installed in NGINX by default, here are the steps to install it. You need to compile the module along with source code while installing NGINX. It cannot be added separately once NGINX is installed.

$ sudo wget 'http://nginx.org/download/nginx-1.17.8.tar.gz'
$ sudo tar -xzvf nginx-1.17.8.tar.gz
$ cd nginx-1.17.8/

# Here we assume you would install you nginx under /opt/nginx/.
$ ./configure --prefix=/opt/nginx --add-module=/path/to/headers-more-nginx-module

$ make
$ make install

Once more_set_headers is installed, you can modify headers using the following syntax.

more_set_headers "header_name:header_value";

Like add_headers, you can place more_set_headers in http, server and location blocks.

location / {
   ...
   more_set_headers "X-Frame-Options:SAMEORIGIN";
   ...
}


3. Restart NGINX Server

Restart NGINX server to apply changes.

$ sudo service nginx restart

In this article, we have looked at two different ways to set response headers in NGINX – using add_header and more_set_header. Please remember that add_header does not change response header if the request passes to other servers via proxy_pass directive. You will need to set response headers if you want to enable CORS or disable iframe on your website.

Also read:

How to Redirect URL to Parent Folder in Apache
How to List Open Ports in Linux
How to Rewrite URL to Subdirectory in Apache
How to Rewrite URL with Parameters in Apache
How to Combine Two Files in Linux

Leave a Reply

Your email address will not be published.