In this document, the system environment is Centos. 7 In this document, the access domain name is nginx.bb.local

The characteristics of

Low memory usage, high performance, and high concurrency

To be able to achieve

Reverse proxy load balancing static/static separation high availability

The installation

Add yum source

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Copy the code

Install nginx

yum -y install nginx
Copy the code

Set the nginx service to start upon startup

systemctl enable nginx
Copy the code

Common commands

#Check for formatting errors in configuration files
nginx -t

#View the current status of the nginx service
systemctl status nginx

#Stop the nginx service
systemctl stop nginx

#Start the nginx service
systemctl start nginx

#Restart the nginx service
systemctl restart nginx
Copy the code

The configuration syntax

  1. Each instruction begins with; At the end, instructions and arguments are separated by a space symbol
  2. An instruction block groups multiple instructions together in {} braces
  3. The include statement allows you to combine multiple configuration files to improve maintainability
  4. Use # signs to add comments to improve readability
  5. Get variables using the $sign
  6. The parameters of some instructions support regular expressions

Load balancing

When the traffic volume increases and the server cannot cope with it, improving hardware performance is only a temporary solution. The most effective method is to deploy multiple servers to share the load balancing.

Load balancing configuration examples:

upstream demo {
  server 127.0.0.1:3000;
  server 127.0.0.1:3001;
}

server {
  listen 80;
  server_name nginx.bb.local;

  location / {
    proxy_passhttp://demo; }}Copy the code

The reverse proxy

  1. Forward proxy The ladder we commonly refer to is a forward proxy server, which accesses resources through a server that can access a specified network. At this point, the client is aware of the proxy operation.

  2. Reverse proxy reverse proxy, in fact, the client’s agent is no perception, because the client does not require any configuration can access, we only need to send the request to the reverse proxy server, the reverse proxy server to select the target server to get data, returned to the client, the reverse proxy server and the target server is a server, The proxy server address is exposed and the real server IP address is hidden.

Reverse proxy configuration example:

location /oss {
  proxy_pass http://game-brew.oss-cn-hangzhou.aliyuncs.com/;
}
Copy the code

The above example, on a visit to nginx. Bb. Local/oss/directory of resources will be forwarded to the game-brew.oss-cn-hangzhou.aliyuncs.com/ is as follows: nginx. Bb. Local/oss/head. The jp… Game-brew.oss-cn-hangzhou.aliyuncs.com/head.jpg both url to access to the head. The JPG, this picture can be solved using images in Canvas cross-domain problem of resources

Dynamic and static separation

Static resources are handed over to Nginx to provide access directly, reducing the pressure on dynamic content servers to handle them.

Static resource server configuration example:

location ~^/(images|stylesheets|download|javascript) {
root /usr/file;
autoindex on;
}
Copy the code

The above code will return the files under the _usr_FILE folder if the access path starts with images, stylesheets, Download, and javascript.

Set the CORS

In addition to reverse proxy, you can also configure CORS to allow the front end to cross domains when certain conditions are met.

add_header 'Access-Control-Allow-Origin' 'http://demo.bb.local';
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS, PUT, DELETE';
add_header 'Access-Control-Allow-Headers' 'Content-Type, token';

if ($request_method = 'OPTIONS') {
return 200;
}
Copy the code

Open the gzip

gzip on; # open
gzip_min_length 1; # less than 1 byte is no longer compressed
gzip_comp_level 2; # Compression level
gzip_types text/plain application/x-javascript text/css application/xml application/javascript text/javascript applic
ation/x-httpd-php image/jpeg image/gif image/png;
Copy the code

High availability

This is an advanced feature that I haven’t learned yet.