Getting started with Nginx for Linux

Posted by TRemmie on Sat, 01 Jan 2022 03:19:32 +0100

Nginx overview

1. Nginx is an open source, high-performance and reliable http web service and proxy service

2. Open source: directly obtain the source code

3. High performance: support mass development

4. Reliable: stable service

Nginx features

1. High performance and high concurrency

nginx supports high concurrency. nginx is faster than other web services when dealing with a large number of concurrency

2. Light weight and high expansion

2.1 light weight

There are few functional modules, only the core module is retained, and other codes are modular (easy to read, convenient for secondary development, and very friendly to developers)

2.2. High scalability

What modules do you need to install? You do not need to install all modules, and you also support third-party modules

3. High reliability

3.1. As long as it is not excessive, there will be almost no problems

3.2. Other web services need to be restarted at regular intervals, but nginx doesn't

3.3. The downtime of nginx is 99999

4. Support hot deployment

nginx can update iterations and deploy code during re running

5. Most companies are using Nginx

5.1. Nginx technology is mature and has the functions most commonly used and needed by enterprises

5.2. It is suitable for the current mainstream architecture trend, including micro service, Cloud Architecture and middle tier

5.3. Unify the technology stack to reduce the maintenance cost and technology update cost

6. Nginx uses Epool network model

6.1. Select: when a user initiates a request, the select model will perform a traversal scan, resulting in low performance.

6.2 epoll: when a user initiates a request, the epoll model will process it directly, which is efficient and has no connection restrictions.

7. Nginx application scenario

Other WEB services

1,apache: httpd,Earliest used web Service, low performance, difficult to operate
2,nginx
    tengine: Tengine It was launched by Taobao Web Server project. It's in Nginx On the basis of, many advanced functions and features are added to meet the needs of high-volume websites
    openresty-nginx: OpenResty Is based on Nginx And Lua High performance Web Platform, which integrates a large number of excellent Lua Libraries, third-party modules, and most dependencies. It is used to easily build dynamic systems that can handle ultra-high concurrency and high scalability Web Application Web Service and dynamic gateway.
3,IIS: windows Lower web service
4,lighttpd: Is a German led open source Web The fundamental purpose of server software is to provide a safe, fast, compatible and flexible platform for high-performance websites Web Server environment With very low memory overhead, CPU Low occupancy, good efficiency, and rich modules.
5,GWS: google web server
6,BWS: baidu web server
 
7,Tomcat
8,Resin
9,weblogic
10,Jboss

Deploy Nginx

1. Download

Official website: https://nginx.org/
Software: https://nginx.org/download/

2. yum installation

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[root@web01 ~]# yum install nginx -y
[root@web01 ~]# systemctl stop httpd
[root@web01 ~]# systemctl start nginx

3. Compilation and installation

[root@web01 ~]#  wget https://nginx.org/download/nginx-1.20.2.tar.gz
[root@web01 ~]# tar -xf nginx-1.20.2.tar.gz
[root@web01 nginx-1.20.2]# ./configure
[root@web01 nginx-1.20.2]# make
[root@web01 nginx-1.20.2]# make install

Add Nginx modules smoothly (only by compiling and installing)

Added modules must be recompiled.
[root@web01 ~]# tar -xf nginx-1.20.2.tar.gz
[root@web01 ~]# cd nginx-1.20.2
[root@web01 nginx-1.20.2]#./configure  --with-http_ssl_module
[root@web01 nginx-1.20.2]#make 
[root@web01 nginx-1.20.2]#make install 

If an error is reported, install it directly according to the missing file

 

Nginx command

1,-v : Print version number
[root@web01 ~]# nginx -v
nginx version: nginx/1.20.2

2,-V :  Print version number and configuration item
[root@web01 ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx 

3,-t :  Check configuration file
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4,-T :  Test the configuration file and start

5,-q : Print error log

6,-s : Operation process
    stop    : stop it
    quit    : sign out
    reopen    : restart
    reload    : heavy load
7,-p :  appoint nginx Working directory for
8,-e : Specify the error log path
9,-c : Specifies the path to the configuration file
10,-g : Set a global Nginx Configuration item
[root@web01 ~]# nginx -g 'daemon off;'

Nginx profile

Nginx is divided into global configuration and module configuration

All configuration

1,user :  appoint Nginx Startup user for
    2,worker_processes :  definition Nginx of worker Number of processes
        auto === CPU quantity
    3,error_log :  Error log path
    4,pid :  pid Storage file path
    5,events :  Module configuration
        5.1,worker_connections : every last worker How many requests can the process access at the same time
        5.2,use : appoint Nginx Network model of
    6,http :  web Service module
        6.1,include :  Load external configuration items
        6.2,default_type :  If the file type cannot be found, the specified default type is used
        6.3,log_format :  Define log format
            log_format json '{"@timestamp":"$time_iso8601",'
                  '"host":"$server_addr",'
                  '"service":"nginxTest",'
                  '"trace":"$upstream_http_ctx_transaction_id",'
                  '"log":"log",'
                  '"clientip":"$remote_addr",'
                  '"remote_user":"$remote_user",'
                  '"request":"$request",'
                  '"http_user_agent":"$http_user_agent",'
                  '"size":$body_bytes_sent,'
                  '"responsetime":$request_time,'
                  '"upstreamtime":"$upstream_response_time",'
                  '"upstreamhost":"$upstream_addr",'
                  '"http_host":"$host",'
                  '"url":"$uri",'
                  '"domain":"$host",'
                  '"xff":"$http_x_forwarded_for",'
                  '"referer":"$http_referer",'
                  '"status":"$status"}';
            access_log /var/log/nginx/access.log json ;
        6.4,sendfile :  Efficient read file
        6.5,keepalive_timeout :  Long connection
            HTTP 1.0 Short link
            HTTP 1.1 Long connection
        6.6,server :  Website module
            6.6.1,listen :  Listening port
            6.6.2,server_name :  Define domain name
            6.6.3,location :  Access path
                6.6.3.1,root : Specify URL path
                6.6.3.2,index :  Specifies the index file for the web address

Super Mary and chess games deployment

 

 

 

Steps

1,Upload code

2,Edit profile
[root@web01 conf.d]# vim /etc/nginx/conf.d/game.conf 
server {
    listen 80;
    server_name game.test.com;
    location / {
        root /opt/Super_Marie;
        index index.html;
    }
}

3,Test whether the configuration file is normal
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4,restart Nginx
[root@web01 conf.d]# systemctl restart nginx 

5,Domain name resolution
C:\Windows\System32\drivers\etc\hosts
172.16.1.7 game.test.com