一、需求背景
存在1台阿里云公网服务器;
存在1台公司内网服务器。
现在需要实现在内网服务器通过nginx部署系统,同时通过域名来进行访问所部署的系统。
二、Frps服务端配置(阿里云服务器)
vim frps.ini
[common]
bind_port = 7000
vhost_http_port = 7000
subdomain_host = xxx.com
三、Nginx配置(阿里云服务器)
vim demo.conf
server {
listen 80;
server_name eam-demo.xxx.com;
location / {
proxy_pass http://localhost:7000;
proxy_set_header Host $host;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
这个配置指定了一个监听在 80 端口的虚拟主机,将请求代理到本地的 http://localhost:7000
。
listen 80;
: 监听在 80 端口,接收来自客户端的 HTTP 请求。
server_name eam-demo.xxx.com;
: 定义了这个虚拟主机的域名为eam-demo.xxx.com
。只有这个域名的请求会被这个服务器块处理。location / { ... }
: 当请求路径为/
时,这个块内的指令将被执行。在这里,它将请求通过代理传递给http://localhost:7000
。
proxy_pass http://localhost:7000;
: 将请求转发到本地的http://localhost:7000
。proxy_set_header Host $host;
: 设置代理请求的 Host 头,以确保目标服务器能够正确处理请求。
error_page 500 502 503 504 /50x.html;
: 配置了错误页面。当发生 500、502、503 或 504 错误时,将会返回 /50x.html
页面。
location = /50x.html { ... }
: 当请求路径为 /50x.html
时,将会返回指定位置的静态文件。
root html;
: 这里指定了根目录为html
,这意味着 Nginx 会在html
目录下寻找/50x.html
文件。
这个配置适用于将来自 eam-demo.xxx.com
的 HTTP 请求代理到本地运行的某个服务(在这里是 http://localhost:7000
)。
三、Frpc客户端配置(公司内网服务器)
vim frpc.ini
[web_eam-demo]
type = http
local_ip = 127.0.0.1
local_port = 9001
subdomain = eam-demo
[test_eam-demo]
type = http
local_ip = 127.0.0.1
local_port = 9001
subdomain = test
这是 frp 客户端的配置文件,用于设置 frp 客户端以实现反向代理功能。frp
是一个可用于内网穿透的工具,允许将本地服务通过公共网络暴露出去,从而可以通过公共网络访问这些服务。
[test_eam-demo]:
同样,
type = http
表示这是一个 HTTP 类型的代理。local_ip = 127.0.0.1
表示本地服务运行在本机的 IP 地址。local_port = 9001
表示本地服务运行在 9001 端口上。subdomain = test
表示这个 frp 代理将通过域名test.xxx.com
提供访问。
四、总结
该配置实现了通过test.xxx.com访问服务器本地的9001端口服务,而无需加上任何端口。