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 use Nginx reverse proxy and load balancing

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to use Nginx reverse proxy and load balancing". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is reverse proxy and load balancing

What is reverse proxy?

When we have a server cluster and the content of each server in the server cluster is the same, we cannot access the server cluster server directly from the personal computer. The cluster must be accessed through a third-party server.

At this time, we access the content of the server cluster through a third-party server, but we don't know which server provided the content. This kind of proxy is called reverse proxy.

What is load balancing?

The company will set up a lot of servers, which form a server cluster, and then, when users visit the website, they first visit an intermediate server, and then ask the intermediate server to select a less stressed server in the server cluster, and then introduce the access request to the selected server.

Therefore, every time users visit, they will ensure that the pressure of each server in the server cluster tends to be balanced, share the server pressure, and avoid the server crash.

Bottom line: nginx will assign you a server with less pressure to access.

Implementation of Nginx reverse proxy and load balancing

When users visit the website, they first visit the nginx server, and then the nginx server selects a less stressed server from the server cluster to direct the access request to that server.

Nginx configuration

The following modification of the configuration I will come down from the mac system for a simple demonstration, how to install then also temporarily give priority to mac, windows system directly to the Nginx official website to download and install.

Install nginx 1-go to the homebrew official website, then copy the command, pre-install what is needed 2-brew install nginx install nginx 3-nginx-v display the version number and enter nginx cd / usr/local/etc/nginx

The following picture shows the contents of the file under the nginx folder.

When we enter this directory, we can manipulate nginx, and then we will list some very useful commands. Type them several times and be sure to remember them.

Nginx common commands

Start nginx

Nginx

When you finish typing the five keys of nginx, there is no response. All you have to do is visit localhost:8080 (default).

Close nginx

If the following picture occurs, don't panic, because nginx has been started before.

Just nginx-s stop and stop the nginx service

Then start nginx again.

Restart nginx

Nginx-s reload

You need to restart nginx every time you modify the .conf file.

Check configuration

Check whether the modified nginx.conf configuration is correct

Nginx-t

If the following ok and successfull appear, it means that it is correct, and everything else is wrong.

Nginx: the configuration file / usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file / usr/local/etc/nginx/nginx.conf test is successful

For our front end to work normally, there is no need to modify the nginx too much. We modified the nginx configuration in order to do some reverse proxies.

Proxy_pass

Nginx reverse proxy is mainly configured through proxy_pass. Fill in the address of the development machine of your project after proxy_pass. The normal format is proxy_pass URL.

Server {listen 80; location / {proxy_pass http://10.10.10.10:20186;}}

Load balancing based on Upstream module

Ip_hash instruction

Server instruction

Upstream instruction and related variables

The three instructions written above are analyzed directly through the code.

/ / modify nginx.conf worker_processes 1; events {worker_connections 1024;} http {upstream firstdemo {server 39.106.145.33; server 47.93.6.93;} server {listen 8080; location / {proxy_pass http://firstdemo;}

The nginx.conf modified above is the file in the wreath above, and the main changes to the nginx configuration are here. Simplify the complexity and directly replace the contents of the original nginx.conf with less than 20 lines of code above.

Since it is less than 20 lines, explain all the corresponding contents in it. It would be nice to have an understanding.

Worker_processes

Number of worker processes, the same as the number of cores in CPU

Worker_connections

Number of * connections allowed per process

Upstream module

Load balancing depends on it.

Syntax format: upstream name {}

The two server written in it correspond to different servers

Server module

Implement reverse proxy

Listen Supervisory Port number

Location / {} access root path

Proxy_pass http://firstdemo, proxy to two servers in firstdemo

After the nginx.conf has been modified above, don't forget the most important step to restart nginx.

If you visit localhost:8080 again, you will see the following page:

There is another page:

Each refresh accesses a different server, thus achieving load balancing.

However, what should be done is that when a user accesses one of the servers for * times, he or she can visit the server directly the next time he visits the server. There is no need to change all the time. Then it gives full play to the power of ip_hash.

/ / omit. Upstream firstdemo {ip_hash; server 39.106.145.33; server 47.93.6.93;}

The function of ip_hash is that if the server is recorded after * visits, and then all the visits are made to the server, for example, if the * visits are 33 servers, then the subsequent visits will be assigned to 33 servers.

Simple use at work

When the company is developing a project, when it comes to design and product inspection, we can't ask them to match a host every time. After all, it's not friendly, and it's troublesome to walk and check. Therefore, it is more important to give them an intuitive feeling that you can see what it looks like by giving them an access address.

Let's take a look at the reverse proxy configuration that I normally do when nginx is in the company, just like the one above us, except that we add a server_name and use the specified domain name to access it.

Server {listen 80; server_name chd.news.so.m.qss.test.so.com; auth_basic off; location / {proxy_pass http://10.10.10.10:20186; proxy_set_header Host $host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for This is the end of proxy_connect_timeout 60; proxy_read_timeout 600; proxy_send_timeout 600;}} "how to use Nginx reverse proxy and load balancing". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Servers

Wechat

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

12
Report