nginx[4] Build an available static resource Web server

Posted by tarleton on Mon, 07 Feb 2022 18:14:16 +0100

1. Download dlib

dlib

$ wget http://dlib.net/files/dlib-19.23.zip
$ unzip dlib-19.23.zip
$ rm -rf dlib-19.23.zip
$ ls -l
 Total dosage 0
drwx------ 2 nobody root   6 2 July 21:54 client_body_temp
drwxr-xr-x 2 root   root 333 2 July 21:53 conf
drwxrwxr-x 7 root   root 226 1 February 2511:16 dlib-19.23
drwx------ 2 nobody root   6 2 July 21:54 fastcgi_temp
drwxr-xr-x 2 root   root  40 2 July 12:11 html
drwxr-xr-x 2 root   root  82 2 July 22:01 logs
drwx------ 2 nobody root   6 2 July 21:54 proxy_temp
drwxr-xr-x 2 root   root  19 2 July 12:11 sbin
drwx------ 2 nobody root   6 2 July 21:54 scgi_temp
drwx------ 2 nobody root   6 2 July 21:54 uwsgi_temp

$ mv dlib-19.23/docs dlib

2. Custom Configuration

$ vim conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       8080;
        server_name  localhost;
        access_log  logs/host.access.log  main;
        location / {
             alias  dlib/;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

  • The alias parameter keeps the nginx url path one to the path of the dlib directory

3. Start

./sbin/nginx -s reload

Interface

4. Modules

4.1 gzip compression

F12 Open Network Snap Pack, Refresh

The size of the first page transfer is 26.6KB and the file size is the same

$ ls -l dlib/index.html
-rwxr-xr-x 1 root root 26341 2 July 23:42 dlib/index.html

We can zip and transfer all the files and unzip them

$ cat conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
    gzip_min_length 1;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php
        image/jpeg image/gif image/png;
    server {
        listen       8080;
        server_name  localhost;
        access_log  logs/host.access.log  main;
        location / {
             alias  dlib/;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

restart

./sbin/nginx -s reload

Interface view, the first page transfer size is only 8KB

The gzip method we see from the response header

4.2 audoindex directory display

http://nginx.org/en/docs/http/ngx_http_autoindex_module.html
The ngx_http_autoindex_module inserts a snippet module here to handle requests that end with a slash character ('/') and to generate a directory list. ngx_http_autoindex_module is usually used when ngx_ Http_ Index_ When the module cannot find the index file, it passes the request to the module.

  location / {
             alias  dlib/;
             autoindex  on;
        }

Effect

4.3 limit_rate limits traffic access

limit_rate Introduction
Trial scenario: When some customers access large files, it takes up a lot of traffic, but other customers access small files normally, so speed limits are needed.

location / {
     alias  dlib/;
     autoindex  on;
     limit_rate 1k;
}

After clearing the cache, see the effect

Former 54ms, now 7s, is much slower, but it takes into account the patience of most customers.

4.4 log_format log format

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';
  • main is named for the log format, why should it be named, to customize the log for different domain names, or to do some special analysis and processing for large files, url s, etc.
  • remote_addr remote address
  • remote_user remote user
  • time_local Time
  • ...
server{
......
        access_log  logs/host.access.log  main;
......

The parameter configuration is to record what type of log a domain name service chooses to log and in what format to output to which directory file. Here is access_log type log, output to logs/host in main format. Access. Log, main has a defined configuration under the http block.

$ cat logs/host.access.log
192.168.211.1 - - [08/Feb/2022:00:28:10 +0800] "GET /dlib-logo.png HTTP/1.1" 200 5742 "http://192.168.211.100:8080/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36" "-"
192.168.211.1 - - [08/Feb/2022:00:28:11 +0800] "GET /dlib-icon.ico HTTP/1.1" 200 1150 "http://192.168.211.100:8080/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36" "-"
192.168.211.1 - - [08/Feb/2022:00:28:12 +0800] "GET /dlib-icon.ico HTTP/1.1" 200 1150 "http://192.168.211.100:8080/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36" "-"

Of course, we actually add a lot of variables to the main format, such as Ngx_ Http_ Core_ Variables of module $connection, $content_of Length..., and the third-party module ngx_that you just used Http_ Gzip_ Module, which has a variable $gzip_ratio, the compression ratio of gzip.

Topics: Front-end Nginx server