< required course for architect > - nginx

Environment introduction: Ubantu 20.04 nginx 18.1 If nginx is not installed on your ubantu, use the following command...
2.1 forward agency and reverse agency
2.2 load balancing
2.3 dynamic and static separation
4.1 configuration file location
4.2 composition of configuration file
5.1 simplest direction agent
5.2 distributing different URLs to different servers
6.1 polling
6.2 weight
6.3 ip_hash
6.4 fair
7.1 prepare static resources
7.2 modifying configuration files


Environment introduction:
Ubantu 20.04
nginx 18.1

If nginx is not installed on your ubantu, use the following command to install it:

sudo apt install nginx

Wait a moment and check if the installation is successful

zjf@ubuntu:~/Desktop$ nginx -v nginx version: nginx/1.18.0 (Ubuntu) # The output version number indicates that the installation is successful zjf@ubuntu:~/Desktop$ systemctl status nginx ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset:> Active: active (running) since Sun 2021-10-03 19:22:12 CST; 49min ago Docs: man:nginx(8) Main PID: 6749 (nginx) Tasks: 3 (limit: 9443) Memory: 3.7M CGroup: /system.slice/nginx.service ├─6749 nginx: master process /usr/sbin/nginx -g daemon on; master_> ├─6750 nginx: worker process └─6751 nginx: worker process
1. Introduction to nginx

nginx is a web server written in C language. It occupies less memory and has high concurrency and high reliability. The common functions are: reverse proxy; Dynamic and static separation; load balancing

2. Overview of main functions of nginx

2.1 forward agency and reverse agency

2.1.1 forward agency

Configure a proxy server on the client to communicate with the actual server through the proxy server

2.1.2 reverse proxy

The client is not aware of the proxy. The client can access it without any configuration. The request is sent to the reverse proxy server. The reverse proxy server selects the target server to obtain data and then returns it to the client. At this time, the reverse proxy server and the target server are one server, the proxy server is exposed and the real server is hidden.

2.2 load balancing

When a single server reaches the performance bottleneck, multiple servers need to be added. As the name suggests, load balancing is to distribute requests equally to different servers.

2.3 dynamic and static separation

In order to speed up the parsing speed of the website, dynamic pages and static pages are parsed by different servers to speed up the parsing speed and reduce the pressure of a single server

3 nginx common commands

Switch to the / usr/sbin directory. You can use the nginx command

zjf@ubuntu:~/Desktop$ whereis nginx nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz zjf@ubuntu:~/Desktop$ cd /usr/sbin/

View version number

zjf@ubuntu:/usr/sbin$ ./nginx -v nginx version: nginx/1.18.0 (Ubuntu)

Stop nginx

zjf@ubuntu:/usr/sbin$ ./nginx -s stop

start nginx

zjf@ubuntu:/usr/sbin$ ./nginx

Reload profile

zjf@ubuntu:/usr/sbin$ ./nginx -s reload

If the permission is not enough, add sudo before the instruction.
This is not the only way to operate nginx. You can use nginx through docker or operate nginx services through systemctl

Check configuration file

root@ubuntu:/usr/sbin# nginx -tc /etc/nginx/nginx.conf nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

Use specified profile

nginx -c /etc/nginx/nginx.conf

Restart with the specified profile

nginx -s reload -c /etc/nginx/nginx.conf
4 nginx configuration file

4.1 configuration file location

The nginx configuration file is in the / etc/nginx directory

zjf@ubuntu:~$ whereis nginx nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz zjf@ubuntu:~$ cd /etc/nginx zjf@ubuntu:/etc/nginx$ ls conf.d koi-win nginx.conf sites-enabled fastcgi.conf mime.types proxy_params snippets fastcgi_params modules-available scgi_params uwsgi_params koi-utf modules-enabled sites-available win-utf

4.2 composition of configuration file

4.2.1 global block

Set the configuration instructions that affect the overall operation of the nginx server, mainly including configuring the users (groups) running the nginx server, the number of worker process es allowed to be generated, the process PID storage path, log storage path and type, and the introduction of configuration files.

user www-data; worker_processes auto; # The larger the value of nginx concurrent processing, the more concurrent processing pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf;

4.2.2 events block

The instructions involved in the events block mainly affect the network link between nginx and users

events { worker_connections 768; # Number of links allowed per process # multi_accept on; }

4.2.3 http block

This is the most frequently configured part of nginx. Most functions such as proxy, cache and log definitions and the configuration of third-party modules are here. http block also includes http global block and server block

4.2.3.1 http global block

http {
include mime_type;
}

4.2.3.2 server block 4.2.3.2.1 global server
sever { listen 80;# The current listening port is 80 server_name localhost; # Host name }
4.2.3.2.2 location
location / { # When a slash appears in the requested path, the following jump appears. You can include a value in the path and perform other jumps root html index index.html index.htm }
5. Nginx reverse proxy configuration practice

5.1 simplest direction agent

When the browser visits 192.168.153.128:81, the request will be redirected to Baidu home page

http{ # http global block - other content server { listen 81; server_name 192.168.153.128; location ~/* { proxy_pass http://www.baidu.com; } } # http other content }

5.2 distributing different URLs to different servers

Taobao sent the request to www.taobao.com
Baidu sends the request to www.baidu.com

server { listen 81; server_name 192.168.153.128; location ~/baidu/* { proxy_pass http://www.baidu.com; } location ~/taobao/* { proxy_pass http://www.taobao.com; } }
6 nginx load balancing configuration practice

Four commonly used strategies for nginx load balancing are polling (default strategy), weight and IP_ Hash, fair (response time mode)

Prepare two services before configuring. If you are developing in java, it is recommended to start two tomcat services. This document uses python web services and flash + gunicorn as services

First, prepare two flash programs

app1.py

from flask import Flask app = Flask(__name__) @app.route('/') def index(): return "Hello gunicorn web 1; open 5000 port"

The second flash program

app2.py

from flask import Flask app = Flask(__name__) @app.route('/') def index(): return "Hello gunicorn web 2; open 5001 port"

Running services using gunicorn

gunicorn -w 2 -b 0.0.0.0:5000 -D app1:app gunicorn -w 2 -b 0.0.0.0:5001 -D app2:app

Visit ip:5000 and ip:5001 through the browser to see the defined return value

6.1 polling

upstream myserver { server 192.168.153.128:5000; server 192.168.153.128:5001; } server { listen 81; server_name 192.168.153.128; location ~/* { proxy_pass http://myserver; } }

Accessing 192.168.153.128:81 will proxy to port 5000 and port 5001 in turn

6.2 weight

upstream myserver { server 192.168.153.128:5000 weight=5; server 192.168.153.128:5001 weight=10; } server { listen 81; server_name 192.168.153.128; location ~/* { proxy_pass http://myserver; } }

Access 192.168.153.128:81 proxy to port 5001 on port 5000. On average, port 5001 is accessed twice and port 5000 is accessed once

6.3 ip_hash

upstream myserver { ip_hash; server 192.168.153.128:5000; server 192.168.153.128:5001; } server { listen 81; server_name 192.168.153.128; location ~/* { proxy_pass http://myserver; } }

The problem of session sharing can be solved by accessing different servers according to the accessed ip

6.4 fair

upstream myserver { server 192.168.153.128:5000; server 192.168.153.128:5001; fair; } server { listen 81; server_name 192.168.153.128; location ~/* { proxy_pass http://myserver; } }
7 nginx dynamic static separation configuration practice

7.1 prepare static resources

Create an image folder in the / home/zjf/Desktop/test directory to put some pictures

7.2 modifying configuration files

server { listen 81; server_name 192.168.153.128; location /image/ { root /home/zjf/Desktop/test/; # Point to static directory autoindex on; # Show files in folder } }


Dynamic and static separation is realized

8 nginx configuring high availability clusters

Two servers are required
Each server needs to be kept alive
yum command: yum install kept - y

8 nginx principle

nginx has a master process and a worker process

root@ubuntu:/home/zjf/Desktop/test# ps -ef |grep nginx root 6385 1501 0 08:58 ? 00:00:00 nginx: master process nginx -c /etc/nginx/nginx.conf www-data 11000 6385 0 10:15 ? 00:00:00 nginx: worker process

The master is responsible for management and monitoring. A master manages multiple workers. Clients send requests to the master first. Multiple workers use the contention strategy to obtain and process requests. Each worker is a separate process

4 October 2021, 22:00 | Views: 3773

Add new comment

For adding a comment, please log in
or create account

0 comments