使用nginx时,让web取得原始请求地址

问题描述

当使用nginx配置proxy_pass参数以后,后端web取得的Request.Uri是proxy_pass中配置的地址,而不是client访问的原始地址

举例说明:

假设nginx配置文件为,nginx所在server ip为192.168.1.100:

server {
	listen 80;
	
	location /{
		proxy_pass http://localhost:5000;
	}
}

当user访问 http://192.168.1.100 时,web获取到的Request.Uri地址为 http://localhost:5000

解决方法

如果想要获取到 http://192.168.1.100 ,则需要再加上一条配置:

proxy_set_header Host $host;

server {
	listen 80;
	
	location /{
		proxy_pass http://localhost:5000;
		proxy_set_header Host $host;
	}
}
原文地址:https://www.cnblogs.com/windchen/p/6415655.html