As a Java programmer, you don't know Nginx yet? Come and study!

Nginx

After watching the crazy video of station B, Xiaobian benefited a lot and wrote down this article on the way to study
Small partners can also get the complete video of crazy God from station B!
Home page of crazy God B station

1 Why use Nginx?

When our company's project was just launched, the concurrency was small and users used less. Therefore, in the case of low concurrency, it was enough to start the application with a jar package, and then the internal tomcat returned the content to users.


But slowly, more and more users use our platform, and the concurrency increases slowly. At this time, one server can't meet our needs.

So we expanded horizontally and added servers. At this time, several projects are started on different servers. If users want to access, they need to add a proxy server to help us forward and process requests through the proxy server.

We hope that this proxy server can help us receive users' requests, and then forward users' requests to different server nodes according to rules. The user is not aware of this process. The user does not know which server returned the result. We also hope that he can provide different weight choices according to the performance of the server. Ensure the best experience! So we used Nginx

2 what is Nginx

Nginx (engine x) is a high-performance HTTP and reverse proxy web server. It also provides IMAP/POP3/SMTP services. Nginx is Rambler.ru, the second most visited site in Russia by Igor sesoyev (Russian: Рамблер) Developed, the first public version 0.1.0 was released on October 4, 2004. nginx 1.0.4 was released on June 1, 2011.

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., with 12.18% usage rate in the global active website, about 22 million 200 thousand websites.

Nginx is a service with very simple installation, concise configuration files (perl syntax is also supported) and very few bugs. Nginx is very easy to start, and can run almost 7 * 24 continuously, even if it runs for several months without restarting. You can also upgrade the software version without interruption.

The Nginx code is completely written from scratch in C. The official data test shows that it can support the response of up to 50000 concurrent connections

3 function

Reverse proxy

Http proxy, reverse proxy: as one of the most commonly used functions of web server, especially reverse proxy

Forward proxy (VPN)

Reverse proxy

load balancing

There are two kinds of load balancing strategies provided by Nginx: built-in strategy and extension strategy. The built-in strategies are polling, weighted polling and Ip hash. The extension strategy is unrestrained. There are only things you can't think of and can't do.

  • polling

  • Weighted polling

  • IPHash

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

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-DurB8Gk9-1631336740170)(E: \ learning file \ JAVA learning \ learning record \ front end \ imgs\image-20210908104556840-1631069159829.png)]

Dynamic and static separation

Dynamic and static separation. In our software development, some requests need background processing, and some requests do not need background processing (such as css, html, jpg, js, etc.) , these files that do not need background processing are called static files. Let the dynamic web 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 to improve the speed of resource response.

4 Nginx installation

Windows

You can download it directly from the official website, stable version

We recommend using DOS installation so that you can see that it is too fast after all

Note that the file path cannot contain Chinese, and the permissions must be sufficient

Linux

Download installation package

Upload to server

#decompression
tar -zxvf nginx.tar.gz
#Enter the extracted folder for automatic environment configuration
./configure
make
make install
#Query location
whereis nginx
#start-up
cd /usr/local/nginx/sbin
nginx

5 common commands

cd /usr/local/nginx/sbin/

./nginx  start-up

./nginx -s stop  stop it
./nginx -s quit  Safe exit

./nginx -s reload  Reload profile#We need to reload after modifying the configuration file

ps aux|grep nginx  see nginx process

linux Firewall command

# open
service firewalld start
# restart
service firewalld restart
# close
service firewalld stop
# View firewall rules
firewall-cmd --list-all
# Query whether the port is open
firewall-cmd --query-port=8080/tcp
# Open port 80
firewall-cmd --permanent --add-port=80/tcp
# Remove port
firewall-cmd --permanent --remove-port=8080/tcp

#Restart the firewall (restart the firewall after modifying the configuration)
firewall-cmd --reload

# Parameter interpretation
1,firwall-cmd: yes Linux Actions provided firewall A tool for;
2,--permanent: Indicates that it is set to persistent;
3,--add-port: Identify the added port;

6 basic configuration

#user  nobody;
worker_processes  1;
#load balancing 
events {
    worker_connections  1024;
}

#Reverse proxy
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

#Configure proxy services
    upstream zheng{
        server 127.0.0.1:8080
        server 127.0.0.1:8081
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
            #Acting
            proxy_pass http://zheng;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
########### Each instruction must end with a semicolon.#################
#user administrator administrators;  #Configure users or groups. The default is nobody.
#worker_processes 2;  #The number of processes allowed to be generated. The default is 1
#pid /nginx/pid/nginx.pid;   #Specify the storage address of nginx process running files
error_log log/error.log debug;  #Set the log path and level. This setting can be put into the global block, http block and server block. The level is: debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #Set the network connection serialization to prevent group panic. The default is on
    multi_accept on;  #Set whether a process accepts multiple network connections at the same time. The default is off
    #use epoll;      #Event driven model, select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #The maximum number of connections is 512 by default
}
http {
    include       mime.types;   #File extension and file type mapping table
    default_type  application/octet-stream; #The default file type is text/plain
    #access_log off; #Cancel service log    
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #Custom format
    access_log log/access.log myFormat;  #combined is the default value for log format
    sendfile on;   #sendfile mode is allowed to transfer files. The default is off. It can be in http block, server block and location block.
    sendfile_max_chunk 100k;  #The number of transfers per call of each process cannot be greater than the set value. The default value is 0, that is, there is no upper limit.
    keepalive_timeout 65;  #The connection timeout, which is 75s by default, can be set in http, server and location blocks.

    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #Hot standby
    }
    error_page 404 https://www.baidu.com; # error page
    server {
        keepalive_requests 120; #Maximum number of single connection requests.
        listen       4545;   #Listening port
        server_name  127.0.0.1;   #Listening address       
        location  ~*^.+$ {       #Request url filtering, regular matching, ~ is case sensitive, ~ * is case insensitive.
           #root path;  #root directory
           #index vv.txt;  #Set default page
           proxy_pass  http://Mysvr; # request to go to the list of servers defined by mysvr
           deny 127.0.0.1;  #Rejected ip
           allow 172.18.5.54; #Allowed ip           
        } 
    }
}

Specific depth, (individual) will not be considered temporarily

Tags: Java Operation & Maintenance Nginx

Posted on Sat, 20 Nov 2021 01:17:38 -0500 by disaster77