You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx仅可正常处理PHP文件,其他格式文件均返回404错误求助

问题诊断与修复方案

核心原因

静态文件返回404的关键问题是版本号重写规则的执行顺序错误:

  • 正则匹配的静态文件location ~ \.(css|js|...)优先级高于前缀location /,且早于rewrite规则执行
  • 当请求带版本号的静态文件(如/CRM/css/v1.0.0/style.css)时,静态文件location会直接尝试查找原路径文件,而重写规则还没触发,导致找不到真实文件返回404

此外,静态文件location的try_files配置可优化得更健壮。

修复步骤

1. 调整重写规则位置

将版本号重写规则移到所有location块之前,确保重写在任何location匹配前执行:

server {
    listen 80;
    server_name localhost;
    root /var/www/html;
    index index.php;

    sendfile on;

    # 前置版本号重写规则,保证优先执行
    rewrite ^/CRM/(.*)/v[0-9]+\.[0-9]+\.[0-9]+/(.*)$ /CRM/$1/$2 last;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    # 其余location块保持不变
}

2. 优化静态文件location的try_files

将try_files $uri =404改为try_files $uri $uri/ =404,兼容可能的目录请求(增强配置健壮性):

location ~ \.(css|js|gif|jpe?g|png|svg|woff|woff2|ttf|eot)$ {
    access_log off;
    expires 30d;
    add_header Cache-Control "public, max-age=2592000";
    add_header Pragma public;
    add_header Vary Accept-Encoding;
    try_files $uri $uri/ =404;
}

3. 验证文件路径与权限

  • 确认静态文件实际存储路径与Nginx的root配置对应:若请求路径是/CRM/css/style.css,文件需存在于/var/www/html/CRM/css/style.css
  • 修复文件权限(确保Nginx进程可读取):
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html

完整修复后的配置

error_log /var/log/nginx/error.log notice;
pid       /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/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 /var/log/nginx/access.log main;

    sendfile on;
    keepalive_timeout 65;

    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_min_length 1000;

    server {
        listen 80;
        server_name localhost;
        root /var/www/html;
        index index.php;

        sendfile on;

        # 前置版本号重写规则
        rewrite ^/CRM/(.*)/v[0-9]+\.[0-9]+\.[0-9]+/(.*)$ /CRM/$1/$2 last;

        location = /favicon.ico {
            log_not_found off;
            access_log off;
        }

        location = /robots.txt {
            log_not_found off;
            access_log off;
        }

        location ~ \.(css|js|gif|jpe?g|png|svg|woff|woff2|ttf|eot)$ {
            access_log off;
            expires 30d;
            add_header Cache-Control "public, max-age=2592000";
            add_header Pragma public;
            add_header Vary Accept-Encoding;
            try_files $uri $uri/ =404;
        }

        location / {
            try_files $uri $uri/ /CRM/index.php?$query_string;
        }

        location ~ \.php$ {
            fastcgi_pass   crm:9000;
            fastcgi_index  index.php;

            fastcgi_param  REQUEST_METHOD $request_method;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

验证方法

  1. 重新加载Nginx配置:sudo nginx -s reload
  2. 访问带版本号的静态文件(如http://localhost/CRM/css/v1.0.0/style.css),确认返回正常内容而非404
  3. 查看错误日志排查问题:tail -f /var/log/nginx/error.log

内容的提问来源于stack exchange,提问作者Rafael Bitencourt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 14:02:48