nginx安装:
需要一台公网服务器
在公网服务器的nginx配置文件nginx.conf添加一下内容:
upstream tunnel {
server 127.0.0.1:7689;}server {
listen 80;
server_name x.xx.xxx;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://tunnel;
}}
理解:用nginx创建代理,如果有人访问http://x.xx.xxx,nginx会把请求转发给tunnel,这个tunnel指的就是这台公网服务器,端口号是7689,后面会用这个跟内网来进行通信
打开自己的内网环境:
在内网服务器跟公网服务器之间,使用ssh打开一个通道。在内网环境服务器中要执行的的命令:
ssh -vnNT -R 服务器端口:localhost:本地端口 服务器用户名@服务器 IP 地址
例:
ssh -vnNT -R 7689:localhost:8000 root@47.45.14.12
7689指的是公网服务器的端口,localhost:8000是内网服务器的ip端口
root是公网服务器的用户,47.45.14.12是公网服务器的ip
所以:当我们访问http://x.xx.xxx的时候,得到的响应是你内网服务器上的服务(会把请求转到服务器上的7968端口,这个端口跟我们内网环境上的8000端口是连接到一块的)
0