使用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,可以正常访问

Logo

一起探索未来云端世界的核心,云原生技术专区带您领略创新、高效和可扩展的云计算解决方案,引领您在数字化时代的成功之路。

更多推荐