本项目实现了一个智能的AList服务访问配置,通过修改Nginx代理配置和PHP脚本,实现了以下功能:
本次实现主要修改了以下文件:
/www/server/panel/vhost/nginx/proxy/alist.xinghuoseo.com/46d9a76ee9f25d6fe22e820d7ccc99b3_alist.xinghuoseo.com.conf/www/wwwroot/alist.xinghuoseo.com/index.php(原文件,未修改但利用了其功能)/www/wwwroot/alist.xinghuoseo.com/autorefresh.html(作为备用访问入口)主要添加了智能判断AList服务状态的请求转发逻辑:
# 根目录请求首先尝试反向代理到AList服务,如果失败则回退到index.php
location = / {
# 尝试直接代理到AList服务
proxy_pass http://127.0.0.1:5244;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 如果代理失败(AList服务未运行),回退到index.php
error_page 502 = @fallback;
}
# 失败回退处理
location @fallback {
try_files /index.php =404;
fastcgi_pass unix:/tmp/php-cgi-74.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
include pathinfo.conf;
}
同时保留了以下必要配置:
# 让PHP能够正常处理
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi-74.sock;
fastcgi_index index.php;
include fastcgi.conf;
include pathinfo.conf;
}
# 为自动刷新的页面设置特殊处理
location = /autorefresh.html {
try_files $uri =404;
alias /www/wwwroot/alist.xinghuoseo.com/index.php;
}
# 所有其他请求都反向代理到AList服务
location / {
proxy_pass http://127.0.0.1:5244;
proxy_set_header Host $http_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;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
# 其他缓存相关配置...
}
虽然没有修改index.php文件内容,但充分利用了其以下核心功能:
// 检测AList服务是否正在运行
function isAlistRunning() {
// 使用netstat命令检查5244端口是否被占用
$output = shell_exec('netstat -tuln | grep :5244');
return !empty($output);
}
// 启动AList服务
function startAlistService() {
// 完整的AList可执行文件路径
$alistPath = '/www/wwwroot/alist.xinghuoseo.com/alist/alist';
// 使用nohup在后台启动AList服务
shell_exec('nohup ' . $alistPath . ' server > /dev/null 2>&1 &');
// 等待服务启动
sleep(3);
return isAlistRunning();
}
// 根据服务状态显示不同页面
$isRunning = isAlistRunning();
if ($isRunning) {
// 显示服务运行状态页面
} else {
// 显示服务未运行状态页面和启动按钮
}
创建了一个备用访问入口,内容与index.php类似,提供AList服务状态的替代访问方式。
不会影响其他网站,原因如下:
/www/server/panel/vhost/nginx/proxy/alist.xinghuoseo.com/目录下整体实现基于以下工作流程:
https://alist.xinghuoseo.com/,系统会自动处理https://alist.xinghuoseo.com/autorefresh.html访问状态页面创建时间:2025年10月1日
更新记录:
- 初始版本:实现智能代理和状态检测功能