Nginx is a very useful tool for Nginx, and it is a very useful tool for Nginx

As developers, WE all know the importance of Nginx. Nginx is a high-performance HTTP and reverse proxy Web server developed by Igor Sessoyev of Russia, the first version was released on October 4, 2004.

Nginx features are:

  • Less memory
  • Strong concurrency capability (can support about 50000 concurrent connections)
  • Super simple configuration
  • The bug is very little
  • Super easy to install
  • Service is very stable (does not need to restart for months)

Based on these features, more and more sites are using Nginx. As a result, mastering Nginx has become an essential skill for developers.

This article has been included in the “Java Programmers advanced path” column “Nginx”, like this column friends can go to click a star to pay attention to. Go for 1000 stars!

Github.com/itwanger/to…

What Nginx does

Reverse proxy is one of the most common features of Nginx as a Web server. What is a reverse proxy? Many beginners are bound to have a lot of question marks when they first encounter this noun.

If you want to understand what a reverse proxy is, you have to understand what a forward proxy is.

For example, erector’s browser cannot directly access Google, but the proxy server in Hong Kong can access Google, so erector accesses the proxy server in Hong Kong and indirectly accesses Google. This proxy server is also a forward proxy.

A forward proxy is a proxy client that allows you to access the destination server.

In contrast, a reverse proxy is a proxy server, allowing a large number of requests to evenly access a single server.

For example, if 100,000 mistress are accessing itwanger.com at the same time, if there is only one server, it is easy to break down. Therefore, in the case of high concurrency, there will be many servers (let’s say 10) to take this job. How can 100,000 mistress access these 10 servers?

This requires A reverse proxy server, and the reverse proxy server lets 10,000 other servers access server A, 10,000 other servers access server B, and one other server access server C, so the pressure on each server is reduced. Isn’t that nice?

Here comes the question. Each server may have different capabilities. For example, server A has A larger memory of 100 GIGABytes. Server B has a smaller memory with 10 gigabytes; Server C has less memory, only one GIGAByte. How do you get every server to do what it can?

Nginx has polling and weighted polling built in for load balancing purposes. Server A niubi will increase its weight A little, let 50,000 small two access it; If server B is weaker, the weight will be smaller, so that 20,000 people will access it. Server C is weaker, so it has the least weight, so let 10,000 little twos access it.

In addition, Nginx has another awesome feature called static and static separation.

In our software development, some requests need to be processed in the background; Some requests do not need background processing, such as CSS, JS file requests, these files do not need to go through background processing is called static files.

According to some rules, we can separate dynamic resources from static resources, and then separate requests through Nginx. Static resource requests do not need to be processed by the Web server, thus improving the overall resource response speed.

Nginx installation

The installation of Nginx varies for different operating systems. For Windows, go to the official website to download the zip green installation package and decompress it.

Nginx.org/en/download…

Before taking everyone white ticket ali cloud server, Linux server, directly through the pagoda panel this artifact can be installed.

However, if you select phpMyadmin when installing the pagodas panel kit, you will overwrite port 80 and you will not be able to access the Nginx startup page directly from the default configuration.

I’ll use the macOS environment as an example to demonstrate this.

First, check whether nginx is installed using the brew info nginx command.

Second, install nginx using the brew install nginx command.

It can be concluded from the above information:

  • The root directory is/usr/local/var/www
  • The configuration file is/usr/local/etc/nginx/nginx.conf
  • The default port is 8080

Step 3: Start nginx with the nginx command.

Step 4: In the browser address bar, go to Localhost :8080 and you will see the following welcome page.

Nginx common commands

In general, once Nginx is started, we rarely let it exit, using the reload command the most. When we modify the configuration file, we need to execute a reload command for Nginx to take effect.

Nginx start stop stop nginx nginx - s - s quit security exit nginx -s reload to reload the configuration file ps aux | grep nginx view nginx processCopy the code

Remember, Nginx’s reload is insensitive to the user, which I think is awesome

4. Nginx configuration

Let’s take a look at the configuration structure of Nginx:

│ ├─ Upstream │ ├─ Server │ ├─ Web Server │ ├─ Web Server │ ├─ Web Server │ ├─ Web Server │ ├─ Web Server There can be multiple server │ ├ ─ ─ server │ │ ├ ─ ─ the location # to match the URI (URL is a URI), there can be multiple location │ │ ├ ─ ─ the location │ │ └ ─ ─... │ └ ─ ─... └ ─ ─...Copy the code

Let’s take a look at the default configuration of Nginx. I added a comment so you can easily see what this line of configuration is for.

worker_processes 1; {worker_connections 1024; HTTP {include mime.types; Default_type application/octet-stream; sendfile on; Keepalive_timeout 65; Server {listen 8080; Server_name localhost; Location / {root HTML; # index index.html index.htm; } error_page 500 502 503 504/50x.html; # default 50x location = /50x. HTML {root HTML; } } include servers/*; # load subconfig}Copy the code

Ok, now let’s go to the pagoda panel and try to copy the default server configuration to the Nginx configuration on the Linux server.

Just a quick explanation.

Since port 80 is turned on by default on my server, I have configured the listening port to 80. If you have configured it to any other port, remember to turn it on in the Pagoda panel and the security group of the cloud service.

Root: /home/ WWW/index.html This file is my own custom, look at the contents.

<! DOCTYPE html> <html> <meta http-equiv="Content-Type" content="text/html; <img SRC ="niubi.jpeg" /> </body> </ HTML >Copy the code

Very concise and comprehensive, in short is the second brother, cow force ~

Ok, save the configuration file, and reload Nginx, we can enter the server IP address in the local browser to see the effect.

5, Nginx learning materials

About Nginx load balancing, and static separation, wait until the second brother’s website run up, can be dedicated to a video to show you, a little more intuitive. Getting started with Nginx is pretty simple, but let’s face it, if you want to use Nginx well at work, you still need to work hard.

Here I recommend some good learning materials to you.

1) Crazy god X video introductory tutorial, I personally feel, crazy God introductory tutorial is very comfortable, the speed and content are just right.

www.bilibili.com/video/BV1F5…

Black X programmer Nginx tutorial, 159 lectures in total, is basically a very comprehensive Nginx video tutorial.

www.bilibili.com/video/BV1ov…

3) geek XX Nginx100 talk, talk a bit deeper, involves a lot of principle level things. If your wallet is bigger, you can pay for it. But I heard you can get a free ticket somewhere.

4) Nginx from the beginning to the practice, 10,000 word detailed explanation, graphical version, can go to the nuggets to see this article, the content is basically covered (can see the following table of contents), with the previous video lessons, Nginx is basically stable.

Juejin. Cn/post / 684490…

If these information can be all over, I will call you Nginx little prince, estimated that the company encountered Nginx problems, you must be the one to solve the problem.


This article has been included in GitHub open source column “The Path to Java Programmer advancement”, recently updated a lot of dry products up, so do not have a point of star partners quickly go to a bunch of, after all, star repository is equal to become a better Java programmer.

Github.com/itwanger/to…