pass header to proxy server

NGINX Pass Headers from Proxy Server

NGINX is a powerful reverse proxy server that you can use to accept incoming requests to your website and distribute them among one or more web servers. Sometimes, you may need to pass another header to your web server. In this article, we will learn how to pass headers from proxy server to web server.


NGINX Pass Headers from Proxy Server

Here are the steps to pass headers from proxy server to backend web servers.


1. Open NGINX Configuration File

Open NGINX configuration file in a text editor.

$ sudo vi /etc/nginx/nginx.conf


2. Forward Headers from Proxy to Backend Servers

Let us say you want to set a custom header . Modify location block (for / or any other URL pattern as per your requirement) to have the following proxy_set_header directive. Here is the basic format to set header to forward to proxy backend.

location / {
    ...
    proxy_set_header                <header_name> <header_value>;
    proxy_pass_request_headers      on;
}

In the following example, we set a header which contains country code information.

location / {
    ...

    proxy_set_header                HTTP_Country-Code $geoip_country_code;
    proxy_pass_request_headers      on;
}

In the above code you need to specify the header name after proxy_set_header directive along with its value. In the above example, we are forwarding a header named ‘HTTP_Country-Code’. Also, you need to set proxy_pass_request_headers to on.


3. Restart NGINX Server

Restart NGINX server to apply changes.

$ sudo service nginx restart

In this article, we have learnt how to forward headers to proxy backend servers.

Also read:

How to Populate MySQL Table with Random Data
How to Get Query Execution Time in MySQL
How to get File Size in Python
How to Block URL Parameters in NGINX
How to View Active Connections Per User in MySQL

Leave a Reply

Your email address will not be published. Required fields are marked *