在网站上,流量通过 http 流入位置/实例,我们需要使用 SSL 和 https 来保证流量的安全。重定向到 https 时,请求内容类型是“text/xml”,而实际上应该是“application/json”。我们是否应该将代理 header 中的某些内容显式设置为“application/json”?我们在 http 配置中尝试了 add_header 内容类型“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"
}
在 header 中设置内容类型不起作用,因为我们仍然收到 204 错误。
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;
}
最佳答案
我认为这里的问题是您执行了 add_header 并且 add_header 似乎将该 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:
关于json - 如何在 proxy_pass 中设置内容类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31709998/