Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set load balance and reverse proxy in Nginx

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to set up load balancer and reverse proxy in Nginx? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

What is load balancing? Concept

Load balancing is a key component of a highly available network infrastructure and is often used to distribute workloads across multiple servers to improve the performance and reliability of websites, applications, databases, or other services.

Commonly used load balancing algorithms

The load balancing algorithm determines which healthy servers at the back end will be selected. Several commonly used algorithms:

Round Robin (polling): select the first server in the list for the first request, then move the list down to the end in order, and then cycle.

Least Connections (minimum connection): the server with the least number of connections is preferred. It is recommended when the session is generally long.

Source: select the server to be forwarded based on the hash of the IP of the request source. This approach ensures to some extent that specific users can connect to the same server. If your application needs to process status and requires the user to connect to the same server as before. You can create associations based on the client's IP information through the Source algorithm, or you can use sticky sessions (sticky sessions).

There are two kinds of load balancing strategies provided by Nginx: built-in strategy and extended strategy. The built-in policies are polling, weighted polling, Ip hash. Expansion strategy, only you can not think of nothing he can not do, you can refer to all the load balancing algorithms, give him one by one to find out to do the implementation. An example of an expansion strategy is fair, which allocates requests according to the response time of the server, and gives priority to those with short response time, that is, priority with low load pressure. Fair source code

See: what is load balancing? Nginx load balancing configuration

What is a reverse proxy? Forward agent

A forward proxy is a proxy server (intermediate server) located between the client and the target server. In order to obtain content from the original server, the client sends a request to the proxy server and specifies the target server, and then the agent transfers the content to the target server and returns the obtained content to the client. In the case of a forward proxy, the client must make some special settings before it can be used.

Reverse proxy

The reverse proxy is just the opposite. To the client, the reverse proxy is like the target server. And the client does not need to make any settings. The client sends a request to the reverse proxy, and then the reverse proxy determines where the request goes and transfers the request to the client, so that the content is just like himself. At one time, the client does not perceive the services behind the reverse proxy. Therefore, the client does not need to make any settings, just regard the reverse proxy server as the real server.

Reference: the difference between forward proxy and reverse proxy

Nginx installation configures the yum source through yum source installation

First, modify the company's yum source configuration, cd / etc/yum.repos.d/, and modify CentOS-7.6-Base.repo.

In addition, install the epel source. (extra package for enterprise linux, provides additional software packages for the "Red Hat" operating system, suitable for RHEL, CentOS, etc., with more than 10, 000 pieces of software, it is strongly recommended to install) yum install epel-release if epel cannot be installed, see: yum install epel source

Add Nginx warehouse vim / etc/yum.repos.d/nginx.repo enter the following content [nginx] name=nginx repo baseurl= http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1

Installation: yum install nginx. Installation using yum is installed online, but the version may not be up to date, and source code installation is required if the latest version is required.

Start Nginx systemctl start nginx or enter / usr/sbin, execute. / nginx or execute service nginx start

Test page http://127.0.0.1/ if the page does not come out, check the firewall configuration vi / etc/sysconfig/iptables to add the following-An INPUT-m state-state NEW-m tcp-p tcp-dport 80-j ACCEPT (allow 80 ports through the firewall)-An INPUT-m state-state NEW-m tcp-p tcp-dport 3306-j ACCEPT (mysql port allows port 3306 to pass through the firewall) and then restart the firewall: service iptables restart

Add Nginx to the system startup item so that it starts automatically every time you boot, using the following command: systemctl enable nginx

Refer to: Linux (Centos7) yum install Nginx

Install 1 through the source code. Before installation, update the system software source first, and update the system with the following command: yum update

An explanation of the two commands:

Yum-y update-upgrade all packages, change software settings and system settings, upgrade the system version of the kernel.

Yum-y upgrade-upgrade all packages without changing software and system settings, system version upgrade, kernel unchanged.

two。 Dependent package installation

[root@localhost src] # yum-y install gcc gcc-c++ autoconf automake libtool make cmake [root@localhost src] # yum-y install zlib zlib-devel openssl openssl-devel pcre-devel

3. Download the Nginx installation source file

Http://nginx.org/en/download.html nginx-1.10.2.tar.gz

4. New nginx users and user groups

Log in to the system as root and execute the following command to create a new user.

[root@localhost src] # groupadd nginx [root@localhost src] # useradd-g nginx-M nginx

The-M argument of the useradd command is used not to establish a home directory for nginx

Modify / etc/passwd to prevent nginx users from bash login (nginx users later changed from / bin/bash to / sbin/nologin)

Vi / etc/passwd then find the line with nginx and change it to (later / bin/bash to / sbin/nologin): for example: nginx..1002:1003::/home/nginx:/sbin/nologin

5. Configure, compile, install

Let's go to the directory of the extracted nginx source code: / usr/local/src/ execute the following command

[root@localhost ~] # cd / usr/local/src/nginx* [root@localhost nginx-1.10.3] # pwd/usr/local/src/nginx-1.10.3 [root@localhost nginx-1.10.3] # [root@localhost nginx-1.10.3] #. / configure-- prefix=/usr/local/nginx\-- pid-path=/usr/local/nginx/run/nginx.pid\-- with-http_ssl_module\-- user=nginx\- Group=nginx\-- with-pcre\-- without-mail_pop3_module\-- without-mail_imap_module\-- without-mail_smtp_module--prefix=/usr/local/nginx is specified to be installed in the / usr/local/nginx directory. After the above configuration is completed, then compile [root@localhost nginx-1.10.3] # make [root@localhost nginx-1.10.3] # make install to view the installed version of the program: [root@localhost nginx-1.10.3] # / usr/local/nginx/sbin/nginx-vnginx version: nginx/1.10.3

Note: the backslash above indicates that the line wrapping continues.

6. Start, stop

There are several ways to manage nginx:

6.1 start Nginx

/ usr/local/nginx/sbin/nginx

6.2 stop Nginx calmly:

Kill-QUIT main process number # as output from the ps command in the previous step is 29151, which is the main process number of Nginx

6.3 Quick stop Nginx:

Kill-TERM main process number

6.4.Forcefully stop Nginx:

Pkill-9 nginx

6.5 smooth restart of nginx

/ usr/nginx/sbin/nginx-s reload

Reference: Nginx installation

The difference between the two installations

Yum installation is online installation, the benefits are: simple installation, not easy to make mistakes; source code package installation is first downloaded the source code of nginx, compiled in their own system to generate an executable file, and then execute, the advantage is: because it is compiled in their own system, more in line with their system performance, that is to say, in their own system to implement nginx services performance is more efficient.

Different installation paths for software compiled and installed by source packages are usually placed under the / usr/local/ package name path. Through source installation, you can specify the installation path yourself.

Yum installs nginx in different ways. We can use the system service command service to start or stop service nginx start # start nginx service service nginx stop # stop nginx service service nginx restart # restart nginx service source package installation nginx can not be started using service, you need to execute the nginx executable under the sbin directory under the nginx installation directory As follows (my nginx is installed in / usr/local/webserver/ directory) ➜~ / usr/local/webserver/nginx/sbin/nginx # starts the nginx service➜ ~ / usr/local/webserver/nginx/sbin/nginx-s stop # stops the nginx service

Refer to: detailed installation process of nginx server (use yum and source package installation, and explain their differences)

Nginx configuration parses nginx configuration structure. # Global block events {# events block.} http # http block {... # http global block server # server block {... # server global block location [PATTERN] # location block {. } location [PATTERN] {...}} server {...}... # http Global Block} nginx configuration details # each instruction must end with a semicolon. # user administrator administrators; # configure users or groups. Default is nobody nobody. # worker_processes 2; # the number of processes allowed to be generated. The default is 1#pid / nginx/pid/nginx.pid; # to specify the address where the nginx process runs files to store, error_log log/error.log debug; # to determine the log path and level. This setting can be put into the global block, http block, server block, and the level is: debug | info | notice | warn | error | crit | alert | emergevents {accept_mutex on; # sets the serialization of network connections to prevent the occurrence of panic phenomena. The default is on multi_accept on; # to set whether a process accepts multiple network connections at the same time. The default is off # use epoll; # event-driven model, select | poll | kqueue | epoll | resig | / dev/poll | eventport worker_connections 1024. # maximum number of connections, default is 512} http {include mime.types; # File extension and File Type Mapping Table default_type application/octet-stream; # default file type, default is text/plain # access_log off; # unservice log log_format myFormat'$remote_addr-$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for' # Custom format access_log log/access.log myFormat; # combined is the default value of log format sendfile on; # allows sendfile to transfer files, default is off, can be in http block, server block, location block. Sendfile_max_chunk 100k; # the number of transfers per call per process cannot be greater than the set value. The default is 0, that is, there is no upper limit. Keepalive_timeout 65; # connection timeout. The default is 75s, which can be found in the http,server,location block. Upstream mysvr {server 127.0.0.1server 7878; server 192.168.10.121 backup; # Hot standby} error_page 404 https://www.baidu.com; # error page server {keepalive_requests 120; # maximum number of single connection requests. Listen 4545; # listening port server_name 127.0.0.1; # listening address location ~ * ^. + ${# request url filtering, regular matching, ~ is case-sensitive, ~ * is case-insensitive. # root path; # Root directory # index vv.txt; # set the default page proxy_pass http://mysvr; # request to mysvr defined server list deny 127.0.0.1; # rejected ip allow 172.18.5.54; # allowed ip} several common configuration items:

$remote_addr and $http_x_forwarded_for are used to record the ip address of the client

$remote_user: used to record the client user name

$time_local: used to record access time and time zone

$request: url and http protocols used to record requests

$status: used to record the status of the request; success is 200

$body_bytes_s ent: the size of the file body content that the record is sent to the client

$http_referer: used to record links accessed from that page

$http_user_agent: record information about client browsers

Other instructions

Surprise phenomenon: when a network connection arrives, multiple sleep processes are awakened at the same time, but only one process can get the link, which will affect the performance of the system.

Each instruction must end with a semicolon.

See:

Nginx reverse proxy and load balancer configuration [nginx] configuration Nginx implementation of load balancing Nginx configuration details

This is the answer to the question about how to set up load balancer and reverse proxy in Nginx. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report