Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

background

Nginx can be used as a static resource server, a reverse proxy service, and, in microservices, as a gateway, with Lua for traffic limiting, fuses, and so on. We usually use it as a static resource server or HTTP request forwarder, which directly returns static file requests to static file resources and forwards dynamic requests to spoolers such as Tomcat. It has the following characteristics:

  • Good performance, consumes fewer system resources, and supports more concurrent connections
  • Hot deployment
  • Good scalability and reliability
  • It is open source

Now look at how to play with an Nginx configuration.

Environment set up

This article is based on the Window environment.

1.1 Download Address

Nginx download address

1.2 Basic Commands

The following script commands are executed in the nginx installation directory, for example, D:t\nginx-1.19.10

  1. Start the
Start nginx # or nginxCopy the code
  1. stop
-s stop # or nginx -s quitCopy the code
  1. Reload the
nginx -s reload
Copy the code

When you fail to start nginx, the logs folder in the nginx directory will generate the corresponding error log file. If the nginx startup port is used, you can change the default application startup port in the configuration file nginx-1.19.10\conf\nginx.conf.

location

Location / {} # if the URI is "/", return to the bottom of the HTML folder. Location = / {# specify the URI to access the directory root HTML; # httpIndex specifies the file name of the default document index index.html index.html. } # / HTML/static/img/location for all images file ~ *. (PNG | JPG | jpeg | GIF) ${root/HTML/static/img /} # here will access/WWW/root/HTML/resources directory location ^~ /conf/ { alias /www/root/html/ } } }Copy the code

The above configuration uses root and alias. The differences between the two are as follows:

  1. Alias can only be in the location block, while root can be in the HTTP /server/location/if configuration blocks
  2. Alias stands for alias and specifies the specific path of the URI request, while root is the parent directory of the specified URI path
  3. When using alias, use slash (/) after the directory name; otherwise, files may not be found

Gzip compression

HTTP {# enable/disable gzip module gzip on; # set the minimum bytes for compressed pages to gzip_min_length 1000; # set the system to obtain several units of cache for storing gzip result data streams gzip_buffers 4 8k; # gzip_comp_level 6; # gzip_level 6; # gzip_level 6; # Identify HTTP protocol versions in case older clients may not support gzip self-extracting gzip_http_version 1.1; Gzip_types text/plain Application /x-javascript text/ CSS text/ HTML application/ XML; gzip_types text/plain Application /x-javascript text/ CSS text/ HTML application/ XML }Copy the code

Add:

  1. Gzip scopes include HTTP,server,location, and so on
  2. Binary resource files do not need to be compressed, because the low compression rate is not cost-effective compared to the CPU resource consumption

Log on

HTTP {# print log format, Use the nginx built-in variable log_format main '$remote_ADDR - $remote_user [$time_local] "$request" "$status $body_bytes_SENT "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; Access_log logs/access.log main; }Copy the code

Nginx variables are added to facilitate subsequent configuration, these variables and nginx core module.

  1. $remote_ADDR: indicates the CLIENT IP address
  2. $remote_user: user name used for HTTP basic authentication services
  3. $time_local: access time
  4. $request: indicates the URL of the get request
  5. $request_body: the request body of the client
  6. $status: request status code
  7. $body_bytes_sent: Request page size (byte)
  8. $http_referer: Source page, referer in request header
  9. $http_user_agent: Browser information, version, type, etc

Of course, there are many more variables, which are not listed here.

conclusion

Should the front end understand Nginx? Load balancing? This is a controversial topic, but the above three basic introductory configurations are already able to see what clients can do when making requests to Nginx in reverse proxy servers. Resource cache configuration, reverse proxy configuration, the next update…

References:

www.nginx.cn/doc/standar…

www.nginx.cn/doc/standar…