Linux企业实战-Nginx第一个静态web服务器

上个博客已将编译安装了nginx,在/usr/local/nginx目录中产生如下文件及含义

conf目录中存放了nginx相关的配置文件
html目录是默认提供的web服务的“根目录”logs目录是nginx日志的存放目录。
modules目录中存放了一些模块会用到的库。
sbin目录中存放了nginx的二进制文件,我们需要使用
nginx二进制文件启动nginx


启动nginx
[root@serverl nginx]# ls
conf  html  logs  modules  sbin
[root@serverl nginx]# cd sbin/
[root@serverl sbin]# ls
nginx
[root@serverl sbin]# ./nginx 
图片证明器启动

启动之后会发现文件变多了
[root@serverl nginx]# ls
client_body_temp  fastcgi_temp  logs     proxy_temp  scgi_temp
conf              html          modules  sbin        uwsgi_temp

查看nginx.conf文件
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;

    keepalive_timeout  65;
    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部分、location部分,或者说,上述示例可以分为几个逻辑块,http块、server块、location块,每个“配置块“都是使用大括号“{}“作为分界线的,而且,从缩进可以看出,它们是有层级关系的,http中可以配置多个server,一个server中可以配置多个location,我们知道,nginx最基础的功能就是用来提供http服务,所以,跟http有关的公共配置,可以放置在http块中,http块中又可以配置多个server

那么server代表了什么呢?我们在一台主机中安装了nginx,那么能不能让这台nginx主机同时提供多个web服务呢?答案是肯定的,每一个server就代表一个http服务,我们可以同时配置多个server,以便同时提供多个http服务,不同的server可以使用不同的配置,写入到某个server块中的配置只对对应的http服务生效,如果多个server存在共同的公用配置,则可以将共同的配置写在
http块中,以便多个server共享这些配置,一个server块中又可以有一个或多个location

location又是什么意思呢?当我们访问一个网络上的资源时,都是通过url访问的,你可以把location当做url的一部分,此处,我们使用如下url作为示例:httpl//www.westos.org/westos/1591上述链接中的"/westos“部分就是一个location,我们可以通过location将url中的路径和服务器的某个目录建立起关联关系,此处不用纠结,在用到它时我们再来细说

正如本文开头的示例所示,当我访问
"http://172.25.0.1/"这个网址时,默认会访问到nginx
服务器上的/usr/local/nginx/html/index.html文件,之所以会访问到这个文件,是由下面这段配置决定的
location/{
root html;
index index.html index.htm;

刚才说过,location可以理解成url的一部分,那么当我们访问“http://172.25.0.1/"这个url时,这个url的最后一个“/“其实就是上图中的“location/",换句话说就是,上图中红线标记出的"/“其实对应的就"http://172.25.0.1/"这个url的最后一个"/",这个location块中有两条配置指令,它们分别是root和index root配置指令的意思是:当前location所对应的文档根目录是哪里,"root html;"表示当前location的文档根目录是html目录,那么“文档根目录“又是什么意思呢?说白了,文档根目录的意思就是当有人访问"/“这个路径时,去服务器的哪个目录中找对应的资源

举个例子
如果我在html目录中放了一张图片,图片名为a.jpg,那么我就能通过"http://172.25.0.1/a.jpg“访问到这张图片,url中的"/“对应了“location/"配置段,而“location/"又对应到了服务器的html目录,所以,url中的“/“就与服务器的html目录建立了对应关系,当我们访问"http://172.25.0.1/a.jpg"这个地址时,其实访问的是服务器上html目录中的a.jpg

再换句话说,html目录就是当前location的资源目录。注意,上例中的html路径是一个相对路径,表示nginx安装目录中的html目录,因为我将nginx安装到了/usr/local/nginx目录中,所以上例中的html目录的绝对路径就是"/usr/local/nginx/html/"

发布了102 篇原创文章 · 获赞 14 · 访问量 2395

猜你喜欢

转载自blog.csdn.net/qq_41871875/article/details/104500589