最近在部署项目到web服务器上时,该项目有一个打开视频监控的功能,视频的服务器是一台内网的服务器,不容许设置外网端口访问,网站服务器和视频服务器在同一个局域网内,能够相互联通。网络拓扑图以下:
为了能在外网打开网站,而且播放视频,所以须要将视频服务的端口映射到外网去,因此咱们使用了反向代理技术。
反向代理(reverse proxy)是指以代理服务器接受请求,而后将请求转发给内部网络上的服务器,并将从服务器上获得的结果返回给请求者。
查询相关资料后,咱们能够在web服务器上搭建一个反向代理,来接受用户的访问视频接口的请求,而后转发给视频服务器处理,处理完后经过反向代理返回给用户。
在反向代理软件的选择上,我尝试了frp和nginx,都可知足项目需求。html
frp是github上的一个开源项目,项目地址为:https://github.com/fatedier/frp。
该项目介绍为,frp 是一个可用于内网穿透的高性能的反向代理应用,感兴趣的小伙伴能够去了解一下,它的功能很强大。
下载完软件后,咱们能够看到软件分为客户端和服务端,以c结尾的为客户端,以s结尾的为服务器。
nginx
(1)服务端配置
服务端配置比较简单,配置http本地监听的端口vhost_http_port = 8033,token为客户端和服务端通讯密钥;dashboard开头为一个web可视化页面,能够看到相关的链接、流量等状况git
# frps.ini [common] bind_port = 7000 vhost_http_port = 8033 token = 123456 dashboard_addr = 0.0.0.0 dashboard_port = 7500 dashboard_user = admin dashboard_pwd = 654321
而后运行命令 ./frps -c ./frps.ini
,启动服务端,注意:服务端要在开通了外网端口的服务器上运行,vhost_http_port端口即为外网访问的端口。
(2)客户端配置
server_addr、server_port为上一步骤中部署frp服务端的地址和端口,token要和服务端token保持一致;
local_port 为本地机器上 web 服务对应的端口,custome_domains为你的域名或者外网ipgithub
# frpc.ini [common] server_addr = 192.168.0.227 server_port = 7000 token = 123456 [web] type = http local_port = 8053 custome_domains = 192.168.0.227
启动客户端运行,在cmd命令行里:./frpc -c ./frpc.ini
(3) 访问
经过浏览器访问 custome_domains :vhost_http_port
便可访问处处于内网机器上的 web 服务web
nginx做为一个功能强大的web服务器一样能够完成这个功能。windows
打开conf文件夹下的nginx.conf文件,咱们能够进行配置浏览器
#user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #access_log logs/access.log main; sendfile on; keepalive_timeout 65; #gzip on; #设定实际的服务器列表 upstream zp_server1{ server 127.0.0.1:8099; } server { listen 8018; server_name localhost; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 location / { proxy_pass http://zp_server1; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one location ~ /\.ht { deny all; } } }
设定要代理的内部服务器地址:服务器
#设定实际的服务器列表 upstream zp_server1{ server 127.0.0.1:8099; }
设定web服务器外网端口网络
server { listen 8018; server_name localhost; location / { proxy_pass http://zp_server1; } }
经过访问外网ip:8018就能够访问部署在内网的web服务了。app
(1)启动脚本
@echo off rem 若是启动前已经启动nginx并记录下pid文件,会kill指定进程 nginx.exe -s stop rem 测试配置文件语法正确性 nginx.exe -t -c conf/nginx.conf rem 显示版本信息 nginx.exe -v rem 按照指定配置去启动nginx nginx.exe -c conf/nginx.conf
(2)关闭脚本
windows下会有两个进程,直接使用资源管理器杀进程比较困难,可以使用taskkill /im nginx.exe /f
不管是frp,仍是nginx均可以知足,将内部网站映射到外网访问的需求,frp功能更强大,nginx功能更专注。 参考文章: (1)https://blog.csdn.net/duchunwang/article/details/82861216 (2)https://www.cnblogs.com/wcwnina/p/8728391.html (3)https://www.cnblogs.com/jingmoxukong/p/5945200.html (4)https://github.com/fatedier/frp