Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverse proxy 中怎样预设原始Host 头。 #572

Open
lnxyz opened this issue Sep 10, 2024 · 5 comments
Open

reverse proxy 中怎样预设原始Host 头。 #572

lnxyz opened this issue Sep 10, 2024 · 5 comments

Comments

@lnxyz
Copy link

lnxyz commented Sep 10, 2024

  • gost 3.0.0-rc.3
    eg. nginx reverse proxy 配置
...
    location / {
        proxy_pass http://www.example.com;
        proxy_set_header Host "www.example.com";
  }
...

我在以下地方配置 没有成功。

- name: http
  addr: :80
  handler:
    type: tcp
    metadata:
      host: "www.example.com    # 1<----- 我在这配置没有起作用
      sniffing: true
  listener:
    type: tcp
  forwarder:
    nodes:
    - name: example-com
      addr: example.com:80
      filter:
        host: example.com
      http:
         host: "www.example.com" # 2<------ 我在这单独或跟1中一并配置 也没有起作用,甚至我按 [文档自定义头部配置](https://gost.run/tutorials/reverse-proxy/#_4)也没有起作用。

请教一下我应该在哪配置

@ginuerzh
Copy link
Member

版本太旧。

@lnxyz
Copy link
Author

lnxyz commented Sep 11, 2024

版本换到 gost v3.0.0-nightly.20240904, 但没有成功(503 Service Unavailable),若将addr更改回IP就可以。另外我通过 nginx 本地内网IP站 反向代理 https站成功,更换采用gost 则不成功。能否给一下这方面的示例: http -- http , http --- https , 预设原始Host头的 example 示例?

@lnxyz
Copy link
Author

lnxyz commented Sep 12, 2024

版本太旧。

已经换用新的版本了,请问能否提供一个示例。

@ginuerzh
Copy link
Member

试试下面配置:

services:
- name: http
  addr: :80
  handler:
    type: tcp
    metadata:
      sniffing: true
  listener:
    type: tcp
  forwarder:
    nodes:
    - name: example-com
      addr: example.com:80
      http:
        host: www.example.com

@lnxyz
Copy link
Author

lnxyz commented Sep 18, 2024

试试下面配置:

services:
- name: http
  addr: :80
  handler:
    type: tcp
    metadata:
      sniffing: true
  listener:
    type: tcp
  forwarder:
    nodes:
    - name: example-com
      addr: example.com:80
      http:
        host: www.example.com

无法成功,

services:
- name: http
  addr: :8080
  handler:
    type: tcp
    metadata:
      sniffing: true
  listener:
    type: tcp
  forwarder:
    nodes:
    - name: web-com
      addr: www.baidu.com:443
      http:
         host: www.baidu.com

返回 HTTP ERROR 503

但我直接golang 用 "net/http/httputil" 包实现则可以

func main() {
	// 解析远程服务器的 URL
	target := "https://www.baidu.com"
	remote, err := url.Parse(target)
	if err != nil {
		log.Fatalf("解析目标地址失败: %v", err)
	}

	// 创建反向代理
	proxy := httputil.NewSingleHostReverseProxy(remote)

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		r.Host = remote.Host
		// 设置代理头信息
		r.Header.Set("X-Forwarded-Host", r.Header.Get("Host"))
		r.Header.Set("X-Forwarded-For", r.RemoteAddr)

		log.Printf("代理请求: %s %s", r.Method, r.URL.String())

		proxy.ServeHTTP(w, r)
	})


	addr := ":8080"
	log.Printf("启动代理服务器, 监听 %s", addr)
	if err := http.ListenAndServe(addr, nil); err != nil {
		log.Fatalf("代理服务器启动失败: %v", err)
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants