关于linux:Linux上使用Nginx搭建本地静态资源服务器

6次阅读

共计 953 个字符,预计需要花费 3 分钟才能阅读完成。

Nginx 映射本地目录

大抵步骤:

1. 开启日志,便于呈现问题排查

error_log /var/log/nginx/error.log warn;

2. 批改 /etc/nginx/conf.d/default.conf nginx 配置文件, 寄存动态文件目录地址 /www/share/
server {
    listen       80;
    server_name  antdesign;
    
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /www/ant-design-pro/dist;
        index  index.html;
        # 解决页面刷新不重定向
        try_files $uri $uri/ /index.html;
    }
    # 搭建本地动态资源服务器配置
    location /img {
        alias /www/share;
        autoindex on;
    }
 }

留神:

1. 拜访页面报 403 谬误,查看日志发现:directory index of “/www/share/signImg/” is forbidden
起因是未配置 index
location /img {
        alias /www/share;
        index index.html;
    }
如果只是想列出目录内容, 应用 autoindex on
location /img {
        alias /www/share;
        autoindex on;
    }

2. 查看日志发现 (13: Permission denied) Nginx 403 forbidden forbidden 403 Permission denied

起因是目录权限不够,解决办法:批改 web 目录的读写权限,或者是把 nginx 的启动用户改成目录的所属用户,重启 Nginx 即可解决。
chmod -R 777 /data

chmod -R 777 /data/www/

root 与 alias 次要区别——在于 nginx 如何解释 location 前面的 uri,这会使两者别离以不同的形式将申请映射到服务器文件上。
root 的处理结果是:root 门路+location 门路
alias 的处理结果是:应用 alias 门路替换 location 门路
alias 是一个目录别名的定义,root 则是最上层目录的定义。
网上说 alias 前面必须要用 ”/” 完结,否则会找不到文件的,自己测试可有可无。

正文完
 0