Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;

}
like image 486
Bennett_bear Avatar asked Jul 29 '15 19:07

Bennett_bear


People also ask

How does Nginx determine content type?

It is determined by the name of the file actually returned. The try_files statement in your last configuration will return the requested file if it exists, otherwise index. html is returned, and that file has a MIME type of text/html .

What does proxy_pass do in Nginx?

The proxy_pass setting makes the Nginx reverse proxy setup work. The proxy_pass is configured in the location section of any virtual host configuration file. To set up an Nginx proxy_pass globally, edit the default file in Nginx's sites-available folder.

What is Proxy_redirect?

The proxy_redirect used above will rewrite all HTTP redirects from upstream to the current scheme, ie HTTPS. The browser will then receive the correct scheme directly, avoiding unnecessary round-trips.


1 Answers

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: 
like image 176
Horacio Avatar answered Sep 16 '22 21:09

Horacio