1.1 installation

1.1.1 Preparations for Installation

The Nginx installation requires the determination of several libraries associated with the Linux installation. Otherwise, the configuration and compilation will be incorrect.

  1. Whether the GCC compiler is installed

    Check for installation:

    yum list installed | grep gcc
    Copy the code

    Perform installation:

    yum install gcc -y
    Copy the code
  2. Check whether the OpenSSL library is installed

    Check for installation:

    yum list installed | grep openssl
    Copy the code

    Perform installation:

    yum install openssl openssl-devel -y
    Copy the code
  3. Check whether the PCRE library is installed

    Check for installation:

    yum list installed | grep pcre
    Copy the code

    Perform installation:

    yum install pcre pcre-devel -y
    Copy the code
  4. Whether the Zlib library is installed

    Check for installation:

    yum list installed | grep zlib
    Copy the code

    Perform installation:

    yum install zlib zlib-devel -y
    Copy the code
  5. To install the software once, run the following command
    yum install gcc openssl openssl-devel pcre pcre-devdl zlib zlib-devel -y 
    Copy the code

1.1.2 Official Installation

Decompress the downloaded nginx file and run the tar -zxvf nginx-1.14.2.tar.gz command

Switch to the decompressed nginx home directory and run the CD nginx-1.14.2 command

Run the./configure –prefix=/usr/loacl/nginx command in nginx-1.14.2

(where –prefix specifies the nginx installation path) Note: No Spaces around the equals sign

Run the make command to compile the file

Run the make install command to install

After the installation is complete, you can switch to the /usr/local/nginx directory to view the contents

Nginx contains the following files: conf (nginx configuration file), HTML (web page address), logs (nginx startup file), sbin (nginx startup file);Copy the code

1.2 start

View the Nginx installation path

where is nginx
Copy the code

1.2.1 Common Startup

Switch to the sbin directory of the nginx installation directory and run./nginx

Step 1: CD sbin/ Step 2: PWD Step 3:./nginx Run the ps -ef | grep nginx process command to check whether the nginx process contains the master process and worker processCopy the code

1.2.2 Starting from the Configuration File

./nginx -c /usr/local/nginx/conf/nginx.conf
Copy the code
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
Copy the code

-c specifies the configuration file and the configuration file path must be an absolute path

1.2.3 Checking whether Nginx is started

Run the ps -ef | grep nginx command to query the nginx process

The Nginx architecture consists of the master process and its worker

The master process reads the configuration file and maintains the worker process, which actually handles the requests

After Nginx starts, some TMP files will appear in the installation directory. These files are temporary files.

1.3 shut down

1.3.1 Gracefully closing Nginx

Run ps -ef | grep nginx to find the nginx process number

Run the kill -quit active Pid command

Note: 1. Pid is the master process PID (PID) of the master process number, and other are worker process pid (pid) of child processes.

2. This type of shutdown will be closed after the request is processed, so it is called an elegant shutdownCopy the code

1.3.2 Quickly Closing Nginx

Find the nginx process number: ps – ef | grep nginx

Run the kill -term active PID command

> Note: 1. Pid is the master process ID, and other worker processes are worker processes. This method of closing, whether the request is completed or not, is more violent, called fast closingCopy the code

1.3.3 restart Nginx

./nginx -s reload
Copy the code

1.4 Checking the Configuration

After modifying the Nginx configuration file, you can run the Nginx command to check the syntax of the configuration file.Copy the code
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -t
Copy the code

1.5 other

On Linux view nginx version: / usr/local/nginx/sbin/nginx – V

-v (lowercase v) Displays the nginx version

-v (uppercase V) displays the nginx version, compiler version, and configuration parameters

2.1 Nginx core configuration file

To learn about Nginx, you need to be familiar with its core configuration file, named nginx.conf, which is located in the Nginx installation directory /usr/local/nginx/conf.

For details, see nginx configuration in the resources directory. Conf.

2.1.1 Basic Configuration

Configure the worker process running user. Nobody is also a Linux user and is used to start applications without a password

#user nobody;

Configure the number of worker_processes, which is usually equal to or twice the number of cpus. Worker_processes 1

[debug | info | notice | warn | error | crit]. The default value is error error_log logs/error.log.

#error_log logs/error.log notice;

#error_log logs/error.log info;

Pid logs/nginx.pid # Configure the process PID file

2.1.2 events configuration

> Configure work mode and link book > >events {> worker_connections 1024; Worker_processes * worker_connections specifies the maximum number of connections per worker process. >} > ` ` `Copy the code

2. Main applications of Nginx

  • Static Site deployment
  • Load balancing
  • Static agent
  • Dynamic and static separation
  • Virtual host

3. Static website deployment

Nginx is an HTTP Web server that can send static files (such as HTML and images) back to the browser client through THE HTTP protocol.

 location / {
            root   html;
            index  index.html index.htm;
        }
        
  location /ace {
            root   /opt/www;  
            index  index.html index.htm;
        }
Copy the code

Root indicates that the root path of the project is (IP + port), and / = /opt/ WWW. “/ ace” = “/ opt/WWW/ace”

3.1 Nginx.conf Configuration file example

Column -1 (including local Api interface forwarding)

Nginx. Conf path: / usr/Nginx/conf/Nginx. Conf

#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' #  '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; {upstream API server 127.0.0.1:9091; } server { listen 9092; server_name x.xxxxx.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root /home/web/pcms_PC; #index index.html index.htm; } location /bim { alias /home/web/pcms_BIM; #index index.html index.htm; } location /api { proxy_pass http://api/; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php${# proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php${# root HTML; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:! aNULL:! MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # #}}}Copy the code

4. Practical operation steps (according to Column -1)

Step 1: Configure and modify nginx.conf

cd /usr/Nginx/conf
Copy the code
vim nginx.conf
Copy the code

Wq save and exit, and restart Nginx

Conf:./nginx -t:./nginx -s reloadCopy the code

Step 2: Check whether the firewall is enabled for the port used in Nginx

Firewall -cmd --query-port=80/ TCP Firewall -cmd --query-port=80/ TCP Firewall-cmd --zone=public --add-port=80/ TCP --permanent Step 3 restart the firewall: firewall-cmd --reload (Check which ports are enabled: Firewall - CMD - list - port)Copy the code

Step 3: Open the browser and enter the corresponding IP address and port number.

5. Load balancing

5.1 Overview of Load Balancing

In the early days of the web, we generally used a single machine to provide centralized services externally. With the increase of business volume, one server is not enough for us, so we will form several machines into a cluster to provide services externally. However, there is usually only one access to our website, such as www.web.com. So how do you distribute your requests to different machines in the cluster when a user accesses www.web.com in the browser? That's what load balancing does. Load balancing usually refers to the "even" distribution of requests across multiple server nodes in a cluster, which in this case means that they are nearly even over a relatively large statistical range, but not completely so.Copy the code

5.2 Implementation mode of load Balancing