Administrator
发布于 2024-02-01 / 8 阅读
0

http反向代理https

使用http反向代理https

Nginx

server {
	listen 30003;
	server_name localhost;

	location / {
		proxy_pass https://httpbin.org/;
		proxy_set_header X-Forwarded-Proto https; # 重要
	}
}

Go(gin)

// 方式1 重写Rewrite
takeOverProxy = &httputil.ReverseProxy{
	Rewrite: func(r *httputil.ProxyRequest) {
        r.SetURL(targetURL)
        r.Out.Header.Set("X-Forwarded-Proto", "https")
        r.Out.Host = targetURL.Host
	},
}
takeOverProxy.ServeHTTP(c.Writer, c.Request)

// 方式2 直接修改
c.Request.Header.Set("X-Forwarded-Proto", "https")
c.Request.Host = targetURL.Host
takeOverProxy.ServeHTTP(c.Writer, c.Request)