nginx installation and getting started

Reference notes:

Install and configure Nginx using yum under CentOS 7

Nginx Getting Started Guide

How to install nginx

Install nginx using yum or up2date

1. First check the version number of Linux distribution

cat /etc/redhat-release 

My system version is:

CentOS Linux release 7.5.1804 (Core)

2.Nginx is not in the default Yum source. First add nginx to the yum source

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

The following words indicate that there is no problem:

obtain http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
 Warning:/var/tmp/rpm-tmp.a7nthc: head V4 RSA/SHA1 Signature, secret key ID 7bd9bf62: NOKEY
 In preparation...                          ################################# [100%]
Upgrading/install...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]

3. Run yum repolist to check and find that Nginx has been installed on this computer

yum repolist

4. Install nginx (wait slowly, it will take some time)

yum install nginx

After successful installation:

#After successful installation
 already installed:
  nginx.x86_64 1:1.18.0-1.el7.ngx                                               

complete!

nginx simple configuration

1. Check Nginx version

nginx -v

2. Start Nginx service

systemctl start nginx

3. Visit Nginx service (visit locally and you can see Welcome to nginx)

curl -i localhost

4. Remote access via public IP (ip:80)

nginx source package installation

Download address

After downloading to / NGX / inus, download to / local

  • Dependent library installation

    • 1. Install the gcc environment - nginx relies on the gcc environment when compiling
    yum -y install gcc gcc-c++
    
    • 2. Install pcre - enable nginx to support rewriting
    yum -y install pcre pcre-devel
    
    • 3. Install zlib - zlib library provides many ways to compress and decompress. nginx uses zlib to gzip compress the contents of http package
    yum -y install zlib zlib-devel
    
    • 4. Install openssl - Secure Socket Layer password library for communication encryption
    yum -y install openssl openssl-devel
    
  • decompression

tar -zxvf  nginx-1.18.0.tar.gz 
  • Enter nginx-1.18.0 directory to compile and install the source code

    Check the platform installation environment: - prefix=/usr/local/nginx is the directory where nginx is compiled and installed (recommended). After installation, relevant files will be generated in this directory
    If the previous dependent libraries are installed successfully, execute/ The configure --prefix=/usr/local/nginx command will display some environment information. If an error occurs, it is generally because the dependent library has not been installed. You can install the missing dependent library according to the error prompt.

./configure --prefix=/usr/local/nginx
  • Compile the source code and install nginx (execute make and install if no error occurs)
make && make install
  • nginx service operation command

    • Start service
    /usr/local/nginx/sbin/nginx
    
    • Reload service
    /usr/local/nginx/sbin/nginx -s reload
    
    • Out of Service
    /usr/local/nginx/sbin/nginx -s stop
    
    • View nginx service process
    ps -ef | grep nginx
    

nginx simple introduction

What is nginx?

Nginx is a lightweight Web server and reverse proxy server. It is widely used in Internet projects because of its low memory consumption, fast startup, high concurrency and strong ability.

What's the use of niginx? (four functions)

  • Dynamic and static separation

In fact, the dynamic and static separation is that the Nginx server divides the received requests into dynamic requests and static requests

The static request directly fetches the corresponding resources from the root directory path set by the nginx server, and the dynamic request is forwarded to the real background for processing

This can not only reduce the pressure on the application server and serve the background api interface, but also separate the front and back-end code for parallel development and deployment

For example:

server {  
        listen       8080;        
        server_name  localhost;

        location / {
            root   html; # Nginx default
            index  index.html index.htm;
        }
        
        # Static configuration. All static requests are forwarded to nginx for processing. The storage directory is my project
        location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css)$ {
            root /usr/local/var/www/my-project; # The root directory to which the static request is proxied
        }
        
        # If the dynamic request matches the one with path 'node', it will be forwarded to port 8002 for processing
        location /node/ {  
            proxy_pass http://localhost:8002; #  Act as service agent
        }
}
  • Reverse proxy

In fact, the reverse agent is similar to you going to the purchasing agent to help you buy things (the browser or other terminals ask nginx). You don't care where he goes to buy, as long as he helps you buy what you want

OK (the browser or other terminals finally get the content they want, but it doesn't know where to get it)

Configure reverse proxy:

server {  
        listen       8080;        
        server_name  localhost;

        location / {
            root   html; # Nginx default
            index  index.html index.htm;
        }
        
        proxy_pass http://localhost:8000; #  Reverse proxy configuration, the request will be forwarded to port 8000
}

  • load balancing

With the continuous growth of business and the increasing number of users, one service can no longer meet the requirements of the system. At this time, the server appears colony.

In the server cluster, Nginx can distribute the received client requests "evenly" (strictly speaking, it is not necessarily uniform, and the weight can be set) to all services in the cluster

On the radiator. This is called load balancing.

Configure load balancing

# Load balancing: setting domain
upstream domain {
    server localhost:8000;
    server localhost:8001;
}
server {  
        listen       8080;        
        server_name  localhost;

        location / {
            # root   html; # Nginx default
            # index  index.html index.htm;
            
            proxy_pass http://domain; #  In load balancing configuration, requests will be evenly distributed to ports 8000 and 8001
            proxy_set_header Host $host:$server_port;
        }
}
  • Forward proxy

Forward proxy is the opposite of reverse. Take the example of purchasing on behalf of others in the above example. Many people find a purchasing agent to buy the same commodity, and the purchasing agent buys it at one time after finding the store to buy it. this

In this process, the shopkeeper doesn't know that purchasing is to help others buy things. So for many customers who want to buy goods, he acts as a positive agent.

nginx delete

When one day you no longer love nginx, how will you delete it

  • Uninstall nginx using yum or up2date
[root@VM_0_4_centos local]# yum remove nginx

Loaded plugins: fastestmirror, langpacks
Resolving Dependencies

......

Removed:
  nginx.x86_64 1:1.18.0-1.el7.ngx                                                          
Complete!
  • Check whether nginx still exists
[root@VM_0_4_centos ~]# find / -name nginx
[root@VM_0_4_centos ~]# 

Keywords: Java CentOS Nginx Back-end

Added by nerya on Wed, 09 Feb 2022 23:26:12 +0200