Bennett_bear
Bennett_bear

Reputation: 121

How to set content-type in proxy_pass?

On a website traffic is flowing in via http for location /instance we need the traffic to be secure using SSL and https. When redirecting to https, the request content-type is "text/xml" when it actually should be "application/json". Should we explicitly set something in the proxy header to "application/json"? We have tried add_header Content-type "application/json" in the http configuration and that didn't make a difference. What are we doing wrong?

Http configuration:

location /instance {
proxy_pass https://instancehost:9443/instance;
proxy_redirect http://localhost.com https://localhost.com;
proxy_set_header X-xmgr-proxy-domain http://localhost.com:80;
proxy_set_header X-xmgr-proxy /instance;
proxy_set_header Access-Control-Allow-Origin "*";
proxy_set_header Access-Control-Allow-Headers "Origin, X-Requested-With,       Content-Type, Accept";
proxy_ssl_certificate /data/nginx/certs/abc.crt;
proxy_ssl_certificate_key /data/nginx/certs/abc.key;
proxy_ssl_trusted_certificate /etc/pki/tls/certs/abc-bundle.crt;
proxy_ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;
proxy_hide_header Content-Type;
add_header Content-type "application/json"
}

Setting the content-type in the header did not work as we are still receiving a 204 error.

https configuration:

location /instance {
proxy_pass           https://instancehost.com:9443/instance;
proxy_set_header     X-xmgr-proxy-domain https://localhost.com:443;
proxy_set_header     X-xmgr-proxy /instance;
proxy_set_header     Access-Control-Allow-Origin "*";
proxy_set_header     Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
proxy_ssl_certificate /data/nginx/certs/abc.crt;
proxy_ssl_certificate_key /data/nginx/certs/abc.key;

}

Upvotes: 12

Views: 28972

Answers (1)

Horacio
Horacio

Reputation: 2965

I think that the problem here is that you doing a add_header and add_header seems add that header to the response (when request come back from backends to your client), and you want set for your backends.

Syntax:     add_header name value [always];
Default:    —
Context:    http, server, location, if in location
Adds the specified field to a response header provided that the response code equals 200, 201, 204, 206, 301, 302, 303, 304, or 307. A value can contain variables. 

You should this line in your conf

proxy_set_header content-type "application/json";

all proxy_* will set for the request (from client to backend way)

Syntax: proxy_set_header field value; Default:

proxy_set_header Host $proxy_host;

proxy_set_header Connection close;
Context:    http, server, location

Allows redefining or appending fields to the request header passed to the proxied server. The value can contain text, variables, and their combinations. These directives are inherited from the previous level if and only if there are no proxy_set_header directives defined on the current level. By default, only two fields are redefined: 

Upvotes: 8

Related Questions