nginx-- proxy_pass转发到另一域名403问题

以下配置看似没有错误,理论上可以正常转发!但是看了半天找不到问题 就是访问403。

server {

	listen 80;        
        server_name test.dalu.com;

	location / {
        proxy_pass http://api.dalu.com;
	}
}

最后究其原因为:在nginx.conf中

http {
...


    proxy_set_header Host $host;
...

proxy_set_header Host $host; 代表转发时不改变请求头“Host”的值。所以转发过去,对方api.dalu.com不识别,所以返回403. 修改配置文件如下:

server {

	listen 80;        
        server_name test.dalu.com;

	location / {
        proxy_pass http://api.dalu.com;
        proxy_set_header Host $proxy_host; #加入了此配置:表示转发后只带上,/后的内容转发到api.dalu.com。
}

其他配置说明 如果客户端请求头中没有携带这个头部,那么传递到后端服务器的请求也不含这个头部。 这种情况下,更好的方式是使用$host变量——它的值在请求包含“Host”请求头时为“Host”字段的值,在请求未携带“Host”请求头时为虚拟主机的主域名: proxy_set_header Host $host; 此外,服务器名可以和后端服务器的端口一起传送: proxy_set_header Host h o s t : host:host:proxy_port; 如果某个请求头的值为空,那么这个请求头将不会传送给后端服务器: proxy_set_header Accept-Encoding “”;

猜你喜欢

转载自blog.csdn.net/weixin_45839894/article/details/126503099