nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > Nginx location静态文件映射配置

Nginx location静态文件映射配置过程

作者:宋发元

文章介绍了Nginx配置中root和alias指令的使用场景和区别,并通过实际问题分析,指出路径拼接逻辑错误导致静态文件无法访问,最终解决方案是将root改为alias,并确保路径正确

遇到问题?

以下这个Nginx的配置,愿意为访问https://abc.com会指向一个动态网站,访问https://abc.com/tongsongzj时会访问静态网站,但是配置之后(注意看后面那个location /tongsongzj/静态文件映射的配置),此时不能通过https://abc.com/tongsongzj/index.html正确访问,显示报错信息如下:

问:这是为啥?需要如何修改?

已经确认前端代码在宿主机路径/opt/docker-data/nginx/h5下,且对应的docker中nginx容器真实路径为/usr/share/nginx/h5

Nginx配置如下

# ABC官网Nginx配置
# 这里跳转到动态网站
server {
    listen 80;  # 监听80端口,用于HTTP请求
#     listen 443 ssl;  # 监听443端口,用于HTTPS请求,并启用SSL
    listen 443 ssl http2;  # 启用 HTTP/2 的 HTTPS 监听

    server_name www.abc.com abc.com;  # 绑定的域名
    root html;
    index index.html index.htm;
    # SSL证书和私钥的路径
    ssl_certificate conf.d/cert/abc.pem;
    ssl_certificate_key conf.d/cert/abc.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    #表示使用的加密套件的类型。
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #表示使用的TLS协议的类型。
    ssl_prefer_server_ciphers on;
    location / {
        #root html;  #Web网站程序存放目录。
        #index index.html index.htm;
        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://abc-cms:9999;
#         proxy_pass http://172.19.19.80:8082;
    }

    # 静态页面配置,代码传到/opt/docker-data/nginx/h5路径即可
    location /tongsongzj/ {
        # 静态网页配置
        root /usr/share/nginx/h5;  # 静态网页的根目录
        index index.html;  # 默认打开的文件
        try_files $uri $uri/ /index.html;  # 尝试查找URI对应的文件,否则返回index.html

        # 禁用缓存
        expires off;
        add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
    }
}

问题解决

静态文件无法访问的原因在于路径拼接方式错误

以下是详细分析和解决方案:

问题分析:路径拼接逻辑错误

当前配置中使用了 root 指令:

location /tongsongzj/ {
    root /usr/share/nginx/h5;  # 最终路径 = root + location
    ...
}

Nginx 会将 location 路径拼接到 root 后面,因此实际访问路径为:

/usr/share/nginx/h5/tongsongzj/index.html

但根据你的描述,静态文件直接存放在容器的 /usr/share/nginx/h5 路径下(对应宿主机 /opt/docker-data/nginx/h5),并未创建 tongsongzj 子目录,导致路径不匹配。

解决方案:改用 alias 指令

root 改为 alias 指令,并直接指向静态文件的实际路径:

location /tongsongzj/ {
    alias /usr/share/nginx/h5/;  # 直接映射路径,不拼接 location
    index index.html;
    try_files $uri $uri/ /tongsongzj/index.html;  # 修正回退路径

    # 禁用缓存配置
    expires off;
    add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
}

关键修改说明

  1. alias 会直接将 /tongsongzj/ 映射到容器内的 /usr/share/nginx/h5/ 目录,不再拼接 location 路径。
  2. 修正 try_files 确保回退到 /tongsongzj/index.html,避免路径错误。

其他注意事项

1.确认文件路径

确保容器内 /usr/share/nginx/h5 路径下直接存在 index.html 等静态文件,而不是嵌套在子目录中。

2.检查目录权限

运行以下命令确保 Nginx 有权限访问该目录:

docker exec [nginx容器名] ls -l /usr/share/nginx/h5

如果权限不足,尝试在宿主机执行:

chmod -R 755 /opt/docker-data/nginx/h5

3.检查 Nginx 日志

查看错误日志定位具体原因:

docker logs [nginx容器名] 2>&1 | grep -i error

完整修正配置参考

# 静态页面配置
location /tongsongzj/ {
    alias /usr/share/nginx/h5/;  # 使用 alias 直接映射真实路径
    index index.html;
    try_files $uri $uri/ /tongsongzj/index.html;  # 保持前端路由正常

    # 禁用缓存
    expires off;
    add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
}

或者

    # 静态页面配置,代码传到 /opt/docker-data/nginx/h5/tongsongzj 路径即可
    # 说明:使用root指令,Nginx 会将 location 路径拼接到 root 后面,因此实际访问路径为:/usr/share/nginx/h5/tongsongzj/index.html
    location /tongsongzj/ {
        # 静态网页配置
        root /usr/share/nginx/h5;  # 静态网页的根目录 【最终路径 = root + location】
        index index.html;  # 默认打开的文件
        try_files $uri $uri/ /index.html;  # 尝试查找URI对应的文件,否则返回index.html

        # 禁用缓存
        expires off;
        add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
    }

修改后重启 Nginx 服务即可生效:

docker exec [nginx容器名] nginx -s reload

root和alias的核心区别

Nginx 的 rootalias 指令都用于定义静态文件的路径映射,但它们的路径拼接逻辑完全不同。理解它们的区别是避免配置错误的关键。

1.root指令

定义

示例

location /static/ {
    root /var/www;
}

当访问 /static/image.jpg 时,Nginx 会查找文件:

/var/www/static/image.jpg

(路径 = root + location 的 URI)

优点

缺点

注意事项

2.alias指令

定义

示例

location /docs/ {
    alias /usr/share/nginx/html/;
}

当访问 /docs/readme.html 时,Nginx 会查找文件:

/usr/share/nginx/html/readme.html

(路径 = alias 路径 + URI 去除 location 前缀后的部分)

优点

缺点

注意事项

关键对比表格

特性rootalias
路径拼接逻辑root + location URI用 alias 完全替换 location URI
斜杠处理root 路径末尾不带斜杠alias 路径末尾必须带斜杠
适用场景URI 与文件路径一致URI 与文件路径不一致
嵌套路径支持自动支持(如 /static/css/)需手动处理(依赖 try_files)
常见错误文件路径多了一层子目录路径拼接错误(斜杠问题)

常见错误场景与修复

错误 1:路径多了一层子目录

错误配置(使用 root):

location /tongsongzj/ {
    root /usr/share/nginx/h5;
    index index.html;
}

修复方法:改用 alias

location /tongsongzj/ {
    alias /usr/share/nginx/h5/;  # 注意末尾斜杠
    index index.html;
}

错误 2:斜杠缺失导致路径拼接错误

错误配置

location /docs {
    alias /usr/share/nginx/html;  # 缺少末尾斜杠
}

修复方法

location /docs/ {                # location 末尾加斜杠
    alias /usr/share/nginx/html/;  # alias 末尾加斜杠
}

最佳实践

1.优先使用 root

2.必须用 alias 的场景

3.严格处理斜杠

4.结合 try_files 处理前端路由

location /tongsongzj/ {
    alias /usr/share/nginx/h5/;
    try_files $uri $uri/ /tongsongzj/index.html;  # 回退到 index.html
}

5.检查文件权限

确保 Nginx 进程有权限读取目录和文件:

chmod -R 755 /opt/docker-data/nginx/h5  # 宿主机路径

总结

核心区别在于路径拼接逻辑,务必通过实际文件路径验证配置。

配置完成后,使用 nginx -t 测试语法,并通过日志排查错误:

tail -f /var/log/nginx/error.log

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文