谷粒商城P139集——云服务器frp内网穿透+nginx完美解决方案

谷粒商城P139-175集——云服务器frp内网穿透+nginx完美解决方案 不需要购买域名即可 只要不提示被拦截

1、修改本地HOST

C:\Windows\System32\drivers\etc 目录下 host文件

在这里插入图片描述

上面前面是自己的云服务器ip

测试:如域名为gulimall.com 备注如果自己的云服务器nginx端口不是80 访问的时候记得打开

可以访问9200或者nacos尝试

则在浏览器中输入gulimall.com:9200(前提是9200端口已经开放)

img

2、frp服务端设置(云服务器)

(1)下载
wget https://github.com/fatedier/frp/releases/download/v0.20.0/frp_0.20.0_linux_amd64.tar.gz

云服务器下载linux版本注意frps的配置即可,本地下载windows版本注意frpc的配置即可

(2)解压

tar -zxvf frp_0.20.0_linux_amd64.tar.gz

(3)修改frps.ini(frp server 服务端)

bind_port = 7001

dashboard_port = 7500

token = 12345678

dashboard_user = admin

dashboard_pwd = admin

vhost_http_port = 88
  1. “bind_port”表示用于客户端和服务端连接的端口,这个端口号我们之后在配置客户端的时候要用到。
  2. “dashboard_port”是服务端仪表板的端口,若使用7500端口,在配置完成服务启动后可以通过浏览器访问 云服务器ip:7500 查看frp服务运行信息。
  3. “token”是用于客户端和服务端连接的口令,请自行设置并记录,稍后会用到。
  4. “dashboard_user”和“dashboard_pwd”表示打开仪表板页面登录的用户名和密码,自行设置即可。
  5. “vhost_http_port":注意我们通过云服务器ip : vhost_http_port去访问本地主机被穿透的端口

(4)测试运行frpc服务端

命令:

./frps -c frps.ini

成功:

img

此时访问 云服务器ip : 7500 并使用自己设置的用户名密码登录,即可看到仪表板界面

img

(5)设置frpc后台运行

nohup ./frps -c frps.ini &

成功:

img

此时可先使用Ctrl+C关闭nohup,frps依然会在后台运行,使用jobs命令查看后台运行的程序

img

3、frp客户端设置(本地主机)

(1)修改frpc.ini

[common]
server_addr = 43.139.29.111 
server_port = 7001
token = 520517li
[http]
type = http
local_ip = 127.0.0.1 
#【本机想要暴露的地址】
#对于谷粒商城来说,我们要把这个设置为网关的端口号
local_port = 88 
 #这里切记要改成自己本机映射的域名而非其他  如 ip
custom_domains = gulimall.com

[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000

(2)运行测试 记得每次想做内网穿透服务就需要打开这个服务由于我公司内网不允许穿透我这里不推荐开机自启动

frpc.exe -c frpc.ini

img

4、nginx设置

方案1不购买域名通过修改本地hots文件去映射完成

前提windows已经设置hosts映射修改云服务器的映射

来到 /etc/hosts

修改它

在这里插入图片描述

示例

云服务ip 域名
在这里插入图片描述

修改后查看域名 发现并不是我们设置的云服务器ip 也没关系

在这里插入图片描述

来到nginx的conf.d 单独创建

gulimall.conf

在这里插入图片描述

配置如下

在这里插入图片描述

server {
listen 80;
listen [::]:80;
server_name gulimall.com;
location /static/ {
root /usr/share/nginx/html;
}
location / {
proxy_pass http://公网ip:frp穿透的端口;
proxy_set_header Host $host;#这里的意思是保留原来的请求头
}

error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

img

方案2购买域名

购买域名后绑定到自己主机

在这里插入图片描述

和上述一样的只有第 3步骤 custom_domains = gulimall.com 这里gulimall.com替换成自己的域名 和第四步骤 不一样

第四步骤

来到nginx的conf.d 单独创建

gulimall.conf

在这里插入图片描述

配置如下

在这里插入图片描述

server {
listen 80;
listen [::]:80;
server_name lkecmy.cn;
location /static/ {
root /usr/share/nginx/html;
}

#这种方法可以使用ip也可以使用域名

​ location / {
​ #proxy_pass http://公网ip:frp穿透的端口;

​ proxy_pass http://域名:88;

​ proxy_set_header Host $host;#这里的意思是保留原来的请求头
​ }

error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

在这里插入图片描述

重要:引用参考文章https://blog.csdn.net/weixin_57139527/article/details/126789451

5、总结

  1. 输入域名,域名解析为云服务器ip地址
  2. nginx监听到server_name为域名的请求,将其转发给http://云服务器ip:88;
  3. frps(云服务器上的服务端)监听到88端口(对应于frps.ini里的vhost_http_port),frps通过绑定的端口号(bind_port)与本地主机连接。
  4. 作为frpc的本地主机通过(server_port)和(server_addr)与有frps的云服务器连接,它将会把来自于云服务器的请求转发给(local_ip : local_port)并且为之设置host字段(host_header_rewrite = gulimall.com
  5. 最终后端项目收到了如http://localhost:88的请求,网关对host字段进行断言,成功后负载均衡给gulimall-product服务,最终响应商城主页。

6、其他参考文章

1、FRP内网穿透–实现公网ip远程访问内网8080端口_尬维的博客-CSDN博客_frp 端口

2、使用frp进行内网穿透 - 少数派

4、windows守护进程工具–nssm详解 - 与f - 博客园

6、基于以上内容你可以轻松完成p139的内容 如果你比较聪明 应该174夜不成问题

第六部分我想详细写一下我的 174p微服务多服务代理的解决方案 给大家一盏路灯

基于上面的配置 我再次去配置centos云服务器的本地/etc/hosts 下的配置文件但是我配置完之后发现并没有生效 (搞了好几天这个问题了为了快速解决我直接使用了 我的购买的域名 +我原来设置的域名gulimall)

以下是配置文件

gateway

- id: cloudserverproduct
uri: lb://gulimall-product
predicates:
- Host=.lkecmy.cn
- id: cloudserversearch
uri: lb://gulimall-search
predicates:
- Host=
.gulimall.com

window frpc.ini

[common]
server_addr =
server_port = 7001
token = token
[http]
type = http
local_ip = 127.0.0.1
#【本机想要暴露的地址】
#对于谷粒商城来说,我们要把这个设置为网关的端口号
local_port = 88

custom_domains =gulimall.com,lkecmy.cn #注意我这里使用的我购买的dns和配置的一个dns域名 gulimall
#【注意】雷神这一步是在nginx中完成的

设置请求的host字段,以便网关断言

#host_header_rewrite = gulimall.com

[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000

windows hosts

公网ip服务器的 gulimall.com

centos dns配置

centosnginx

server {
listen 80;
listen [::]:80;
server_name lkecmy.cn gulimall.com;
location /static/ {
root /usr/share/nginx/html;
}
location / {
proxy_pass http://服务器公网ip:88;
#proxy_set_header Host lkecmy.cn;
proxy_set_header Host $host;
}

error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

}

总结

人就像一艘逆水行舟,不进则退 或许大家对未来迷茫 仿徨,但请你不要失去对编程的探索,或许不是热爱。但解决好一个问题便是快乐

  • 24
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值