45 Nginx的index指令

45 Nginx的index指令

45.1 index指令

index:网站的默认首页

语法 index file ...;
默认值 index index.html;
位置 http 、 server 、location

 

 

 

 

index 后面可以跟多个设置,如果访问时,没有指定具体访问的资源,则会从左到右挨个找,找到第一个能用的就用,找不到返回 403

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;
        
        location ^~/abcd {
          default_type text/plain;
          return 200 'abcd access success';
        }
        location ~*^/abc\w$ {
      default_type text/plain;
      return 200 'access success';
        }

        location /images/ {
          alias html/images/;
          index mv.png;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code

浏览器访问:http://10.0.0.100/images/   ,自动匹配到 mv.png

image

45.2 举例

       location / {
            root   html;
            index  index.html index.htm;
        }

访问该 location 时,可以通过 http://ip:port/ ,地址后面如果不添加任何内容,则默认依此访问 index.html 和 index.htm,从左到右匹配,匹配到就返回,匹配不到返回 403

 

———————————————————————————————————————————————————————————————————————————

                                                                                                                         无敌小马爱学习

posted on 2026-05-12 10:32  马俊南  阅读(8)  评论(0)    收藏  举报