Java is really not difficult (50) Getting Started and Using Nginx

In this article, we will learn Nginx with very few texts and illustrations, really easy to get started with Nginx!

1. What is Nginx?

Nginx: High-performance HTTP and reverse proxy web server
Features: less memory, strong concurrency (Baidu, JD.com, Sina, Netease, Tencent, Taobao are all in use)
Detailed introduction can be read:https://lnmp.org/nginx.html


Second, the role of Nginx

1. Forward proxy

Forward proxy (client’s proxy)
We know that in China, you can’t directly access the external network. You need to connect to a ladder (VPN). (Hong Kong can directly access the external network). We first access the server in Hong Kong in the mainland, and then send the server in Hong Kong to access the foreign server. The returned data is also the same way. This is the forward proxy.
insert image description here

Summary: Proxy tools (accelerators, circumvention tools) on the client side are called forward proxy (proxy client)


2. Reverse proxy

reverse proxy (proxy for server)

We know that it is impossible for a large website or project to have only one server when it is deployed. For example, Baidu has servers in Beijing, Shanghai, and Guangzhou, but we only need to enter Baidu’s unified URL to access it, but we don’t know what is being accessed. Wherever the server can be used, it is equivalent to using the server without perception.
insert image description here
Summary: Enter a fixed URL, and the agent will assign the accessed server according to the configuration


3. Load balancing

Load balancing is actually easy to understand. It is allocated according to the carrying capacity of each server. The built-in strategy is round robin, weighted round robin, and IP Hash:

Polling: It’s easy to understand. Each server takes turns to perform
weighted polling: add a weight value to each server. The higher the value, the stronger the carrying capacity and the more tasks.
For example, the weight value of server A is 3, and that of server B is 2. , C is 1, then A can undertake 50% of all tasks, B can undertake 35%, and C can undertake 15%
IP Hash: Through IP Hash calculation, the access of this IP will only hit the fixed server in the future


4. Separation of static and dynamic

To speed up the parsing speed of the website, dynamic pages and static pages can be parsed by different servers to speed up the parsing speed. Reduce the pressure on the original single server,
insert image description here
that is, load some static resources and load them directly on Nginx


3. Installation of Nginx

1. Windows version installation

Official website:http://nginx.org/en/download.html

Just download the version of your own operating system. Currently, it is recommended to download the stable version of 1.18
insert image description here

After the download is complete, unzip it

nginx.conf in the conf directory is a configuration file:
insert image description here
start nginx:

It is not recommended to double-click nginx.exe, because it will pass by in a flash. Use cmd directly in the directory, and enter nginx.exe to start.
If there is an error:
insert image description here
it means that port 80 is already occupied, just open the configuration file and modify the port number. :
insert image description here
If there is no error after restarting, enter in the browser: http://localhost:81, the interface will appear and the startup will be successful:
insert image description here


2. Linux version installation

Follow the steps above to download a Linux version of the Nginx package. After downloading, use the Xftp 7 tool to transfer it to Linux.
First, make sure that Nginx is not installed on Linux. You can use the command: whereis nginx
Use the command to decompress the compressed package:
insert image description here
Decompression is complete, enter nginx-1.18.0 Directory:
insert image description here
You can see that these files are the same as the Windows version

Then start the installation, instruction: ./configure
After executing the direct instruction: make
one more:make install

Enter the command: whereis iginxYou can view the installation location:
insert image description here
Enter the directory:
sbin is executed to
insert image description here
start Nginx:
insert image description here

If there is no information prompt, the startup is successful, or you can enter the Linux address in the Windows browser, and enter 80 for the port:
insert image description here
Done!

Note: If you cannot enter the above page after installing the Linux version, you can check whether the Linux firewall is enabled.
If you use a remote server (Alibaba Cloud, Tencent Cloud), you need to release port 80 of the security group

View open ports: firewall-cmd --list-all
Set open port numbers:firewall-cmd --add-service=http –permanent, firewall-cmd --add-port=80/tcp --permanent

Restart the firewall:firewall-cmd –reload


Four, Nginx common commands:

Start: ./nginx
Stop: ./nginx -s stop
Safe exit: ./nginx-s quit
Reload configuration file: ./nginx -s reload
View nginx process:ps auxigrep nginx


5. Configuration file

There are many configuration files in Nginx, but they are basically paragraphs beginning with #. You can sort them out and get the following concise content:

<!--第一块,全局生效-->
worker_processes  1;

<!--第二块,在events部分中生效-->
events {
    worker_connections  1024;
}

<!--第三块,以下指令在http部分中生效-->
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

This configuration file consists of three parts, namely the global block, events block and http block. For a
detailed explanation of the configuration file, please refer to this article: Detailed explanation of Nginx configuration file


insert image description here

Guess you like

Origin blog.csdn.net/m0_57310550/article/details/127053127