server {
listen 80;
server_name mydomain.com;
location / {
proxy_pass http://myproxy.com:80;
proxy_set_header Host mydomain.com; #设置为跟server name 一样
}
}
As above, when proxy_set_header is set to the same as the server name, a 502 error occurs.
The error stack is as follows:
upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: mydomain.com, request: "GET /xx HTTP/1.0", upstream: "http://127.0.0.1:80/ xx", host: "mydomain.com
I am puzzled, what could be the reason for this?
When I update the proxy port, such as changing it from 80 to 81 proxy_pass http://myproxy.com:81;
At this point, it can work normally. (My previous myproxy.com was listen 80. Now it is changed to 81.)
But why ?
習慣沉默2017-05-16 17:10:21
CentOS7 installation and maintenance of Nginx, common usage scenarios
server {
listen 80;
server_name mydomain.com;
location / {
proxy_pass http://127.0.0.1:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
}
or this form
upstream server-a{
# api 代理服务地址
server 127.0.0.1:80;
}
server {
listen 80;
server_name mydomain.com; # 这里指定域名
# 匹配 api 路由的反向代理到API服务
location ^~/ {
rewrite ^/(.*)$ / break;
proxy_pass http://server-a;
}
}