nginx提供静态内容-阅读官方文档

官方网址:Beginner’s Guidehttp://nginx.org/en/docs/beginners_guide.html#static

 翻译部分:Serving Static Content

# An important web server task is serving out files (such as images or static HTML pages).

 web服务器 的一个重要任务是提供文件(例如 照片或者静态html文件)

# You will implement an example where, depending on the request, files will be served from different local directories: /data/www (which may contain HTML files) and /data/images (containing images). 

接下来你将会实现一个实例,根据请求,从不同的本地目录中提供请求所需的文件

# This will require editing of the configuration file and setting up of a server block inside the http block with two location blocks.

这将要求你修改nginx配置文件,在http块中设置带有两个location块的sever块

人话:xx是路径匹配规则,根据自己的情况修改即可

http {
    server {
        location xx {}
        location xx {}
    }
}

# First, create the /data/www directory and put an index.html file with any text content into it and create the /data/images directory and place some images in it.

首先,创建目录/data/www并在该目录放置一个含有任意内容的index.html文件,然后,创建目录/data/images并在该目录下放置一些图片。

# Next, open the configuration file. 

接下来,打开nginx配置文件:nginx.conf

nginx提供静态内容-阅读官方文档_第1张图片

# The default configuration file already includes several examples of the server block, mostly commented out.

安装nginx生成的默认的配置文件已经包含了一些server块的示例,大部分被注释掉了。

nginx提供静态内容-阅读官方文档_第2张图片

nginx提供静态内容-阅读官方文档_第3张图片

 # For now comment out all such blocks and start a new server block:

http {
    server {
    }
}

现在,注释掉所有此类的块,写一个新的server块

# Generally, the configuration file may include several server blocks distinguished by ports on which they listen to and by server names.  

一般来说,配置文件包括几个server块,这些server块通过它们监听的端口和它们的name来区别开

# Once nginx decides which server processes a request, it tests the URI specified in the request’s header against the parameters of the location directives defined inside the server block.

一旦nginx要决定由那个server来处理请求,它会根据服务器块中定义的location 指令的参数测试请求头中指定的URI

# Add the following location block to the server block:

location / {
    root /data/www;
}

在server块中添加上面的location块

# This location block specifies the “/” prefix compared with the URI from the request. 

这个location块指定和请求的URI做比较的前缀是/

#  For matching requests, the URI will be added to the path specified in the root directive, that is, to /data/www, to form the path to the requested file on the local file system.

匹配到URI后,URI会被添加到root指令指定的路径上,即,加到/data/www后,以便生成要请求的文件在本地文件系统上的路径。

比如:请求/min.js文件,nginx会在本地文件系统的路径/data/www/min.js找该文件

# If there are several matching location blocks nginx selects the one with the longest prefix.

如果请求的URI匹配到了多个location块,nginx会选择前缀最长的那个location块来匹配

例如location / {} 和 location /min/ {},请求URI为/min/index.js,nginx会匹配到/min/而不是/

# The location block above provides the shortest prefix, of length one, and so only if all other location blocks fail to provide a match, this block will be used.

上面的location块(location / {})有最短的前缀/,长度为1,所以只有当其他所有location块都匹配失败后,才会匹配到它(location / {})

# Next, add the second location block:

location /images/ {
    root /data;
}

接下来,添加第二个location块

# It will be a match for requests starting with /images/ (location / also matches such requests, but has shorter prefix).

location /images/ {} 会匹配到以/images/开头的请求URI(location / 也匹配此类请求,但/相比/images/更短,所以/败给了/images)

# The resulting configuration of the server block should look like this:

server {
    location / {
        root /data/www;
    }

    location /images/ {
        root /data;
    }
}

最后的nginx配置是应该是这样的

# This is already a working configuration of a server that listens on the standard port 80 and is accessible on the local machine at http://localhost/.

这已经是 监听80端口的服务的一个有效的配置,可以通过http://localhost/来访问本地机器

# In response to requests with URIs starting with /images/, the server will send files from the /data/images directory.

为响应URI以/images/开头的请求,服务器会从/data/images目录发送文件

# For example, in response to the http://localhost/images/example.png request nginx will send the /data/images/example.png file.

例如,为了响应http://localhost/images/example.png请求,nginx会发送/data/images/example.png文件。

# If such file does not exist, nginx will send a response indicating the 404 error.

如果/data/images/example.png文件不存在,nginx发送一个指示404错误的响应。

# Requests with URIs not starting with /images/ will be mapped onto the /data/www directory.

URI不以/images/开头的请求会被映射到/data/www目录

例如/min/index.js---> /data/www/min/index.js

# For example, in response to the http://localhost/some/example.html request nginx will send the /data/www/some/example.html file.

例如,为了响应http://localhost/some/example.html请求,nginx会发送 /data/www/some/example.html文件

# To apply the new configuration, start nginx if it is not yet started or send the reload signal to the nginx’s master process, by executing:

nginx -s reload

 为了应用修改过的配置文件,如果nginx没启动则需要启动

 

如果nginx已经启动则需要发送nginx -s reload给nginx主进程来重启

 

# In case something does not work as expected, you may try to find out the reason in access.log and error.log files in the directory /usr/local/nginx/logs or /var/log/nginx.

万一nginx没有按照预期正常工作,你需要通过查看/usr/local/nginx/logs目录或者/var/log/nginx目录下的access.log和error.log文件来找出原因。

你可能感兴趣的:(nginx,运维)