nginx模块,配置指令,块之间的联系

安装nginx以后,nginx会提供一个默认server,我们可以从nginx.conf文件中找到这个默认server的相关配置,如下:

server {
    listen       80;
    server_name  localhost;
 
    location / {
        root   html;
        index  index.html index.htm;
    }
 
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
 
}

一个http块中可以配置多个server块,一个server块中可以配置多个location,这里配置一个新的location:

[root@server1 conf]# vim nginx.conf
 写入:
 41         location /demo {
 42             root   /opt;
 43             index  index.html index.htm;
 44         }

这个location对应的url为/demo,此location块中的root /opt;配置指令表示这个location的文档根目录为/opt目录,所以,当我们在浏览器中访问/demo/1.jpg这个url时,访问的其实是服务器中的/opt/demo/1.jpg文件

在服务器上创建/opt/demo/目录,并且在此目录中创建了一个名为index.html的文件,index.html内容如下:

cd /opt/
mkdir demo
cd demo/
vim index.html
写入:www.westos.org

之后执行nginx -s reload命令重载配置,重载配置以后,在浏览器中访问链接地址http://172.25.63.1/demo/,效果如下:
在这里插入图片描述
自定义的location已经生效了,当我们访问http://172.25.63.1/demo/这个url时,即可访问到服务器的/opt/demo/index.html文件,在同一个web服务中,我们可以将不同的url对应到不同的服务器路径中,上例中,除了默认的location,我们又手动配置了一个新的location,默认的location为/,我们手动配置的新的location为/demo,所以,访问这两个url时,会分别对应的不同的文档根目录,从不同的目录中查找对应的资源,又因为index配置指令的原因,会默认访问对应目录中的index.html文件或index.htm文件。

同时,默认的location和新加入的location中,index配置指令的值是完全相同的,由于这两个location的index配置完全相同,所以,可以把这个index配置项提取到上一级的server块中,以便这两个location共享这个index配置,配置如下:

   server {
        listen       80;
        server_name  localhost;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
 
        location /demo {
            root   /opt;
            index  index.html index.htm;
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
    }

将index配置指令提取到了这两个location块的上一级server块中,以便两个location块能够共享这个index配置,换句话说就是,server块中的index指令是对当前server块中的所有location生效的,当然,如果某个location块中有自己的index配置,那么针对当前location块来说,还是以自己的index配置为准,比如如下配置:

[root@server1 conf]# vim nginx.conf

 29     server {
 30         listen       80;
 31         server_name  localhost;
 32 
 33 
 34 
 35         location / {
 36             root   html;
 37 
 38         }
 39 
 40 
 41         location /demo {
 42             root   /opt;
 43             index 1.jpg;
 44         }
 45 
 46 

上述配置中,server块中index配置是对当前server块中的所有location块生效的,但是由于 location /demo中有自己的index配置,那么针对 location /demo来说,自己的index配置的优先级更高,自己的index配置会覆盖上一级的index配置,所以,当我们访问/demo这个url时,默认会在对应的目录中查找1.jpg这个文件,而不是index.html文件,但是另外一个location中由于没有配置index指令,所以它仍然会以上一级server块中的index配置为准。此时在浏览器访问http://172.25.63.1/demo/
在这里插入图片描述可知访问的是/opt/demo/1.jpg

某些配置指令只能在http块中配置,某些配置指令只能在location块中配置,有些配置指令既能在server块中配置又能在http块中配置,而有些配置指令只能在main区中进行配置。

index指令就属于那种既能在location块中配置,又能在server块中配置,还能再http块中配置的指令,只不过,当index指令配置在不同的块中时,对应的作用域不同。

同样,有些指令既能配置在server块中,也能配置在http块中,当多个server存在相同的配置时,我们可以将这些完全相同的配置指令提取到上一级的http块中,以便多个server块共用这些配置,当然,如果你在某个server中单独配置了对应的配置指令,那么这个server仍然会以自己的配置为准

“配置指令"不仅和"块"有一定的关系,“配置指令"和"模块"也有着非常紧密的对应关系,nginx是模块化的,不同的"模块"负责不同的"功能”,所以,当我们需要针对某个"功能"进行配置时,就需要使用到对应的"配置指令”,从根本上来说,每个"配置指令"都属于某一个"模块",一个"模块"中会有一个或多个"配置指令",当我们想要对相关模块或者功能进行设置时,就会使用到对应模块中的配置指令。在nginx编译安装过程中,除了内置的标准模块,还有一些可选模块,我们可以在编译安装时选择安装哪些可选模块,如果你没有安装对应的可选模块,那么你也无法使用对应的配置指令。原因刚才已经说过,每个配置指令都属于某一个模块。

发布了127 篇原创文章 · 获赞 65 · 访问量 4356

猜你喜欢

转载自blog.csdn.net/qq_35887546/article/details/104501968