背景:有一个别人提供的服务是通过网关配置的(以下简称:目标服务),也就是路径是http://host/a/b/c/d这种的,现在我自己有两台服务器A、B, A可以访问目标服务,B不能,但是B又能访问A服务器,所以我想通过在A服务器做一个nginx反向代理,让B服务器通过A服务器来访问目标服务。
初始nginx server配置(复制自别处= =!)
server {
listen 443;
listen [::]:443;
location /kg/ {
proxy_pass http://example.com/a/b/c/;
proxy_buffering off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
}
问题:当我去访问https://服务器ip/kg/的时候,一直提示not found。
原因:多次尝试没找到原因,找到了解决方法
解决:proxy_pass下面的配置全部删除就好了,这个是我对nginx配置不了解,清楚后面几个配置应该就能解释(后面再行解释),正确配置
server {
listen 443;
listen [::]:443;
location /kg/ {
proxy_pass http://example.com/a/b/c/;
}
}
问题: 访问目标服务提示header问题,
原因:为nginx转发将带有下划线“_”的header过滤掉了
解决:需要去nginx.conf里面配置underscores_in_headers on;
参考:nginx反向代理导致请求header头信息丢失 - 天宇星空 - 博客园
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
underscores_in_headers on;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
问题:所有的post请求都变成了get请求
原因:为协议不匹配引发的重定向,A服务nginx用了https,当时目标服务是http,而我的请求url依 旧使用了http
解决:应该用A服务器的https
参考:nginx 代理post请求变成get请求问题_supming1的专栏-优快云博客_nginx post 转get