在做ICT集成项目时,遇到需要对现网接口进行拨测的需求。搜索后尝试使用开源的Uptime Kuma组件完成现网接口拨测。
但该项目有个问题就是默认不支持配置固定前缀,这对现网进行请求转发会造成较大的影响。通过查看该项目的github后找到了问题的解决方案。Subfolder support (publicPath) · Issue #147 · louislam/uptime-kuma · GitHub
解决的方案就是在Nginx上追加以下配置:
location /subpath {
proxy_pass http://127.0.0.1:3001;
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 X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# define url prefix
set $url_prefix subpath;
# remove url prefix to pass to backend
rewrite ^/subpath/?(.*)$ /$1 break;
# redirect location headers
proxy_redirect ^ /$url_prefix;
proxy_redirect /dashboard /$url_prefix/dashboard;
# sub filters to replace hardcoded paths
proxy_set_header Accept-Encoding "";
sub_filter_last_modified on;
sub_filter_once off;
sub_filter_types *;
sub_filter '/assets/' '/$url_prefix/assets/';
sub_filter '"assets/' '"$url_prefix/assets/';
sub_filter '/socket.io' '/$url_prefix/socket.io';
sub_filter '/icon.svg' '/$url_prefix/icon.svg';
sub_filter '/favicon.ico' '/$url_prefix/favicon.ico';
sub_filter '/apple-touch-icon.png' '/$url_prefix/apple-touch-icon.png';
sub_filter '/manifest.json' '/$url_prefix/manifest.json';
sub_filter '/add' '/$url_prefix/add';
sub_filter '/settings/' '/$url_prefix/settings/';
sub_filter '"/settings' '"/$url_prefix/settings';
sub_filter '/dashboard' '/$url_prefix/dashboard';
sub_filter '/maintenance' '/$url_prefix/maintenance';
sub_filter '/add-status-page' '/$url_prefix/add-status-page';
sub_filter '/manage-status-page' '/$url_prefix/manage-status-page';
sub_filter '"/status/' '"/$url_prefix/status/';
sub_filter '/upload/' '/$url_prefix/upload/';
sub_filter '/api/' '/$url_prefix/api/';
}
实际运行时又遇到报错:unknown directive 'sub_filter_last_modified',需要重新编译Nginx,在编译时增加--with-http_sub_module命令,具体下载和重新编译Nginx的过程不再赘述。
最终可以达到通过固定前缀,访问Uptime Kuma项目的结果。