V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
itabas016
V2EX  ›  NGINX

我想用树莓派搭一个文件服务器,然后通过 NGINX 访问,中间遇到了个用户权限的问题,小白一枚诚心请教...

  •  
  •   itabas016 ·
    itabas016 · 2016-09-11 21:38:02 +08:00 · 5435 次点击
    这是一个创建于 2755 天前的主题,其中的信息可能已经有所发展或是发生改变。

    主页访问正常:

    • share
    • book
    • music
    • image
    • film

    访问子链接就报 403 error:

    2016/09/11 18:39:18 [error] 22904#0: *1 open() "/media/pi/ITABAS/Book" failed (13: Permission denied), 
    client: 192.168.11.201, server: ~^\d+\.\d+\.\d+\.\d+$, request: "GET /Book HTTP/1.1",...
    

    外接硬盘目录结构及权限, 用户 pi

    pi@raspberrypi:/ $ ls -al /media/pi/ITABAS/
    total 8
    drwx------  1 pi   pi   4096 Sep 10 01:10 .
    drwxr-x---+ 3 root root 4096 Sep  8 22:13 ..
    drwx------  1 pi   pi    176 Sep 11 19:40 Book
    drwx------  1 pi   pi    168 Sep 11 01:21 download
    drwx------  1 pi   pi      0 Sep  9 23:07 Film
    drwx------  1 pi   pi    144 Sep 10 00:13 Image
    drwx------  1 pi   pi    160 Sep 10 00:12 Music
    drwx------  1 pi   pi      0 Sep  9 23:07 Photo
    
    sudo chown -R www-data:www-data /media/pi/ITABAS/
    sudo chmod 755 /media/pi/ITABAS/
    

    执行后权限无变化

    Index 主页目录及权限

    pi@raspberrypi:/ $ ls -al /var/www/nas/
    total 16
    drwxr-xr-x 2 www-data www-data 4096 Sep 11 18:18 .
    drwxr-xr-x 5 root     root     4096 Sep 11 12:30 ..
    -rw-r--r-- 1 www-data www-data  451 Sep 11 18:18 index.html
    -rw-r--r-- 1 www-data www-data  840 Sep 11 16:58 index.html.bk
    
    

    NGINX 站点 conf 配置, nginx 默认用户 www-data

    server {
            listen 8080 default_server;
    
            root /media/pi/ITABAS;
    
            server_name ~^\d+\.\d+\.\d+\.\d+$;
    
            location / {
                    root /var/www/nas/;
                    index index.html index.htm index.nginx-debian.html;
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    try_files $uri $uri/ =404;
            }
    
             location /download {
                    autoindex on;
                    autoindex_exact_size on;
                    autoindex_localtime on;
                    allow   all;
            }
    
            location /Book {
                    autoindex on;
                    autoindex_exact_size on;
                    autoindex_localtime on;
                    allow   all;
            }
     }
    

    另外我的外接硬盘还做了 samba, 把 www-data 加进去也没有反应...

    [usbhdd]
        comment = HDD Media Storage
        path = /media/pi/ITABAS/
        browseable = yes
        writable = yes
        create mask = 0664
        directory mask = 0775
        valid users = root pi www-data
    

    麻烦大家指点一下,谢谢~

    9 条回复    2016-09-12 23:27:57 +08:00
    misaka19000
        1
    misaka19000  
       2016-09-11 22:47:06 +08:00
    如果 Nginx 中不使用外接硬盘而是直接访问本地的文件能够访问吗?
    itabas016
        2
    itabas016  
    OP
       2016-09-12 10:26:22 +08:00
    @misaka19000 我的 index 主页是本地文件, www-data 可以访问。链接是指向的外接硬盘目录,用户是 pi, 我执行
    ```
    sudo chown -R www-data:www-data /media/pi/ITABAS/
    sudo chmod 755 /media/pi/ITABAS/
    ```
    但是文件所有者权限却没有改变, 还是 pi
    arcas
        3
    arcas  
       2016-09-12 10:48:23 +08:00
    @itabas016 外置硬盘文件系统不兼容。
    fangdingjun
        4
    fangdingjun  
       2016-09-12 10:49:43 +08:00
    ntfs-3g 挂载的 NTFS 分区,挂载后是无法改变所有者和权限的, 但是可以在挂载时指定

    建议让 nginx 以 pi 用户运行
    misaka19000
        5
    misaka19000  
       2016-09-12 11:06:39 +08:00
    @itabas016 那直接使用 pi 用户启动 Nginx 试一下
    itabas016
        6
    itabas016  
    OP
       2016-09-12 11:20:58 +08:00
    @arcas
    @fangdingjun
    @misaka19000 我回家试一试。多谢~
    ety001
        7
    ety001  
       2016-09-12 13:33:01 +08:00
    你用的是 pi 用户挂载的外设,所以权限就不会修改成功的。建议修改 nginx 配置文件 nginx.conf 中的用户项,让 nginx 以 pi 用户身份启动。
    cike
        8
    cike  
       2016-09-12 13:45:23 +08:00
    有没有关掉 selinux ?
    itabas016
        9
    itabas016  
    OP
       2016-09-12 23:27:57 +08:00
    @fangdingjun
    @misaka19000 用 pi 启动可以 work 了, thx
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   965 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:46 · PVG 04:46 · LAX 13:46 · JFK 16:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.