Pandorabox固件路由器上申请Let's Encrypt证书,为内网里的多个web服务提供SSL支持

2023-03-18,,

对于家中宽带有公网IP的用户,有时我们需要将路由器内部网络的某些web服务通过端口转发暴露到外网(例如NAS远程访问),但HTTP是明文传输,有被监听的风险;如果在NAS上使用自签名证书,再端口转发,会被Chrome浏览器认为是风险连接拒绝访问(笔者使用80.0.3987版本,命令行参数启动、系统添加证书信任法均不能通过);使用某些NAS自带的Let's Encrypt插件申请到的证书,也只能为NAS单独一个服务添加HTTPS。本文将介绍如何在路由器上安装NGINX并部署SSL,反向代理内网中的多个HTTP服务。

要求

一个域名,无须备案;路由器有公网IP,路由器为Pandorabox固件(理论上基于openwrt的固件都可以);软件源已更至最新。

截至2020.03,可用的Pandorabox软件源地址为http://downloads.pangubox.com:6380/pandorabox

比如我的路由器为newifiD1,配置信息则为

src/gz 18.10_base http://downloads.pangubox.com:6380/pandorabox/18.10/packages/mipsel_1004kc_dsp/base
src/gz 18.10_lafite http://downloads.pangubox.com:6380/pandorabox/18.10/packages/mipsel_1004kc_dsp/lafite
src/gz 18.10_luci http://downloads.pangubox.com:6380/pandorabox/18.10/packages/mipsel_1004kc_dsp/luci
src/gz 18.10_mtkdrv http://downloads.pangubox.com:6380/pandorabox/18.10/packages/mipsel_1004kc_dsp/mtkdrv
src/gz 18.10_newifi http://downloads.pangubox.com:6380/pandorabox/18.10/packages/mipsel_1004kc_dsp/newifi
src/gz 18.10_packages http://downloads.pangubox.com:6380/pandorabox/18.10/packages/mipsel_1004kc_dsp/packages

安装Nginx和acme

nginx在pandorabox的web管理界面或命令行opkg install nginx 安装均可,acme按照官方说明安装、运行,但之前还需再安装几个包:curl wget ca-certificates openssl-util ca-bundle socat

ca-bundle是一堆打包的CA根证书,pandorabox默认不包含任何CA根证书,所以无法建立任何SSL连接。socat在acme的部分功能里会用到,最好安装。

安装过程还有几个地方需要注意:

1. 安装nginx后会报启动失败,端口被占用,这是正常的,因为80端口已经被路由器Web管理界面(uhttpd服务)占用了,之后我们会改nginx配置文件。

2. 使用http验证,注意uhttp的默认网站根目录是/www,并需在防火墙中打开80端口,如果运营商屏蔽了80端口,那http根目录验证无法使用,但可以用standalone模式指定其他端口验证。比如使用88端口,先在防火墙中打开88端口,然后验证命令为:

acme.sh --issue -d example.com --standalone --httpport 

如果使用nginx自动验证,要确保nginx已经正确运行,这需要配置uhttpd端口与nginx不冲突,以及防火墙,不建议使用。

3. 使用dns验证,无需在路由器上做任何配置,建议使用acme的自动txt记录添加功能

4. 拷贝证书可以用acme的拷贝命令,但需做修改如下

acme.sh --installcert -d example.com \
--key-file /etc/nginx/key.pem \
--fullchain-file /etc/nginx/cert.pem
# --reloadcmd "service nginx force-reload" 不要添加这句

配置Nginx

至此证书已经获取完毕,接下来在nginx中配置。假设我们内网的两个web服务地址分别为http://192.168.0.100, http://192.168.0.102/test

user nobody nogroup;
worker_processes ; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections ;
} http {
include mime.types;
#default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout ;
client_body_timeout ;
client_header_timeout ;
keepalive_timeout ;
send_timeout ; gzip on; server {
listen ssl; #不方便使用443端口可更换其他端口
server_name example.com; charset utf-;
ssl_certificate cert.pem; #注意这里两个文件不要写反了
ssl_certificate_key key.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_prefer_server_ciphers on;
#access_log logs/host.access.log main; #error_page /.html;
proxy_connect_timeout ;
proxy_send_timeout ;
proxy_read_timeout ;
proxy_set_header Host $host;
proxy_set_header X-Forwarder-For $remote_addr; location /servertest {
proxy_pass http://192.168.0.102/test;
proxy_redirect default; #nginx服务器不做缓存,所有请求都转发到目标服务器进行处理
proxy_buffering off;
}
location /server1 {
proxy_pass http://192.168.0.100;
# 不做配置,nginx会对内容进行缓存,速度有些许提升,但可能遭遇超时问题
} location ~ //.ht {
deny all;
} #error_page /50x.html;
#location = /50x.html {
# root html;
#} }
}

Nginx配置文件

重启nginx服务:/etc/init.d/nginx restart

如果没有错误信息输出,防火墙的相应端口已经打开,则访问https://example.com/server1和https://example.com/servertest(如果指定非443端口需加上端口号),发现服务已经运行在ssl之上了。更多服务只需设置更多子目录转发即可。

注意

1. 理论上到nginx配置之前都可以在内网主机上进行,最终只要手动拷贝生成的两个证书文件到路由器/etc/nginx目录即可,但在证书验证环节就需要端口转发,或者用dns验证。

2. 即使路由器不支持安装nginx,也可以在内网主机上安装nginx,然后把此主机设为DMZ或者用端口映射的方式暴露出来,效果是一样的,具体步骤不再赘述。

3. SSL对路由器性能开销不小,笔者的newifiD1在测试大文件下载时速度要比HTTP降低一半左右,且路由器负载跑满,有条件应使用高配路由器。

Pandorabox固件路由器上申请Let's Encrypt证书,为内网里的多个web服务提供SSL支持的相关教程结束。

《Pandorabox固件路由器上申请Let's Encrypt证书,为内网里的多个web服务提供SSL支持.doc》

下载本文的Word格式文档,以方便收藏与打印。