几个日夜, 无数坑.
背景是:
目前有备案的域名只有一个, 而这个已经在生产环境采用. 所以如果要采用这个域名, 将不得不用三级泛域名证书, 形如 *.dev.aaa.com.
由于首要目的是开发用 (微信小程序), 后台语言是 ASP.NET core, 出于开发方便考虑, web 服务器用 Kestrel.(当然, 最终解决方案于此无关)
阿里云服务器, CentOS 7.4, 域名也在阿里云.
一开始用 ngrok 但始终不能成功, 后改用 Frp, 确实比 ngrok 好很多, 但依然无法配置成功 https 访问.
最终, 采用 Nginx + Frp 的方案, 网站无需实现 https,SSL 在 Nginx 处用反向代理实现.
GO!
一, 域名解析:
二, 安装 Frp:
参考:
Frps 一键安装脚本, 带 Frpc Windows 便捷启动脚本
三, 配置 Frp:
(1) 服务器端:
用 f.sh 命令修改, 或直接改文件:
# VIM /usr/local/frps/frps.INI
主要要改:
- vhost_http_port = 8090 #避开 nginx 要用的 80
- vhost_https_port = 8443 #其实没什么用, 因为靠 Nginx 来处理 https
- dashboard_user = admin
- dashboard_pwd = admin
- token = IamPassword
- subdomain_host = dev.aaa.com
如果是直接改的 INI 文件, 保存后别忘了用 systemctl restart frps 重启服务.
(2) 安全组或防火墙配置
Frps.INI 文件中配置的端口确保在阿里云的安全组中打开. 当然, 还包括默认的 7000,7500 等端口. 如果不是云主机, 那就是防火墙同样操作.
(3) 客户端
Frpc.INI 文件:
[common]
server_addr = 服务器 IP
- token = IamPassword
- server_port = 7000
- # protocol = kcp 这个不知道为什么, 一开就连不上
- log_file = ./frpc.log
- log_level = info
- log_max_days = 3
- [testdevhttp] #注意, 这个名称不能重
- type = http
- local_port = 5000
- subdomain = test
- # http_user = admin
- # http_pwd = admin
- ......
同样, 5000 端口要打开妨火墙.
这个时候内网网站就应该监听 5000 端口, 确保 http://localhost:5000 能打开网站.
执行 start.bat 打开客户端.
(4) 验证:
访问 http:// 服务器 IP:7500, 能进入管理界面 (需要输入 dashboard_user 和 pwd);
访问 http://test.dev.aaa.com 能打开网站
四, 安装 nginx
- sudo yum install nginx
- VIM /etc/nginx/nginx.conf
把 server_name _; 改成: server_name *.dev.aaa.com;
保存退出.
用 sudo nginx -t 验证修改正确, 然后用
sudo systemctl reload nginx 重载 nginx.
确保安全组或防火墙配置的 80 和 443 端口是打开的.
此时打开 http://*.dev.aaa.com, 应能看到 nginx 的默认站点. * 可以是任意字符组合.
五, 安装 Let's Encrypt
参考:
Let's Encrypt 安装配置教程, 免费的 SSL 证书 https://segmentfault.com/a/1190000017194280
- ./certbot-auto --nginx -d *.dev.aaa.com --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory
- (是不是 certonly 要去掉忘了 -- 应该是要去掉, 关键是要加上 --nginx, 去掉 --manual)
- server {
- server_name *.dev.aaa.com;
- root /usr/share/nginx/html;
- # Load configuration files for the default server block.
- include /etc/nginx/default.d/*.conf;
- location / {
- # 手动添加下面内容:
- proxy_pass http://127.0.0.1:8090;
- 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 REMOTE-HOST $remote_addr;
- add_header X-Cache $upstream_cache_status;expires 12h;
- }
- error_page 404 /404.HTML;
- location = /40x.HTML {
- }
- error_page 500 502 503 504 /50x.HTML;
- location = /50x.HTML {
- }
- listen [::]:443 ssl ipv6only=on; # managed by Certbot
- listen 443 ssl; # managed by Certbot
- ssl_certificate /etc/letsencrypt/live/dev.aaa.com/fullchain.pem; # managed by Certbot
- ssl_certificate_key /etc/letsencrypt/live/dev.aaa.com/privkey.pem; # managed by Certbot
- include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
- ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
- }
- sudo nginx -t
- sudo systemctl reload nginx
来源: http://www.bubuko.com/infodetail-2948123.html