Project deployment (Nginx, reverse proxy, load balancing)

Project deployment



1. Introduction and use of Nginx

1.1 What is nginx?

Nginx is a lightweight Web server, Reverse proxy server and email proxy server.
advantage:

  1. Occupies less memory and has strong concurrency
  2. Nginx is specially developed for performance optimization. In the case of high connection concurrency, it can support up to 50,000 concurrent connections. 3. Nginx supports hot deployment and can upgrade the software version without interruption of service.

1.2 Application scenarios

  1. http server: Nginx is an http service that can independently provide http services. It can be a static web server.
  2. Virtual hosting: Multiple websites can be virtualized on one server. For example, virtual hosts used by personal websites.
  3. Reverse proxy, load balancing: When the website traffic reaches a certain level, when a single server cannot meet the user's request, you need to use multiple server clusters to use nginx as a reverse proxy. And multiple servers can share the load equally, and no one server will be idle due to a high load of one server.

1.3 Nginx installation

Download nginx, official website: http://nginx.org/en/download.html
The version we use is version 1.17.8.
Insert picture description here
Installation environment configuration
1. Because Nginx is written in C language, you need to configure the C language compilation environment (must be installed in a networked state)

Need to install gcc environment.
Execute the command: yum install gcc-c++

2. Third-party development packages, these third-party packages need to be installed before compiling.


  • The http module of PCRE nginx uses pcre to parse regular expressions, so the pcre library needs to be installed on linux

Installation command: yum install -y pcre pcre-devel

  • zlibnginx
    uses zlib to gzip the content of the http package, so you need to install the zlib library on linux.

Installation command: yum install -y zlib zlib-devel

  • openssl
    OpenSSL is a powerful secure socket layer cryptographic library, nginx not only supports http protocol, but also supports https, so you need to install
    openssl library in linux .

Installation command: yum install -y openssl openssl-devel

Steps to install Nginx

  1. Upload the Nginx source code package to the root directory of Linux
    Insert picture description here

  2. Unzip Nginx

tar -xvf nginx-1.17.8.tar
Insert picture description here

  1. Enter the unzipped directory nginx-1.17.8

cd nginx-1.17.8
Insert picture description here
1). Create a temporary directory mkdir /var/temp/nginx/client -p
2). Execute the command configure to generate the Mikefile file
Command: ./configure –prefix
=/usr/local/nginx
–pid -path=/var/run/nginx/nginx.pid
–lock-path=/var/lock/nginx.lock –error
-log-path=/var/log/nginx/error.log
–http-log-path= /var/log/nginx/access.log
–with-http_gzip_static_module
–http-client-body-temp-path=/var/temp/nginx/client
–http-proxy-temp-path=/var/temp/nginx/proxy
--Http-fastcgi-temp-path=/var/temp/nginx/fastcgi
–http-uwsgi-temp-path=/var/temp/nginx/uwsgi
–http-scgi-temp-path=/var/temp/nginx/ After scgi
Insert picture description here
executes the command, a MakeFile file is generated.
Insert picture description here
Execute the make command to compile: make
install: make install
Insert picture description here

3. Start and access Nginx
1) Enter the nginx installation directory

cd /usr/local/nginx/
Insert picture description here

2) Enter the sbin directory and execute the nginx command
Insert picture description here

./nginx
start./nginx -s stop shutdown./nginx
-s reload restart
ps aux | grep nginx view process

Access through the browser, the default port is 80 (Note: whether to turn off the firewall.)
Insert picture description here

1.4 Configure virtual host

Virtual host refers to that in a server, we use Nginx to configure multiple websites.
How to distinguish different websites:

  1. Different ports
  2. Different domain names

Differentiate different virtual hosts by port
Nginx configuration file
1. Location of Nginx configuration file

cd /usr/local/nginx/conf
nginx.conf is the configuration file of Nginx

2.Nginx core configuration file description

worker_processes  1; #work的进程数,默认为1
#配置 影响nginx服务器与用户的网络连接
events {
    worker_connections  1024; #单个work 最大并发连接数
}
# http块是配置最频繁的部分 可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能
http {
     # 引入mime类型定义文件
     include    mime.types;
     default_type application/octet-stream;
     sendfile    on;
     keepalive_timeout  65; # 超时时间
 
     #server 配置虚拟主机的相关参数 可以有多个,一个server就是一个虚拟主机
     server {
        # 监听的端口
         listen    80;
        #监听地址
         server_name localhost;    
 
        # 默认请求配置
         location / {
             root  html; # 默认网站根目录
             index index.html index.htm; # 欢迎页
         }
 
        # 错误提示页面
         error_page  500 502 503 504 /50x.html;
         location = /50x.html {
             root  html;
         }
    }
}

Access flow chart
Insert picture description here

Use Notpad to connect to Linux.
Use notepad++ to connect to linux. The advantage is that using notepad++ to edit the batch text of files in linux is much more convenient and faster than operating directly in linux

  1. Install NppFTP in the Notepad plugin
    Insert picture description here
    Insert picture description here
    Insert picture description here
    Insert picture description here
    Insert picture description here
    Insert picture description here
    Insert picture description here

  2. Configure nginx.conf
    1) Use Notpad to add a new server in nginx.conf
    Insert picture description here

http {
     include    mime.types;
     default_type application/octet-stream;
     sendfile    on;
     #tcp_nopush   on;
     #keepalive_timeout 0;
     keepalive_timeout  65;
     #gzip on;
     server {
         listen    80;
         server_name localhost;
         location / {
             root  html;
             index index.html index.htm;
         }
     }
    # 配置新的server
     server {
         listen    81; # 修改端口
         server_name localhost;
         location / {
              root  html81; # 重新制定一个目录
              index index.html index.htm;
         }
     }
}

Copy a copy of the html directory

cp -r html html81
Insert picture description here
html and html81 make a fine distinction
Insert picture description here

Reload configuration file

sbin/nginx -s reload
Insert picture description here

Insert picture description here

Insert picture description here
Insert picture description here
Distinguish different virtual hosts by domain name
Configure domain name mapping

  1. The hosts file can be modified for local testing. Modify the windows hosts file: (C:\Windows\System32\drivers\etc)
  • The mapping relationship between domain name and ip can be configured. If the corresponding relationship between domain name and ip is configured in the hosts file, there is no need to go to the dns server.

Configure nginx mapping
182.168.80.100 www.ng.com

Insert picture description here
Insert picture description here
Configure nginx.conf

#通过域名区分虚拟主机
    server {
        listen 80;
        server_name www.taobao.com;
        location / {
            root taobao;
            index taobao.html;
        }
    }
    server {
        listen 80;
        server_name www.jd.com;
        location / {
            root jd;
            index jd.html;
    }
}

Modify index.html, refresh

sbin/nginx -s reload

access
Insert picture description here

1.5 Reverse proxy

Forward proxy For
example, when we visit Google in China, we can’t access it directly. We can send the request to the proxy server through a forward proxy server, and the proxy server can access Google, so that the proxy can go to Google to get the returned data, and then Return to us so we can access Google.
Insert picture description here
Nginx realizes reverse proxy.
Nginx is installed on the server as a reverse proxy server. The function of Nginx is to forward requests to the subsequent application server
Insert picture description here
Insert picture description here

Configuration steps

  • Step 1: Simply use two tomcat instances to simulate two http servers, and change the ports of tomcat to 8080 and 8081 respectively
    Insert picture description here

  • Step 2: Start two tomcats.

./bin/startup.sh Visit two tomcats http://192.168.80.100:8080/ http://192.168.80.100:8081/

  • Step 3: Reverse proxy server configuration
#反向代理配置
#upstream中的server是真正处理请求的应用服务器地址
upstream lagou1{
#用server定义HTTP地址
    server 192.168.80.100:8080;
}
 
server {
    listen    80;
    server_name www.lagou1.com;
    location / {
    # 利用 proxy_ pass可以将请求代理到upstream命名的HTTP服务
        proxy_pass http://lagou1;  #转发到的地址
        index index.html index.htm;
    }
}
 
upstream lagou2{
    #用server定义HTTP地址
    server 192.168.80.100:8081;
}
 
server {
    listen    80;
    server_name www.lagou2.com;
    location / {
        proxy_pass http://lagou2;
        index index.html index.htm;
    }
}
  • Step 4: nginx reloads the configuration file

nginx -s reload

  • Step 5: Configure the domain name, add the mapping relationship between the domain name and ip in the hosts file

192.168.80.100 www.lagou1.com
192.168.80.100 www.lagou2.com

1.6 Load balancing

What is load balancing?
When a request is sent, Nginx acts as a reverse proxy server and will find the subsequent target server to process the request according to the request. This is a reverse proxy. Then, if there are multiple target servers, which one to find The server handles the current request? This process of reasonably distributing requests to the server is called load balancing.
Insert picture description here
Why load balancing is used
when the system is facing a large number of user accesses and the load is too high, usually increasing the number of servers for horizontal expansion, load balancing The main purpose is to share the traffic, distribute the requests to different servers reasonably, and avoid temporary network congestion.
Load balancing strategy
polling

  • The default strategy, each request is assigned to different servers one by one in chronological order. If a certain server goes offline, it can be automatically removed
  • Configuration method
    Load balancing
#配置负载均衡
upstream lagouedu{ 
 
	# 用server定义 HTTP地址 
	server 192.168.80.100:8080; 
	server 192.168.80.100:8081; 
}
 
server { 
	listen 80; 
 
	server_name www.lagouedu.com;
 
	location / {
		# 利用 proxy_ pass可以将请求代理到upstream命名的HTTP服务
		proxy_pass http://lagouedu;
		index index.html;
	}
}

weight

The server weight can be adjusted according to the actual situation of the server. The higher the weight, the more requests are allocated, and the lower the weight, the fewer requests. The default is 1.


#Load balancing upstream lagouServer{ # Use server to define HTTP address server 192.168.52.100:8080 weight=1; server 192.168.52.100:8081 weight=10; }



2. Publish the web project

1. Project packaging

Insert picture description here

2. Publish the web project to tomcat

Copy to the webapps directory of tomcat1 and tomcat2 in linux
Insert picture description here
start tomcat1 tomcat2

[root@centos7-1 bin]# ./startup.sh && tail -100f …/logs/catalina.out

3. Configure nginx load balancing

upstream vis{ 
    # 用server定义 HTTP地址 
    server 192.168.80.100:8080; 
    server 192.168.80.100:8081; 
}
    
server { 
    listen 80; 
    server_name www.vis.com;
    location / {
        # 利用 proxy_ pass可以将请求代理到upstream命名的HTTP服务
        proxy_pass http://vis;
        index login.html ;
    }
}

Guess you like

Origin blog.csdn.net/weixin_47134119/article/details/112253597