前端域名:**.cn 后端域名:api.**.cn 前端请求后端会出现跨域问题,所以我想通过nginx配置代理,使前端请求**.cn/api时响应的是api.**.cn 以下是前端站点配置反代的截图:
**.cn
api.**.cn
**.cn/api
后端是通过bt设置反代,把webman反代到api.**.cn的
api.**.cn的
nginx错误日志: 2024/04/20 17:44:44 [error] 11326#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 10x.xxx.xxx.x23, server: shabo.yuntower.cn, request: "GET /api//captcha/image?hard=false HTTP/2.0", upstream: "https://ip:443/api//captcha/image?hard=false", host: "xxx.cn", referrer: "https://xxx.cn/user/login"
多了个/
proxy_set_header Host $host; 改成 proxy_set_header Host api.xxx.cn;
佬,还是502 location /api { proxy_ssl_server_name on; proxy_set_header Host api.xxx.cn; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://api.xxx.cn; }
location ^~/apis/ {
rewrite ^/apis/(.*)$ /$1 break; #重写只要后部分 proxy_pass api.abc.cn; #代理到api.域名 }
nginx错误日志:
2024/04/20 17:44:44 [error] 11326#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 10x.xxx.xxx.x23, server: shabo.yuntower.cn, request: "GET /api//captcha/image?hard=false HTTP/2.0", upstream: "https://ip:443/api//captcha/image?hard=false", host: "xxx.cn", referrer: "https://xxx.cn/user/login"
多了个/
proxy_set_header Host $host;
改成
proxy_set_header Host api.xxx.cn;
佬,还是502
location /api {
proxy_ssl_server_name on;
proxy_set_header Host api.xxx.cn;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://api.xxx.cn;
}
location ^~/apis/ {
这里重写了请求,将正则匹配中的第一个分组的path拼接到真正的请求后面,并用break停止后续匹配
rewrite ^/apis/(.*)$ /$1 break; #重写只要后部分
proxy_pass api.abc.cn; #代理到api.域名
}