Linux environment installation using nginx detailed explanation

01-Nginx installation

  Nginx is a lightweight web server/ reverse proxy server and email (IMAP/POP3) proxy server released under a BSD-like protocol . Developed by Russian programmer Igor Sysoev, it is used by Russia's large portal and search engine Rambler (Russian: Рамблер). Its characteristic is that it occupies less memory and has strong concurrency capability. In fact, the concurrency capability of nginx does perform better among similar web servers. Users of nginx websites in mainland China include: Jingdong , Sina , NetEase , Tencent , Taobao , etc.

nginx installation

# 0. First need to have dependencies
yum install -y gcc pcre-devel zlib-devel
# 1. Download Nginx
http://nginx.org/en/download.html
# 2. Upload Nginx to linux and decompress it
tar -zxvf nginx-1.11.1.tar.gz
# 3. Execute the following command in the decompressed nginx directory: (specify the installation location)
./configure --prefix=/usr/nginx
# 5. After executing the above command, execute the following command:
make && make install
# 6. After the compilation is complete, enter the compilation installation directory /usr/nginx directory to view:
[root@localhost nginx]# ls -l
Total usage 4
drwxr-xr-x. 2 root root 4096 10月 14 21:17 conf
drwxr-xr-x. 2 root root 40 10月 14 21:17 html
drwxr-xr-x. 2 root root 6 10月 14 21:17 logs
drwxr-xr-x. 2 root root 19 10/14 21:17 sbin
# 7. Start nginx, enter the sbin directory of the nginx installation directory and execute:
./nginx
# 8. Visit the browser in windows, you can see the nginx welcome page:
http://10.15.0.8:80/ --Enter your own server address: nginx port number
Note: turn off the network firewall
# 9. Close nginx, enter the sbin directory of the nginx installation directory and execute:
./nginx -s stop
# 10. The nginx configuration file is in the conf directory of the nginx installation directory:
[root@localhost conf]# ls -l
Total dosage 60
-rw-r--r--. 1 root root 2656 10月 14 21:17 nginx.conf
.......
Note: nginx.conf is the configuration file of nginx, you can modify the default configuration of nginx in nginx.conf

02-Nginx load balancing strategy

# 1. Polling
Description: The default policy, each request will be assigned to different backend servers one by one in chronological order
# 2.weight weight
Description: The weight parameter is used to specify the polling probability, the default value of weight is 1; the value of weight is proportional to the access ratio
upstream tomcat-servers {
    server localhost:8080 weight=2;
    server localhost:8081;
    server localhost:8082 backup;
}
Note: 1. The higher the weight, the more requests that need to be processed. 2. This strategy can be used in combination with least_conn and ip_hash, mainly for uneven performance of backend servers
# 3.ip_hash 4%3=1
 Description: Specify the load balancer to allocate based on the client IP. This method ensures that the same client's request is always sent to the same server to ensure the session session. In this way, each visitor visits a fixed backend server, which can solve the problem that sessions cannot cross servers.
 upstream tomcat-servers {
        ip_hash; #Guarantee that each visitor visits a backend server
        server localhost:8080;
        ......
    }
# 4.least_conn
Description: Forward the request to the backend server with fewer connections. The polling algorithm is to forward requests to each backend on average, so that their loads are roughly the same; however, some requests take a long time, which will lead to a higher load on the backend where they are located. In this case, least_conn can achieve better load balancing effect.
upstream tomcat-servers{
    least_conn; #Forward the request to the backend server with fewer connections
    server localhost:8080;
}

03-Nginx implements Tomcat load balancing cluster

# 0. Prepare multiple tomcats
tar -zxvf apache-tomcat-8.5.46.tar.gz #Decompress a new tomcat installation package
mv apache-tomcat-8.5.46 tomcat1 #Change the name to tomcat1
cp -r tomcat1/ tomcat2 #copy a copy
cp -r tomcat1/ tomcat3 #copy a copy
# 1. At this time, there are three servers in the current directory, as follows:
[root@localhost ~]# ls -l
Total usage 12248
-rwxrwxrwx. 1 root root 11623939 10月13 12:25 apache-tomcat-8.5.46.tar.gz
drwxr-xr-x. 9 root root 220 10月 14 21:28 tomcat1
drwxr-xr-x. 9 root root 220 10月 14 21:38 tomcat2
drwxr-xr-x. 9 root root 220 10月 14 21:38 tomcat3
# 2. Modify the tomcat1 port number: (pseudo-distributed)
vim tomcat1/conf/server.xml, command to modify the following content:
a.<Server port="8001" shutdown="SHUTDOWN"> --- close the port
b.<Connector port="8888" protocol="HTTP/1.1" ---http protocol port
connectionTimeout="20000"
redirectPort="8443" />
c.<Connector port="10010" protocol="AJP/1.3" redirectPort="8443" /> ---AJP protocol port
# 3. Modify the tomcat2 port number: (pseudo-distributed)
vim tomcat2/conf/server.xml, command to modify the following content:
a.<Server port="8002" shutdown="SHUTDOWN">
b.<Connector port="8889" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
c.<Connector port="10011" protocol="AJP/1.3" redirectPort="8443" />
# 4. Modify the tomcat3 port number: (pseudo-distributed)
vim tomcat2/conf/server.xml, command to modify the following content:
a.<Server port="8003" shutdown="SHUTDOWN">
b.<Connector port="8890" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
c.<Connector port="10012" protocol="AJP/1.3" redirectPort="8443" />
# 5. Start multiple tomcats:
tomcat1/bin/startup.sh
tomcat2/bin/startup.sh
tomcat3/bin/startup.sh
# 6. Check whether tomcat starts successfully
ps -aux|grep tomcat
# 7. Visit tomcat separately in windows, and you can see the home page means the startup is successful:
http://10.15.0.8:8888/
http://10.15.0.8:8889/
http://10.15.0.8:8890/
Note: This step must turn off the network firewall
# 8. Configure multiple tomcats into the nginx configuration file:
1). Enter the sbin directory of nginx and turn off the nginx service
./nginx -s stop
2). Enter the conf directory, and edit the nginx.conf file
vi nginx.conf
3). Add the following configuration to the server label:
upstream tomcat-servers {
server 192.168.80.130:8090;
server 192.168.80.130:8091;
server 192.168.80.130:8092;
}
4). Comment out the following configuration in the configuration file (in the server configuration)
        location / {
            root   html;
            index  index.html index.htm;
        }
5). Replace location / in the configuration file with the following configuration:
location / {
proxy_pass http://tomcat-servers;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_next_upstream http_502 http_504 error timeout invalid_header;
}
# 9. Enter the nginx installation directory sbin directory to start nginx
./nginx -c /usr/nginx/conf/nginx.conf
# 10. Visit nginx and see one of the tomcat screens:
http://10.15.0.8/

04-nginx.conf configuration file

    4.1 Introduction to nginx.conf

The configuration file of Nginx is usually a text file, usually named nginx.conf , and its content includes the following aspects:
  1. Global configuration: This part contains some global configuration directives, such as the user directive specifies the running user of the Nginx process, the worker_processes directive specifies the number of worker processes to be started by Nginx, and so on.
  2. Events configuration: This part contains some configuration instructions related to event processing. For example, the worker_connections instruction specifies the number of connections that each worker process can handle at the same time. The use instruction can specify the event model, such as use epoll or use kqueue .
  3. HTTP configuration: This part contains all HTTP-related configuration directives. For example, the server directive defines an HTTP server, the location directive defines a request processing location, and the proxy_pass directive can proxy requests to other servers.
Here is an example of a simple Nginx configuration file:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    server {
        listen 80;
        server_name example.com;
        location / {
            root /var/www/example.com;
            index index.html;
        }
        location /api/ {
            proxy_pass http://api.example.com/;
        }
    }
}

    4.2. Common commands

4.2.1. alias : The alias command can map the requested URI to another path in the local file system, so as to achieve the effect of path replacement.

For example, to map http://example.com/resource to /var/www/cdn/resource , the following configuration could be used:
location /resource {
    alias /var/www/cdn/resource;
    index index.html;
}
The above configuration will map the http://example.com/resource request to the /var/www/cdn/resource path of the local file system , and Nginx will automatically search for the index.html file in this directory and return it to the client end. If no index.html file is found, a directory listing is returned.
It should be noted that when using the alias command, you need to specify a complete local path, and the path cannot contain regular expressions. In addition, using the alias directive may affect performance, because Nginx needs to perform a path replacement for each request. If it is just a simple path replacement, it is recommended to use the rewrite command or handle it in the application code.
There is also a similar command root , which can also map the requested URI to a path in the local file system, but it will splice the requested URI and the path specified by root . For example, if the following configuration is used:
location /resource {
    root /var/www/cdn;
    index index.html;
}
Then the http://example.com/resource request will be mapped to the /var/www/cdn/resource path of the local file system . The root command is suitable for simple static file services, but it cannot achieve the effect of path replacement.

4.2.2, autoindex : display directory

First of all, you need to ensure that the access rights of the directory are set correctly so that the Nginx user (usually the nginx user) has the permission to read the directory. Then, in the Nginx configuration file, find the corresponding location block and add the autoindex on command, for example:
server {
    listen 80;
    server_name example.com;
    location / {
        root /var/www/example.com;
        index index.html;
    }
    location /files/ {
        root /var/www/example.com;
        autoindex on;
    }
}
The above configuration specifies the /files/ path to display the directory. When requesting http://example.com/files/ , Nginx will automatically list all the files and subdirectories under the directory and display them on the web page.
You can customize the display format of the directory listing by configuring the autoindex_format directive, for example:
location /files/ {
    root /var/www/example.com;
    autoindex on;
    autoindex_format html;
    autoindex_exact_size off;
    autoindex_localtime on;
}
The configuration above specifies displaying directory listings in HTML format, not displaying exact values ​​for file sizes, displaying local time instead of GMT time, etc.
It should be noted that when displaying a directory, if there is an index.html file in the directory, Nginx will give priority to displaying the content of the file instead of the directory list. You can change the default index file by modifying the index command, for example:
location /files/ {
    root /var/www/example.com;
    autoindex on;
    index index.html index.php;
}
The above configuration will search for index.html and index.php files in the directory. If one of the files is found, the content of the file will be displayed first, otherwise the directory list will be displayed.

4.2.3, proxy_pass : Indicates the address of the reverse proxy, which can forward the client request to the backend service specified by the address for processing.

  It is most common to use the proxy_pass configuration item in the location block, examples are as follows   :   
location /app/ {
  proxy_pass http://localhost:8000/;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
The above configuration will   forward the request containing the /app/ path to   http://localhost:8000/ , and pass the parameters of the request header Host , X-Real-IP , and X-Forwarded-For to the backend service.      
In   the proxy_pass   configuration item, protocols such as HTTP, HTTPS, and FTP can be specified, and the target of the reverse proxy can also be specified through the domain name. For example:
    1. Use HTTPS:
location /app/ {
  proxy_pass https://localhost:8000/;
  ...
}
    2. Use a domain name:
location /app/ {
  proxy_pass http://backend.example.com;
  ...
}
It should be noted that for   the address specified in proxy_pass, if it is a domain name or other address that needs to be resolved, it is necessary to confirm whether the DNS resolution is correct, and it is necessary to consider related security solutions, such as SSL/TLS, etc.   In addition, when using   proxy_pass   , you need to consider the performance tuning of the reverse proxy, such as   optimizing through keepalive , proxy_cache and other configuration items, to avoid the reverse proxy from becoming a system performance bottleneck. 

Continually updated!

Guess you like

Origin blog.csdn.net/weixin_51689532/article/details/129893094