Nginx and PHP Installation and Configuration 6: Nginx Reverse Proxy and Load Balancing Deployment Guide

不言
Release: 2023-03-23 19:34:01
Original
1879 people have browsed it

The content shared with you in this article is about Nginx and PHP installation and configuration. The Nginx reverse proxy and load balancing deployment guide has a certain reference value. Friends in need can refer to it

1. Find and open the conf file


2. Load balancing configuration
nginx’s upstream defaults to a polling method to achieve load balancing. This method In , each request is assigned to different backend servers one by one in chronological order. If the backend server goes down, it can be automatically eliminated.
Another way is ip_hash: each request is allocated according to the hash result of the accessed IP, so that each visitor has fixed access to a back-end server, which can solve the session problem.
Load balancing is something that our high-traffic website needs to do. Now I will introduce to you the load balancing configuration method on the Nginx server. I hope it will be helpful to students in need.

Load Balancing


Let’s first briefly understand what load balancing is. It can be explained by just understanding the literal meaning. NThe servers share the load evenly, and there will be no downtime due to a high load on a certain server and a certain server being idle. Then the premise of load balancing is that it can be achieved by multiple servers, that is, more than two servers are enough.

Test environment

Test domain name a.com

AServerIP :192.168.5.149 (Master)

BServerIP :192.168.5.27

CServerIP 192.168.5.126

## Deployment idea A
server is used as the main server, and the domain name is directly resolved to the A server (192.168.5.149 ), load balancing from A server to B server (192.168.5.27) and C server (192.168.5.126).

##A

Servernginx.confSettings
Open
nginx.conf, the file location is nginxIn the conf directory of the installation directory.

Add the following code in the

http section

upstream a.com { 
      server  192.168.5.126:80; 
      server  192.168.5.27:80; 
} 
  
server{ 
    listen 80; 
    server_name a.com; 
    location / { 
       proxy_pass         http://a.com; 
        proxy_set_header  Host            $host; 
        proxy_set_header  X-Real-IP        $remote_addr; 
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for; 
    } 
}
保存重启nginx
B、C服务器nginx.conf设置
打开nginx.confi,在http段加入以下代码
server{ 
    listen 80; 
    server_name a.com; 
    index index.html; 
    root /data0/htdocs/www; 
}
Copy after login

Save and restart

nginx

测试
当访问a.com的时候,为了区分是转向哪台服务器处理我分别在BC服务器下写一个不同内容的index.html文件,以作区分。

打开浏览器访问a.com结果,刷新会发现所有的请求均分别被主服务器(192.168.5.149)分配到B服务器(192.168.5.27)与C服务器(192.168.5.126)上,实现了负载均衡效果。


假如其中一台服务器宕机会怎样?
当某台服务器宕机了,是否会影响访问呢?

我们先来看看实例,根据以上例子,假设C服务器192.168.5.126这台机子宕机了(由于无法模拟宕机,所以我就把C服务器关机)然后再来访问看看。


我们发现,虽然C服务器(192.168.5.126)宕机了,但不影响网站访问。这样,就不会担心在负载均衡模式下因为某台机子宕机而拖累整个站点了。

如果b.com也要设置负载均衡怎么办?
很简单,跟a.com设置一样。如下:

假设b.com的主服务器IP192.168.5.149,负载均衡到192.168.5.150192.168.5.151机器上

现将域名b.com解析到192.168.5.149IP上。

在主服务器(192.168.5.149)nginx.conf加入以下代码:

u

pstream b.com { 
      server  192.168.5.150:80; 
      server  192.168.5.151:80; 
} 
  
server{ 
    listen 80; 
    server_name b.com; 
    location / { 
        proxy_pass        http://b.com; 
        proxy_set_header  Host            $host; 
        proxy_set_header  X-Real-IP        $remote_addr; 
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for; 
    } 
}
保存重启nginx
在192.168.5.150与192.168.5.151机器上设置nginx,打开nginx.conf在末尾添加以下代码:
server{ 
    listen 80; 
    server_name b.com; 
    index index.html; 
    root /data0/htdocs/www; 
}
Copy after login


保存重启nginx

完成以后步骤后即可实现b.com的负载均衡配置。

主服务器不能提供服务吗?
以上例子中,我们都是应用到了主服务器负载均衡到其它服务器上,那么主服务器本身能不能也加在服务器列表中,这样就不会白白浪费拿一台服务器纯当做转发功能,而是也参与到提供服务中来。

如以上案例三台服务器:

A服务器IP 192.168.5.149 (主)

B服务器IP 192.168.5.27

C服务器IP 192.168.5.126

我们把域名解析到A服务器,然后由A服务器转发到B服务器与C服务器,那么A服务器只做一个转发功能,现在我们让A服务器也提供站点服务。

我们先来分析一下,如果添加主服务器到upstream中,那么可能会有以下两种情况发生:

1、主服务器转发到了其它IP上,其它IP服务器正常处理;

2、主服务器转发到了自己IP上,然后又进到主服务器分配IP那里,假如一直分配到本机,则会造成一个死循环。

怎么解决这个问题呢?因为80端口已经用来监听负载均衡的处理,那么本服务器上就不能再使用80端口来处理a.com的访问请求,得用一个新的。于是我们把主服务器的nginx.conf加入以下一段代码:

server{ 
    listen 8080; 
    server_name a.com; 
    index index.html; 
    root /data0/htdocs/www; 
}
Copy after login


重启nginx,在浏览器输入a.com:8080试试看能不能访问。结果可以正常访问

既然能正常访问,那么我们就可以把主服务器添加到upstream中,但是端口要改一下,如下代码:

upstream a.com { 
      server  192.168.5.126:80; 
      server  192.168.5.27:80; 
      server  127.0.0.1:8080; 
}
Copy after login

由于这里可以添加主服务器IP192.168.5.149或者127.0.0.1均可以,都表示访问自己。

RestartNginx, and then visit a.com to see if it will be assigned to the main server.

The main server can also join the service normally.

Finally
1. Load balancing is not unique to nginx, famous Dingding's apache is also available, but the performance may not be as good as nginx.

2. Multiple servers provide services, but the domain name is only resolved to the main server, and the real server IP will not be ## You can get it by clicking #ping, which adds a certain degree of security.

3. The #IP# in upstream is not necessarily the internal network, not the external network IP is also available. However, the classic case is that a certain IP in the LAN is exposed to the external network, and the domain name is directly resolved to this IP. Then the main server forwards it to the intranet server IP.

4. If a certain server is down, it will not affect the normal operation of the website. Nginx will not forward the request to the down server. IP

Reference article:


http://www.php100.com /html/program/nginx/2013/0905/5525.html

http://blog.csdn.net/xyang81/article/details/51702900

http://www.linuxdiyf .com/linux/10205.html

http://www.cnblogs.com/jacktang/p/3669115.html

Related recommendations:

Nginx and php installation and configuration 5. LINUX uses PHPIZE to install PHP GD extension

Nginx and php installation and configuration 4. nginx and php start or restart

Nginx and PHP installation and configuration three nginx configuration files



The above is the detailed content of Nginx and PHP Installation and Configuration 6: Nginx Reverse Proxy and Load Balancing Deployment Guide. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!