diff --git a/404 b/404 index 2e333fa..dafe323 100644 --- a/404 +++ b/404 @@ -1,7 +1,6 @@ #!/bin/bash -# 日系动漫风格404页面一键部署脚本 - 修复版 -# 适用于Nginx反向代理环境 +# Docker环境日系动漫风格404页面一键部署脚本 set -e @@ -29,59 +28,45 @@ log_error() { echo -e "${RED}[ERROR]${NC} $1" } -# 检查Nginx是否安装(修复版) -check_nginx() { - log_info "检查Nginx安装状态..." +# 检查Docker环境 +check_docker_nginx() { + log_info "检查Docker Nginx容器..." - # 多种方式检查Nginx - if command -v nginx &> /dev/null; then - log_success "通过command命令检测到Nginx" - return 0 + # 查找运行的Nginx容器 + local nginx_container=$(docker ps --filter "name=nginx" --format "{{.Names}}" | head -1) + + if [ -z "$nginx_container" ]; then + nginx_container=$(docker ps --filter "publish=80" --format "{{.Names}}" | head -1) fi - if systemctl is-active --quiet nginx 2>/dev/null; then - log_success "通过systemctl检测到Nginx服务正在运行" - return 0 + if [ -z "$nginx_container" ]; then + nginx_container=$(docker ps --filter "publish=443" --format "{{.Names}}" | head -1) fi - if ps aux | grep -v grep | grep nginx &> /dev/null; then - log_success "通过进程检测到Nginx正在运行" + if [ -n "$nginx_container" ]; then + log_success "找到Nginx容器: $nginx_container" + echo "$nginx_container" return 0 + else + log_error "未找到运行的Nginx容器" + return 1 fi - - if [ -f /usr/sbin/nginx ] || [ -f /usr/local/nginx/sbin/nginx ]; then - log_success "通过文件路径检测到Nginx" - return 0 - fi - - log_warning "未检测到Nginx,但继续执行(可能是自定义安装)" - return 0 } -# 创建404页面 +# 创建404页面到宿主机目录 create_404_page() { - local html_path="/usr/share/nginx/html/404.html" - local custom_path="/var/www/html/404.html" + local host_data_dir="/boot/data/dataxn" log_info "正在创建日系动漫风格404页面..." - # 尝试多个可能的HTML目录 - for path in "$html_path" "$custom_path"; do - local dir=$(dirname "$path") - if [ -d "$dir" ]; then - html_path="$path" - log_info "使用HTML目录: $dir" - break - fi - done - - # 如果目录不存在,创建它 - local dir=$(dirname "$html_path") - if [ ! -d "$dir" ]; then - log_warning "目录 $dir 不存在,正在创建..." - mkdir -p "$dir" + # 检查宿主机目录是否存在 + if [ ! -d "$host_data_dir" ]; then + log_warning "目录 $host_data_dir 不存在,正在创建..." + mkdir -p "$host_data_dir" fi + local html_path="$host_data_dir/404.html" + cat > "$html_path" << 'EOF' @@ -415,91 +400,46 @@ EOF log_success "404页面已创建: $html_path" } -# 查找Nginx配置目录 -find_nginx_config() { - log_info "查找Nginx配置目录..." +# 为Docker容器创建Nginx配置 +create_nginx_config() { + local container_name="$1" - local possible_paths=( - "/etc/nginx" - "/usr/local/nginx/conf" - "/usr/local/etc/nginx" - ) + log_info "为容器 $container_name 创建Nginx配置..." - for path in "${possible_paths[@]}"; do - if [ -d "$path" ]; then - echo "$path" - log_success "找到Nginx配置目录: $path" - return 0 - fi - done + # 检查容器内的Nginx配置路径 + local nginx_conf_path="/etc/nginx/conf.d/custom_404.conf" - log_error "未找到Nginx配置目录" - return 1 -} - -# 配置Nginx -configure_nginx() { - log_info "正在配置Nginx..." - - local nginx_conf_dir=$(find_nginx_config) - if [ $? -ne 0 ]; then - log_error "无法找到Nginx配置目录" - return 1 - fi - - local sites_available_dir="$nginx_conf_dir/sites-available" - local sites_enabled_dir="$nginx_conf_dir/sites-enabled" - local conf_d_dir="$nginx_conf_dir/conf.d" - - # 创建404配置片段 - local error_conf="$nginx_conf_dir/conf.d/custom_errors.conf" - cat > "$error_conf" << 'EOF' -# 自定义错误页面配置 + # 创建配置内容 + local config_content=$(cat << 'EOF' +# 自定义404错误页面配置 error_page 404 /404.html; -error_page 500 502 503 504 /50x.html; location = /404.html { root /usr/share/nginx/html; internal; -} - -location = /50x.html { - root /usr/share/nginx/html; - internal; + allow all; } EOF +) - log_success "已创建错误页面配置: $error_conf" + # 将配置写入容器 + echo "$config_content" | docker exec -i "$container_name" tee "$nginx_conf_path" > /dev/null - # 如果存在sites-available目录,也为每个站点配置 - if [ -d "$sites_available_dir" ]; then - for config_file in "$sites_available_dir"/*; do - if [[ -f "$config_file" && ! -L "$config_file" ]]; then - local config_name=$(basename "$config_file") - log_info "检查站点配置: $config_name" - - # 备份原配置 - cp "$config_file" "$config_file.backup.$(date +%Y%m%d%H%M%S)" - - # 检查是否已存在404配置 - if ! grep -q "error_page 404" "$config_file"; then - # 在server块中添加404配置 - if grep -q "server {" "$config_file"; then - sed -i '/server {/a\\n # 自定义错误页面\n error_page 404 /404.html;\n location = /404.html {\n root /usr/share/nginx/html;\n internal;\n }' "$config_file" - log_success "已为 $config_name 添加404配置" - fi - else - log_info "$config_name 已有404配置,跳过" - fi - fi - done + if [ $? -eq 0 ]; then + log_success "Nginx配置已写入容器: $nginx_conf_path" + else + log_error "无法写入容器配置,请检查容器权限" + return 1 fi } # 测试Nginx配置 -test_nginx() { - log_info "测试Nginx配置..." - if nginx -t 2>/dev/null || /usr/local/nginx/sbin/nginx -t 2>/dev/null; then +test_nginx_config() { + local container_name="$1" + + log_info "测试容器Nginx配置..." + + if docker exec "$container_name" nginx -t; then log_success "Nginx配置测试通过" return 0 else @@ -510,61 +450,61 @@ test_nginx() { # 重新加载Nginx reload_nginx() { - log_info "重新加载Nginx配置..." + local container_name="$1" - # 多种方式重载Nginx - if systemctl reload nginx 2>/dev/null; then - log_success "通过systemctl重新加载Nginx" + log_info "重新加载容器Nginx配置..." + + if docker exec "$container_name" nginx -s reload; then + log_success "Nginx配置已重新加载" return 0 + else + log_error "Nginx重新加载失败" + return 1 fi - - if service nginx reload 2>/dev/null; then - log_success "通过service重新加载Nginx" - return 0 - fi - - if nginx -s reload 2>/dev/null; then - log_success "通过nginx命令重新加载Nginx" - return 0 - fi - - log_warning "无法自动重新加载Nginx,请手动执行: nginx -s reload" - return 1 } -# 显示完成信息 -show_completion() { +# 显示Docker特定信息 +show_docker_info() { + local container_name="$1" + echo log_success "日系动漫风格404页面部署完成!" echo log_info "部署详情:" - log_info "- 404页面位置: /usr/share/nginx/html/404.html" - log_info "- Nginx配置已更新" - log_info "- 创建了自定义错误页面配置" + log_info "- 宿主机404页面: /boot/data/dataxn/404.html" + log_info "- Nginx容器: $container_name" + log_info "- 容器内配置: /etc/nginx/conf.d/custom_404.conf" echo - log_info "您可以通过访问一个不存在的URL来测试404页面" - log_info "例如: curl http://your-domain/not-exist-page" + log_info "目录映射关系:" + log_info "- 宿主机: /boot/data/dataxn → 容器: (请检查您的具体映射路径)" echo - log_warning "如果页面未立即生效,请手动重启Nginx服务" + log_info "测试方法:" + log_info "1. 访问一个不存在的页面: curl http://your-domain/not-exist-page" + log_info "2. 或直接在浏览器中访问不存在的URL" + echo + log_warning "如果您的目录映射不是默认的/usr/share/nginx/html,请手动调整配置" } # 主函数 main() { echo - log_info "开始部署日系动漫风格404页面..." + log_info "开始为Docker Nginx部署日系动漫风格404页面..." echo - # 检查环境 - check_nginx + # 检查Docker环境 + local container_name=$(check_docker_nginx) + if [ $? -ne 0 ]; then + exit 1 + fi # 执行部署步骤 create_404_page - configure_nginx + create_nginx_config "$container_name" # 测试并重新加载Nginx - if test_nginx; then - reload_nginx - show_completion + if test_nginx_config "$container_name"; then + reload_nginx "$container_name" + show_docker_info "$container_name" else log_error "部署失败,请检查Nginx配置" exit 1 @@ -573,7 +513,7 @@ main() { # 显示使用说明 usage() { - echo "日系动漫风格404页面一键部署脚本 - 修复版" + echo "Docker环境日系动漫风格404页面一键部署脚本" echo echo "使用方法: $0 [选项]" echo