Preface: learn the video notes of UP master crazy God in station B Video link
brief introduction
What is Nginx
Nginx (enginex) is a high-performance HTP and reverse proxy web server. It also provides IMAP/POP3/SMTP services. Nginx was developed by Igor sesoyev for Ramberru site (in Russian: PaM6nep), the second most visited site in Russia. The first public version 0.1.0 was released on October 4E, 2004. On June 1, 2011, nginx 1.0.4 was released.
Its characteristics are less memory and concurrent capability. In fact, nginx's concurrent capability is better in the same type of web server. Chinese mainland users use Baidu website: Baidu, Jingdong, Sina, NetEase, Tencent, Taobao, etc. There are 12.18% of the active websites in the world, about 22.2 million websites.
Ngink is a service with very simple installation, very concise configuration files (and support per syntax) and very few bugs. Nginx is very easy to start, and can almost run 7 * 24 uninterrupted. It doesn't need to be restarted even if it runs for several months. You can also upgrade the software version without interruption of service.
Nginx code is written entirely from scratch in C language. Official data tests show that it can support responses with up to 50000 concurrent connections.
The role of Nginx
Before you learn about Nginx, you should first learn about forward proxies
Forward proxy
A proxy client is a forward proxy
For example, VPN. After users connect to VPN, the VPN server may be in Hong Kong. Users first request VPN and forward it to foreign servers
Reverse proxy
A reverse proxy is a proxy server
For users, the same server is accessed, and the expansion of the server is not perceived by users
Summary:
The forward proxy is the proxy client, and the reverse proxy is the proxy server
Nginx load balancing
There are two load balancing strategies provided by Nginx: built-in strategy and expansion strategy. The built-in policies are polling, weighted polling and lp hash. The expansion strategy is unrestrained. There is only what you can't think of, and there is nothing he can't do.
polling
Polling is rotating request servers
weight
By setting the weight of the server, you can find a large number of requests to the server with high weight
ipsash
iphash hashes the ip requested by the client, and then distributes the request of the same client ip to the same server for processing according to the hash result, which can solve the problem of session non sharing.
Dynamic and static separation. In our software development, some requests need to be processed in the background, and some requests do not need to be processed in the background (such as:. css, html.jpg
js and so on), these files that do not need background processing are called static files. Let the dynamic pages in the dynamic website distinguish the constant resources from the frequently changing resources according to certain rules. After the dynamic and static resources are split, we can cache them according to the characteristics of static resources. Improve resource response speed.
Nginx installation
Install under windows
Download nginx from the official website: Official website address
After downloading, unzip it
From conf / nginx As can be seen from the conf configuration file, nginx listens to port 80 by default
Double click nginx Exe start nginx
See that there are two default processes of nginx in the background
Test it by typing localhost:80 in the browser
Installation under Linux
Download the liunx version from the official website
Upload and install to liunx server
# Unzip the installation package tar -zxvf niginx-1.18.0.tar.gz # Enter folder cd nginx-1.18.0 # Execution profile ./configure # make to install make # make confirm installation make install # View nginx installation directory whereis nginx # nginx: /usr/bin/nginx # Enter the installation directory cd /usr/bin/nginx # Execute start nginx ./nginx
Nginx common commands
1,Start: C:\server\nginx-1.0.2>start nginx or C:\server\nginx-1.0.2>nginx.exe 2,stop it: C:\server\nginx-1.0.2>nginx.exe -s stop or C:\server\nginx-1.0.2>nginx.exe -s quit Note: stop It's a quick stop nginx,Relevant information may not be saved; quit Is a complete and orderly stop nginx,And save relevant information. 3,Reload Nginx: C:\server\nginx-1.0.2>nginx.exe -s reload Use this command when the configuration information is modified and it is necessary to reload these configurations. 4,Reopen log file: C:\server\nginx-1.0.2>nginx.exe -s reopen 5,see Nginx edition: C:\server\nginx-1.0.2>nginx -v
Nginx actual combat
Start a SpringBoot project locally
Port number: 9090
Now let's configure Nginx
Locate the conf configuration file for Nginx
First analyze a wave of configuration files
# Global configuration, user process information, etc #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; # Maximum number of connections, listening events, etc events { worker_connections 1024; } # http configuration http { # Global configuration of http include mime.types; default_type application/octet-stream; #Set allow cross domain add_header Access-Control-Allow-Origin *; #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; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; # server can configure different services server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # HTTPS service configuration #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
It looks like a lot. Let's simplify it
# Global configuration # Maximum number of connections, listening events, etc events { worker_connections 1024; } # Http configuration http { # http configuration upstream xxxx{ # Load balancing configuration # Server resources } server { listen 80; server_name localhost; # agent # WWW.kuangstudy.com location / { # xxxx } # WWW.kuangstudy.com/admin location /admin { # xxxx } }
Configure services
Reload after changing the configuration file
Test use
When we access port 80, we successfully jump to the back-end server