使用Nginx做端口转发

515 阅读1分钟

需求:将目标机器的8080端口转到本机的8080端口。

1)在/etc/nginx/conf.d下添加port-forward.conf文件。

server {
  listen 9988;
  location / {
    proxy_pass https://[dest_host]:8080; #目标机器使用了https
    proxy_set_header Host $host;
    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_ssl_verify off; #关闭ssl验证
  }
}

2)确保nginx.conf中包含conf.d/port-forward文件,若不包含需要添加。

http {
  include /etc/nginx/conf.d/port-forward.conf;
}

3)测试nginx配置文件的有效性。

nginx -t

4)重载nginx配置文件。

nginx -s reload

5)验证:访问http://[src-host]:8080 。