Today, let’s take a small test-use Nginx to build a static resource web server, and share the whole process with you.
As for what is Nginx? Nginx installation and configuration, so I won't go into too much detail, don't talk nonsense, go straight to the topic.
Preliminary preparation
Prepare a web static resource (I used the webgl example that I learned before), let's take a look at how to configure nginx first:
First configure the listen command to monitor port 8000. Then configure a location instruction block, / means all requests, and then configure the route after / to be consistent with the path in the request directory. At this time, there are two ways to use it, alias is one of them.
listen: Before a request enters nginx, it needs to listen on the port first so that nginx establishes a tcp link with the client. The listen command is used to listen to the port, which is in the server command block. Through the listening port and address, you can determine which server to use to process the request.
The listen command value is mainly divided into three types:
- address[:port]-Listen to an address or add the corresponding port. For example, listen 127.0.0.1:8000
- port-listen on a port. Such as listen 8000
- unix:path-monitor a unix socket address, only used for local communication. For example, unix:/var/run/nginx.sock
Then use the command to reload nginxnginx -s reload, then you can see the effect of root and alias by visiting localhost:8000: the main functions of these two instructions are to map the URL to the file path to return the static file content. The main differences are:
Root has a default value html, which can appear in the http, server, and location instruction blocks, and will map the complete URL into the file path. alias has no default value and can only appear in the location instruction block, and only after location The url is mapped to the file path for example:
location /root {
root html
}
location /alias {
alias html
}
When accessing localhost/root/, according to the configured command value, /root will be added after html to access index.html. So the actual routing address is localhost/html/root/index.html. This is because root maps the complete URL into the file path.
When accessing localhost/alias/, the routing address accessed is localhost/html/index.html.
Use gzip compression to reduce network transmission
When the accessed resources are large, a large amount of broadband is often consumed, and the loading time is also increased. Nginx can set the compression function of static resources.
gzip on
#表示打开gzip功能开关
gzip_min_length 1
#表示gzip最小压缩字节大小,如果一个文件很小,在一个tcp报文就能发送出来,这时再进行压缩效果不太,却又消耗cpu。(我这是为了演示才设置为1)
gzip_comp_level 2
#表示压缩级别
gzip_types
#表示只对列出来的类型进行压缩
Only 830kb after compression, and it can be seen in the response header that gzip compression is used
Use autoindex
Use autoindex to share a directory information with users, and users can open the corresponding directory according to their needs.
It is introduced in the document that when the URL at the end of / is accessed, it will correspond to this directory and display the structure of this directory. The method of use is to set autoindex to on when accessing the css/ folder:
What needs to be mentioned here is that there will be some situations: After autoindex is turned on, the directory structure is still not returned. It may be because the index command is configured, the priority of the index command will be greater than that of the autoindex command. details as follows:
index:当访问/时会返回index指令的文件内容。index file,默认值是index.html,可以出现在http、server和location指令块中。
autoindex:当url以/结尾时,尝试以html/xml/json等格式返回root/alias中指向目录的目录结构
Limit access speed
Because the bandwidth of the public network is limited, when many users access at the same time, they have an enhanced relationship. At this time, it may be necessary for users to limit the access speed when accessing some large files to ensure that there is enough bandwidth so that other users can access some basic files such as css and js. At this time, you can set the set command with some built-in variables to achieve this function. For example
set $limit_rate 1k;
Limit the speed at which the server can send responses to the browser. The variable $limit_rate can be found in Embedded Variables in the ngx_http_core_module module on the official website
The usage is to add a number in space after the variable to indicate how many bytes are transmitted per second. After adding the restriction, you will find that the access speed has changed.
Record access log
The content of the log depends on the format to be set. Use the log_format directive to define the format of the log.
The log\_format format allows you to set a name, which can record log files in different formats for different purposes. As shown in the figure, it is set to the log format named main. This format uses many built-in variables:
$remote_addr:表示远端的ip地址,也就是浏览器的ip地址
$remote_user:表示用户名提供基本身份验证
$time_local:表示访问时间
$request:完整的原始请求行
$status:表示响应状态
$body_bytes_sent:发送给客户端的body字节数
$http_referer:表示从哪跳转过来
$http_user_agent:用户浏览器的类别,版本以及操作系统的一些信息
$http_x_forwarded_for:客户端请求头中的"X-Forwarded-For"
After setting log_format, go to the place where the log record is set. Use the access_log command.
In which server block the access_log is located, it means that the log of this type of request is recorded in the place where access_log is set;
server {
...
access_log logs/access.log main;
}
Indicates that the request of this server is recorded in the access.log file of logs, using the record format of main
The above is the whole process of using Nginx to build a static WEB resource server. It is very simple, but very practical.
The content is from: 160b1d86aed05d https://blog.csdn.net/hugo233/article/details/117092673
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。