server {
listen 80;
server_name mydomain.com;
location / {
proxy_pass http://myproxy.com:80;
proxy_set_header Host mydomain.com; #设置为跟server name 一样
}
}
如上,当proxy_set_header 设置为跟server name 一样的时候,出现502错误。
error stack如下:
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
百思不得其解,这个会是什么原因呢?
当我更新proxy的端口,如从80改为81 proxy_pass http://myproxy.com:81;
此时,是可以正常工作的。(我之前的myproxy.com就是listen 80。现在改为81。)
But why ?
習慣沉默2017-05-16 17:10:21
CentOS7安装维护Nginx,常见使用场景
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;
}
}
或者下面这种形式
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;
}
}