How to set content type to proxy_pass?

On the website, the traffic flows through http for the location / instance, we need the traffic to be secure using SSL and https. When redirecting to https, the content type of the request is "text / xml", when in fact it should be "application / json". Should I explicitly set something in the proxy header to "application / json"? We tried add_header Content-type "application / json" in http config and it didn't help. What are we doing wrong?

Http config:

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 didn't work as we are still getting the 204 error.

Https config:

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;

}

      

+3


source to share


1 answer


I think the problem is that you are doing add_header and add_header seems to be adding this header to the response (when the request comes back from the backend to your client) and you want to 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. 

      

This line should be in your conf

proxy_set_header content-type "application/json";

      



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

Syntax: value of the proxy_set_header field; 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: 

      

+2


source







All Articles