使用nginx搭建的文件服务器一直提示 404 Not Found ,是文件不存在还是没有权限?

使用nginx搭建一个简单的文件服务器,配置如下

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 10;
}

http {
    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;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    autoindex on;               # 显示目录
    autoindex_exact_size on;    # 显示文件大小
    autoindex_localtime on;     # 显示文件时间

    server {
        listen       6000 default_server;
        listen       [::]:6000 default_server;
        server_name  _;
        root         /tmp/;

        location / {
        }

        error_page 404 /404.html;
                location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
                location = /50x.html {
        }
    }
    include /etc/nginx/conf.d/*.conf;
}

配置完成后,启动nginx

service nginx restart

没有错误提示,服务正常启动,但是访问时出现了问题,
一直提示:

404 Not Found

通过观察日志发现并没有其他异常

2024/03/20 11:07:49 [error] 26641#26641: *1 "/tmp/index.html" is not found (2: No such file or directory), client: 192.168.0.0, server: _, request: "GET / HTTP/1.1", host: "192.168.0.1:6000"
2024/03/20 11:07:49 [error] 26641#26641: *1 open() "/tmp/404.html" failed (2: No such file or directory), client: 192.168.0.0, server: _, request: "GET / HTTP/1.1", host: "192.168.0.1:6000"
2024/03/20 11:07:49 [error] 26641#26641: *1 "/tmp/index.html" is not found (2: No such file or directory), client: 192.168.0.0, server: _, request: "GET / HTTP/1.1", host: "192.168.0.1:6000"
2024/03/20 11:07:49 [error] 26641#26641: *1 open() "/tmp/404.html" failed (2: No such file or directory), client: 192.168.0.0, server: _, request: "GET / HTTP/1.1", host: "192.168.0.1:6000"

各种检测和调试,发现设置目录为根目录/和其他目录正常,看来是和目录有关系,通过查看配置文件

 cat /usr/lib/systemd/system/nginx.service
 
 [Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true  # !!!

[Install]
WantedBy=multi-user.target

上面的PrivateTmp=true这说明这个服务使用到tmp目录是默认会创建一个私有的文件夹来使用,如果不想要使用,只需要把这个true设置为false就可以了,然后重启nginx,可以正常访问

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值