nginx 的位置块配置错误?一直显示 404

nginx 的位置块配置错误?一直显示 404

http://192.168.0.37/hls我对 nginx 还很陌生。当我访问或 时,我得到 404 not found 错误http://192.168.0.37/hls/

这是我的/etc/nginx/nginx.conf文件:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

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

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;


        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
        server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;

        root /var/www/html;
        index index.html;

        location /hls {
                root /var/www/html;
                index testtest.html;
            }
        }
}

我的目录中什么都没有/etc/nginx/sites-enabled/。文件/var/www/html/index.html/var/www/html/testtest.html均归其所有www-data并具有755权限。

/var/www/html/testtest.html为什么我访问http://192.168.0.37/hls或时看不到 的内容http://192.168.0.37/hls/


然而,我能够看到http://192.168.0.37/testtest.html

答案1

Nginx 正在寻找 处的文件/var/www/html/hls/testtest.html

URL 的路径部分是/hls/location /hls选择该块是为了处理请求

index testtest.html;指令将路径部分转换为/hls/testtest.html

root /var/www/html;指令通过以下方式将其转换为路径名将根值与路径组件连接起来-/var/www/html/hls/testtest.html


或者,使用alias指令它先从路径部分中减去位置值,然后将其解析为路径名。

任何一个:

location /hls {
    alias /var/www/html;
    index testtest.html;
}

或者:

location /hls/ {
    alias /var/www/html/;
    index testtest.html;
}

/hls/testtest.html将在 处查找/var/www/html/testtest.html。请注意,为了正确操作,locationalias值都应以 结尾,/或者都不以 结尾/

相关内容