nginx installation under centos7

Posted Jun 15, 20205 min read

What is nginx

Nginx is a high-performance HTTP and reverse proxy web server, and also provides IMAP/POP3/SMTP services
Nginx features low memory occupation and strong concurrency
Generally speaking, if we introduce Nginx in our project, our project architecture might look like this:
image
In this architecture, the role represented by Nginx is called a load balancing server or a reverse proxy server. All requests first arrive on Nginx, and then Nginx forwards the request sent by the client to a certain one according to the forwarding rules configured in advance. Tomcat up
So here are two concepts involved:

  • Load balancing server

It is to forward requests and reduce the pressure on a certain server. There are many load balancing strategies and there are many layers. For some large websites, load balancing is basically started from DNS. Load balancing is divided into hardware and software, and their respective representatives are F5 and Nginx(Nginx has been acquired by F5). , You can also use Apache to do load balancing, but the efficiency is not as good as Nginx, so the mainstream solution is now Nginx.

  • Reverse proxy server

It is to the user that the user only determines that I am requesting a uri resource at a certain location, but who provided this resource to me, the user does not need to know. Just like we call 110 on a daily basis, you will have such a question, the whole country calls 110, so if I call 110 in Beijing, will I call Shanghai? Haha, this is actually similar to a reverse proxy. We only need to know that we call 110, but the real process is that the network adds the prefix number of the local base station before the number we dialed by default.

Nginx advantages

In Java development, Nginx is very widely used, just give a few points:

  1. Use Nginx as a static resource server:The resources in Java can be divided into dynamic and static. The dynamic needs to be parsed by Tomcat before it can be returned to the browser, such as the JSP page, Freemarker page, and the JSON data returned by the controller. As a dynamic resource, the dynamic resource is processed by Tomcat, and the speed will inevitably decrease. For static resources, such as images, HTML, JS, CSS and other resources, such resources do not have to be parsed by Tomcat. When the client requests these resources, the resources are returned to the client in between. At this time, you can use Nginx to build a static resource server and return the static resources directly to the client.
  2. Use Nginx as a load balancing server, whether using Dubbo or Spirng Cloud, in addition to using their own load balancing strategy, you can also use Nginx as a load balancing server.
  3. Support high concurrency, low memory consumption, low cost, simple configuration, stable operation, etc.

Nginx installation

Because our daily servers are selected centos7, so here I will only introduce the installation of nginx under centos, we use the default installation(that is, do not actively set the installation directory)

  1. Download nginx

wget http://nginx.org/download/nginx-1.17.0.tar.gz
image

  1. Unzip

tar -zxvf nginx-1.17.0.tar.gz
image

  1. Then enter the nginx-1.17.0 directory

Before compiling and installing, we also need to install two dependencies
yum -y install pcre-devel
image >
with
yum -y install openssl openssl-devel
image

  1. Compile and install

Run the following commands in sequence:
./configure,
image
make,
image
make install
After installation, the default installation location is:/usr/local/nginx/sbin/nginx
image

  1. Start nginx

image
Enter the sbin directory and start:cd /usr/local/nginx/sbin, ./nginx to start
After the startup is successful, we access the local ip address, you can see the following interface:
image
See the above page, indicating that Nginx has been successfully installed
If you modify the Nginx configuration, you can reload the Nginx configuration file with the following command:./nginx -s reload

Off topic

Everyone knows that now our projects generally adopt the front-end and back-end separation development methods, and the front-end and back-end separation projects are deployed in only two ways:

  1. One is to package and compile the front-end project and put it in the back-end project(for example, under the src/main/resources/static directory of the Spring Boot project)
  2. The other is to deploy the static resources after the front end is packaged with Nginx. The separate deployment of the back end only requires a simple interface.

Generally in company projects, we use the latter more
During deployment, we only need to package our project into a jar package, then upload it to the server, and then execute the command to start the project:nohup java -jar demo.jar> demo.log &(demo.jar is us Packaged package name, this is just an example, you can replace it with your package name)
In this way, the running log of this project is written to the demo.log file


This article was first published from https://latin-xiao-mao.github.io and https://latin-xiao-mao.gitlab.io and [ https://www.cnblogs.com/latin-xiao-ao/] ( https://www.cnblogs.com/latin-xiao- ao/) Welcome to visit

image