# Override Content-Type header with proxied requests

**URL:** <https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406>\
**Category:** NGINX\
**Created:** [August 6, 2014, 2:36pm UTC](https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406 "2014-08-06T14:36:09Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [August 6, 2014, 2:36pm UTC](https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406/1 "2014-08-06T14:36:09Z")

</div>

Please refer this page

> **[Re: Override Content-Type header with proxied requests](https://forum.nginx.org/read.php?2,239473,252358#msg-252358)**
>
> Hi !! i have a Nginx server that operates as a reverse proxy to a my bucket in Amazon S3. Amazon S3 service could deliver contents with wrong Content-Type header, so i would like to override this header by referring to file extension. In other serv

Posted at Nginx Forum:

> **[Override Content-Type header with proxied requests](https://forum.nginx.org/read.php?2,252360,252360#msg-252360)**
>
> Please refer this page http://forum.nginx.org/read.php?2,239473,252358#msg-252358

---

<div class="post-metadata">

**Author:** ![manish-ezest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@manish-ezest](https://www.ruby-forum.com/u/manish-ezest)\
**Post date:** [August 7, 2014, 5:14am UTC](https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406/2 "2014-08-07T05:14:36Z")

</div>

Hello All,

Can somebody answer this question? I am facing this issue from many  
days.

–Manish

Posted at Nginx Forum:

> **[Re: Override Content-Type header with proxied requests](https://forum.nginx.org/read.php?2,252360,252378#msg-252378)**
>
> Please refer this page http://forum.nginx.org/read.php?2,239473,252358#msg-252358

---

<div class="post-metadata">

**Author:** ![manish-ezest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@manish-ezest](https://www.ruby-forum.com/u/manish-ezest)\
**Post date:** [August 7, 2014, 6:11am UTC](https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406/3 "2014-08-07T06:11:04Z")

</div>

Hello,

you can use many values on the map, take a look on its docs.  
One quickly example may be (not tested)

map $uri $custom\_content\_type {  
default “text/html”;  
~(.\*.json)$ “application/json”;  
}

location ~ .json$ {  
proxy\_hide\_header Content-Type;  
add\_header Content-Type $custom\_content\_type;

```
# other proxy configurations like proxy_pass

```

}

Or try to use the types {} directive which is specific to make the  
relationship between extensions and content-type.

---

<div class="post-metadata">

**Author:** ![manish-ezest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@manish-ezest](https://www.ruby-forum.com/u/manish-ezest)\
**Post date:** [August 7, 2014, 7:58pm UTC](https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406/4 "2014-08-07T19:58:20Z")

</div>

Did you tried to only hide the header with  
proxy\_hide\_header Content-Type;  
and let nginx use the mime.types to set the content type?

Do not set the add\_header Content-Type manually.

Let me know the result.

---

<div class="post-metadata">

**Author:** ![manish-ezest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@manish-ezest](https://www.ruby-forum.com/u/manish-ezest)\
**Post date:** [August 7, 2014, 5:57pm UTC](https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406/5 "2014-08-07T17:57:35Z")

</div>

Hello wandenberg,

Thank you for your response. I truly appreciate your help. I tried this  
options given below

######First Method###############

1. Edit nginx.conf and add  
map $uri $custom\_content\_type {  
default “text/html”;  
~(.\*.json)$ “application/json”;  
}

2. Put the custom map in location directive  
location / {  
proxy\_redirect off;  
proxy\_set\_header Host  
[www-aaa.com.s3.amazonaws.com](http://www-aaa.com.s3.amazonaws.com);  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header Referer [www-aaa.com](http://www-aaa.com);  
proxy\_pass [http://www-aaa.com.s3.amazonaws.com/](http://www-aaa.com.s3.amazonaws.com/);  
add\_header Pragma “no-cache”;  
proxy\_cache\_valid 200 302 10m;  
proxy\_read\_timeout 60s;  
proxy\_hide\_header Content-Type;  
add\_header Content-Type $custom\_content\_type;  
}

######Second Method###############

1. Edit nginx.conf and add  
map $uri $custom\_content\_type {  
default “text/html”;  
~(.\*.json)$ “application/json”;  
}

2. Put the custom map in location directive  
location / {  
proxy\_redirect off;  
proxy\_set\_header Host  
[www-aaa.com.s3.amazonaws.com](http://www-aaa.com.s3.amazonaws.com);  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header Referer [www-aaa.com](http://www-aaa.com);  
proxy\_pass [http://www-aaa.com.s3.amazonaws.com/](http://www-aaa.com.s3.amazonaws.com/);  
add\_header Pragma “no-cache”;  
proxy\_cache\_valid 200 302 10m;  
proxy\_read\_timeout 60s;  
location ~ .json$ {  
proxy\_hide\_header Content-Type;  
add\_header Content-Type $custom\_content\_type;  
}  
}

##Third Method####################

1. Enter this in server block of nginx.  
location ~ .json$ {  
types { }  
default\_type application/json;  
}

#################################

1. When I tried first method I put below content in “location / { }” and  
due  
to this the JSON file served as application/json but all other files  
loaded  
as text/html as it was default in the map directive which caused  
image/css  
to not load. I thought it would read the mime.types files and will  
select  
the appropriate Content-Type.  
#########################  
proxy\_hide\_header Content-Type;  
add\_header Content-Type $custom\_content\_type;  
##########################

2. When I tried second and third method, I am getting 404 because it is  
taking docroot as /var/empty

2014/08/07 17:13:22 [error] 14205#0: \*33 open()  
“/var/empty/aaa/bbb/ccc/ddd/eee.json” failed (2: No such file or  
directory),  
client: 5.5.5.5., server: [www-aaa.com](http://www-aaa.com), request: “GET  
/aaa/bbb/ccc/ddd/eee.json HTTP/1.1”, host: “www.aaa.coml”

So my question is where should I put the location ~.json block(in  
location  
/ {} or in server directive)?

**my NGINX.conf** \*\*\*\*\*\*  
user nginx;  
worker\_processes 1;  
error\_log /var/log/nginx/error.log notice;  
pid /var/run/nginx.pid;  
worker\_rlimit\_nofile 30000;  
events {  
worker\_connections 1024;  
}  
http {  
include /etc/nginx/mime.types;  
default\_type application/octet-stream;

```
log_format main '$remote_addr - $remote_user [$time_local] 

```

$request ’  
'“$status” $body\_bytes\_sent “$http\_referer” ’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’;  
log\_format combined\_time ‘$remote\_addr - $remote\_user [$time\_local]’  
'“$request” $status $body\_bytes\_sent ’  
‘“$http\_referer” “$http\_user\_agent” $request\_time’;

```
access_log /var/log/nginx/access.log combined_time;

```

include /etc/nginx/servers/\*.conf;  
}

* * *

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*_ **VHOST SETTING** _  
server {  
listen 80;  
server\_name [www-aaa.com](http://www-aaa.com);  
add\_header Cache-Control off;  
expires 1d;  
root /var/empty;  
error\_log /var/log/nginx/www.aaa.com-error.log;  
access\_log /var/log/nginx/www.aaa.com-access.log  
combined\_time;  
location = /favicon.ico {  
root /www;  
}  
proxy\_intercept\_errors on;  
error\_page 400 401 402 403 404 406 407 408 409 410 411 412 413 414 415  
416  
417 495 496 497 500 501 502 503 504 505 506 507 = /error\_page.pl;  
error\_page 405 =200 $uri;  
location /error\_page.pl {  
fastcgi\_pass 127.0.0.1:8999;  
fastcgi\_param REQUEST\_URI $request\_uri;  
fastcgi\_pass\_header “Status”;  
}

location / {  
proxy\_redirect off;  
proxy\_set\_header Host  
[www.aaa.com.s3.amazonaws.com](http://www.aaa.com.s3.amazonaws.com);  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header Referer [www.aaa.com](http://www.aaa.com);  
proxy\_pass [http://www.aaa.com.s3.amazonaws.com/](http://www.aaa.com.s3.amazonaws.com/);  
}  
}

* * *

NOTE: All my files are served from s3 bucket and not a single files are  
in  
the server.

–Manish

Posted at Nginx Forum:

> **[Re: Override Content-Type header with proxied requests](https://forum.nginx.org/read.php?2,239473,252390#msg-252390)**
>
> Hi !! i have a Nginx server that operates as a reverse proxy to a my bucket in Amazon S3. Amazon S3 service could deliver contents with wrong Content-Type header, so i would like to override this header by referring to file extension. In other serv

---

<div class="post-metadata">

**Author:** ![manish-ezest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@manish-ezest](https://www.ruby-forum.com/u/manish-ezest)\
**Post date:** [August 7, 2014, 8:18pm UTC](https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406/6 "2014-08-07T20:18:00Z")

</div>

Hi Wandenberg,

As you suggested I added “proxy\_hide\_header Content-Type;” in location  
/ {}  
directive and I am getting 200 but how would I check what is the  
Content-Type. I usually check with curl command. But with curl command I  
don’t find the Content-Type since it is hidden now. Is there any other  
way  
to confirm the changes?

[root@LOCALHOST ~]# curl -v

> **[AAA Sorry Page](https://www.aaa.com/aaa/bbb/ccc/ddd/eee/fff.json)**

- Hostname was NOT found in DNS cache
- Trying 5.5.5.5…
- Connected to [www.aaa.com](http://www.aaa.com) (5.5.5.5) port 80 (#0)

> GET /content/unittests/maja-test/essdev-2668/test2.json HTTP/1.1  
> User-Agent: curl/7.36.0  
> Host: [www.aaa.com](http://www.aaa.com)  
> Accept: _/_

\< HTTP/1.1 200 OK

- Server nginx is not blacklisted  
\< Server: nginx  
\< Date: Thu, 07 Aug 2014 18:06:53 GMT  
\< Content-Length: 603  
\< Connection: keep-alive  
\< Keep-Alive: timeout=5  
\< x-amz-id-2:  
6h0Xzm+Sa6+C7fPeocKD0iNJIRhm6thHqa1GJB+aOrOKwrT8T6YL2Lp2M74UlD39  
\< x-amz-request-id: 62A0E211E69B6F9B  
\< x-amz-meta-md5-hash: ab9b7b2c58d3a481e172aea95b1e87a0  
\< Last-Modified: Fri, 25 Jul 2014 13:18:14 GMT  
\< ETag: “ab9b7b2c58d3a481e172aea95b1e87a0”  
\< Accept-Ranges: bytes  
\< Expires: Fri, 08 Aug 2014 18:06:53 GMT  
\<  
{  
JSON CONTENTS
- Connection #0 to host [www.aaa.com](http://www.aaa.com) left intact  
}

–Manish

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,239473,252395#msg-252395](http://forum.nginx.org/read.php?2,239473,252395#msg-252395)

---

<div class="post-metadata">

**Author:** ![manish-ezest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@manish-ezest](https://www.ruby-forum.com/u/manish-ezest)\
**Post date:** [August 8, 2014, 3:28pm UTC](https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406/7 "2014-08-08T15:28:50Z")

</div>

Hello wandenberg,

This procedure is not working for internet explorer. The CSS are not  
loading  
properly. so we have to somehow get the Content-Type in the header. Is  
there  
anything we can use?

–Manish

Posted at Nginx Forum:

> **[Re: Override Content-Type header with proxied requests](https://forum.nginx.org/read.php?2,239473,252416#msg-252416)**
>
> Hi !! i have a Nginx server that operates as a reverse proxy to a my bucket in Amazon S3. Amazon S3 service could deliver contents with wrong Content-Type header, so i would like to override this header by referring to file extension. In other serv

---

<div class="post-metadata">

**Author:** ![manish-ezest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@manish-ezest](https://www.ruby-forum.com/u/manish-ezest)\
**Post date:** [August 7, 2014, 8:31pm UTC](https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406/8 "2014-08-07T20:31:06Z")

</div>

Hi Wandenberg,

Is there any way to show the Content-Type in the header as well?

–Manish

Posted at Nginx Forum:

> **[Re: Override Content-Type header with proxied requests](https://forum.nginx.org/read.php?2,239473,252396#msg-252396)**
>
> Hi !! i have a Nginx server that operates as a reverse proxy to a my bucket in Amazon S3. Amazon S3 service could deliver contents with wrong Content-Type header, so i would like to override this header by referring to file extension. In other serv

---

<div class="post-metadata">

**Author:** ![manish-ezest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@manish-ezest](https://www.ruby-forum.com/u/manish-ezest)\
**Post date:** [August 8, 2014, 5:43pm UTC](https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406/9 "2014-08-08T17:43:58Z")

</div>

One question, your backend are sending the content-type header if you  
made  
the request to it?  
If yes, the value is right?  
If not, I think the problem is on backend configuration.  
You can set it on the nginx using the map like I suggest, but will have  
to  
add all possible file types on the map, like

map $uri $custom\_content\_type {  
default “text/html”;  
~(._.json)$ “application/json”;  
~(._.css)$ “text/css”;  
~(.\*.js)$ “application/javascript”;

# and all other formats

}

But I suggest to check the configuration on Backend server.

---

<div class="post-metadata">

**Author:** ![manish-ezest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@manish-ezest](https://www.ruby-forum.com/u/manish-ezest)\
**Post date:** [August 9, 2014, 4:34am UTC](https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406/10 "2014-08-09T04:34:50Z")

</div>

Hello Wandenberg,

Yes, the back-end(s3 bucket) is sending Content-Type header for each  
request  
and what I heard from the developers that the header is must for the  
site to  
work properly.

The issue started when I got the request of enabling content-type header  
of  
all the json files to application/json. As the default content-type in  
NGINX  
is application/octet-stream, the JSON files are served as  
application/octet-stream. The jquery uses content-type header to  
identify  
the JSOn files and due to the application/octet-stream content type it  
is  
not able to identify the JSOn files.

I tried many options but each time one of the features(html files got  
downloaded, CSS not loaded, CSS problem in internet explorer) got  
impacted.

I will try adding all the mime types to the map directive and will let  
you  
know the result.

Here is my nginx.conf and vhost file for your reference

**my NGINX.conf** \*\*\*\*\*\*  
user nginx;  
worker\_processes 1;  
error\_log /var/log/nginx/error.log notice;  
pid /var/run/nginx.pid;  
worker\_rlimit\_nofile 30000;  
events {  
worker\_connections 1024;  
}  
http {  
include /etc/nginx/mime.types;  
default\_type application/octet-stream;

log\_format main '$remote\_addr - $remote\_user [$time\_local] $request ’  
'“$status” $body\_bytes\_sent “$http\_referer” ’  
‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’;  
log\_format combined\_time ‘$remote\_addr - $remote\_user [$time\_local]’  
'“$request” $status $body\_bytes\_sent ’  
‘“$http\_referer” “$http\_user\_agent” $request\_time’;

access\_log /var/log/nginx/access.log combined\_time;  
include /etc/nginx/servers/\*.conf;  
}

* * *

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*_ **VHOST SETTING** _  
server {  
listen 80;  
server\_name [www-aaa.com](http://www-aaa.com);  
add\_header Cache-Control off;  
expires 1d;  
root /var/empty;  
error\_log /var/log/nginx/www.aaa.com-error.log;  
access\_log /var/log/nginx/www.aaa.com-access.log  
combined\_time;  
location = /favicon.ico {  
root /www;  
}  
proxy\_intercept\_errors on;  
error\_page 400 401 402 403 404 406 407 408 409 410 411 412 413 414 415  
416  
417 495 496 497 500 501 502 503 504 505 506 507 = /error\_page.pl;  
error\_page 405 =200 $uri;  
location /error\_page.pl {  
fastcgi\_pass 127.0.0.1:8999;  
fastcgi\_param REQUEST\_URI $request\_uri;  
fastcgi\_pass\_header “Status”;  
}

location / {  
proxy\_redirect off;  
proxy\_set\_header Host [www.aaa.com.s3.amazonaws.com](http://www.aaa.com.s3.amazonaws.com);  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header Referer [www.aaa.com](http://www.aaa.com);  
proxy\_pass [http://www.aaa.com.s3.amazonaws.com/](http://www.aaa.com.s3.amazonaws.com/);  
add\_header Access-Control-Allow-Origin \*;  
}  
}

Posted at Nginx Forum:

> **[Re: Override Content-Type header with proxied requests](https://forum.nginx.org/read.php?2,239473,252439#msg-252439)**
>
> Hi !! i have a Nginx server that operates as a reverse proxy to a my bucket in Amazon S3. Amazon S3 service could deliver contents with wrong Content-Type header, so i would like to override this header by referring to file extension. In other serv

---

<div class="post-metadata">

**Author:** ![manish-ezest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@manish-ezest](https://www.ruby-forum.com/u/manish-ezest)\
**Post date:** [August 10, 2014, 5:35am UTC](https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406/11 "2014-08-10T05:35:45Z")

</div>

I wrote a module to process the problem  
ena.[GitHub - flygoast/ngx\_http\_types\_filter: A NGINX module to change the `Content-Type` output header depending on an extension variable according to a condition specified in the 'if' clause.](https://github.com/flygoast/ngx_http_types_filter)

our configuration in production like this:  
location / {  
types\_filter $uri;  
…  
proxy\_pass http://down\_server;  
}

---

<div class="post-metadata">

**Author:** ![manish-ezest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@manish-ezest](https://www.ruby-forum.com/u/manish-ezest)\
**Post date:** [May 24, 2015, 1:25pm UTC](https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406/12 "2015-05-24T13:25:21Z")

</div>

Dear Manish!

Could you please share the file with mime types that you use to include  
in a map?

manish-ezest wrote in post #1155024:

> Hello Wandenberg,
> 
> Thanks for your help. Finally it is working. I included all the mime  
> types  
> in a file and included inside map directive and used it in the location  
> / {}  
> directive with proxy hide parameter like you suggested.
> 
> –Manish
> 
> Posted at Nginx Forum:  
> [http://forum.nginx.org/read.php?2,239473,252532#msg-252532](http://forum.nginx.org/read.php?2,239473,252532#msg-252532)

---

<div class="post-metadata">

**Author:** ![manish-ezest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@manish-ezest](https://www.ruby-forum.com/u/manish-ezest)\
**Post date:** [August 13, 2014, 12:19pm UTC](https://www.ruby-forum.com/t/override-content-type-header-with-proxied-requests/231406/13 "2014-08-13T12:19:15Z")

</div>

Hello Wandenberg,

Thanks for your help. Finally it is working. I included all the mime  
types  
in a file and included inside map directive and used it in the location  
/ {}  
directive with proxy hide parameter like you suggested.

–Manish

Posted at Nginx Forum:

> **[Re: Override Content-Type header with proxied requests](https://forum.nginx.org/read.php?2,239473,252532#msg-252532)**
>
> Hi !! i have a Nginx server that operates as a reverse proxy to a my bucket in Amazon S3. Amazon S3 service could deliver contents with wrong Content-Type header, so i would like to override this header by referring to file extension. In other serv
