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

使用Nginx Alias与Rewrite时出现Error 500问题求助

解决Nginx多目录URL Rewrite导致的500错误

错误原因

错误日志明确提示:"alias" cannot be used in location "/folder1" where URI was rewritten——在同一个location块里同时使用alias和rewrite会触发冲突,这是Nginx的规则限制。你的rewrite指令完全多余,因为alias本身就负责将请求URI映射到指定的文件系统路径,不需要额外改写URI。

修正方案(两种可选)

方案一:保留alias,移除多余的rewrite

直接删掉每个location里的rewrite指令,alias会自动处理路径映射。修正后的配置如下:

server {
    server_name mydns.com.br;

    listen 443 ssl;
    ssl_certificate /etc/nginx/ssl/godaddy-chained.crt ;
    ssl_certificate_key /etc/nginx/ssl/server.key ;

    root /var/www/manuais/index-site/;

    location /folder1/ {
        alias /var/www/manuais/folder1/;
        index index.html;
        satisfy any;
        allow myIP;
        allow myIP;
        deny all;
        auth_basic "Auth";
        auth_basic_user_file myAUTH;
    }

    location /folder2/ {
        alias /var/www/manuais/folder2/;
        index index.html;
        satisfy any;
        allow myip;
        allow myip;
        deny all;
        auth_basic "Auth";
        auth_basic_user_file myAuth;
    }
}

方案二:改用root替代alias

如果更习惯用root,可以调整location的匹配规则和root路径,同样不需要rewrite:

server {
    server_name mydns.com.br;

    listen 443 ssl;
    ssl_certificate /etc/nginx/ssl/godaddy-chained.crt ;
    ssl_certificate_key /etc/nginx/ssl/server.key ;

    root /var/www/manuais/index-site/;

    location ^~ /folder1/ {
        root /var/www/manuais/;
        index index.html;
        satisfy any;
        allow myIP;
        allow myIP;
        deny all;
        auth_basic "Auth";
        auth_basic_user_file myAUTH;
    }

    location ^~ /folder2/ {
        root /var/www/manuais/;
        index index.html;
        satisfy any;
        allow myip;
        allow myip;
        deny all;
        auth_basic "Auth";
        auth_basic_user_file myAuth;
    }
}

这里^~是为了让Nginx优先匹配这个location,避免和其他正则location冲突;root /var/www/manuais/会把请求/folder1/xxx映射到/var/www/manuais/folder1/xxx,和原alias的效果一致。

验证方法

修改配置后,执行以下命令检查语法并重启Nginx:

nginx -t
systemctl reload nginx

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 03:01:08