web-dev-qa-db-ja.com

proxy_passでコンテンツタイプを設定する方法は?

Webサイトでは、場所/インスタンスのトラフィックがhttp経由で流入しているため、SSLとhttpsを使用してトラフィックを保護する必要があります。 httpsにリダイレクトする場合、リクエストのコンテンツタイプは「text/xml」ですが、実際には「application/json」である必要があります。プロキシヘッダーの何かを「application/json」に明示的に設定する必要がありますか? http構成でadd_headerContent-type "application/json"を試しましたが、違いはありませんでした。私たちは何が間違っているのですか?

Http構成:

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"
}

まだ204エラーが発生しているため、ヘッダーにcontent-typeを設定しても機能しませんでした。

https構成:

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;

}
8
Bennett_bear

ここでの問題は、add_headerを実行していて、add_headerがそのヘッダーを応答に追加しているように見え(リクエストがバックエンドからクライアントに戻ってきたとき)、バックエンドに設定したいことだと思います。

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. 

あなたはあなたのconfでこの行をすべきです

proxy_set_header content-type "application/json";

すべてのproxy_ *がリクエストに設定されます(クライアントからバックエンドへ)

構文:proxy_set_headerフィールド値;デフォルト:

proxy_set_headerホスト$ 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: 
5
Horacio