Override Content-Type header with proxied requests

manish-ezest nginx-forum at nginx.us
Thu Aug 7 15:57:03 UTC 2014


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;
        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;
        proxy_pass         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;
        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;
        proxy_pass         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, 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;
        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;
        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;
        proxy_pass         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: http://forum.nginx.org/read.php?2,239473,252390#msg-252390



More information about the nginx mailing list