nginx实现反向代理商、负载均衡
1.简介
本篇博文是《nginx实现动态/静态文件缓存-技术流ken》的二部曲。将详细详情nginx如何实现反向代理商以及负载均衡技术,并辅以实战案例。
反向代理商–“反向代理商(Reverse Proxy)方式是指以代理商服务器来接受internet上的连接请求,而后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的用户端,此时代理商服务器对外就体现为一个反向代理商服务器。”
负载均衡–“网络专用术语,负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设施和服务器的带宽、添加吞吐量、增强网络数据解决能力、提高网络的灵活性和可用性。”
2.nginx实现反向代理商
1.几个概念
反向代理商:在收到用户端请求之后,会修目标IP地址和端口
正向代理商:在收到用户端请求之后,会修源IP地址和端口
上游服务器:代理商服务器后台的哪些真正给用户端提供服务的节点,这样的服务器称之为上游服务器
下游服务器:用户端就是下游节点
2.反向代理商指令
板块:nginx_http_proxy_module
指令
proxy_pass:指定上游服务器的ip和端口
proxy_set_header:指定在重新封装请求报文的时候,增加一个新的首部
Syntax: proxy_pass URL;
Default: —
Context: location, if in location, limit_except
例子:proxy_pass http://10.220.5.200:80;
Syntax: proxy_set_header field value;
Default: proxy_set_header Host $proxy_host;
Context: http, server, location
3.反向代理商简单示例
location / {
proxy_pass http://10.220.5.180;
proxy_set_header X-Real-IP $remote_addr
proxy_set_header Host $proxy_host;
}
4.反向代理商实战案例
1.环境准备
centos7.5
反向代理商服务器IP:172.20.10.7/28
web1服务器IP:172.20.10.8/28
web2服务器IP:172.20.10.9/28
2.配置反向代理商服务器端
yum安装nignx需要配置网络源,复制下面的代码到你的yum仓库中
[ken]
name=ken
enabled=1
gpgcheck=0
baseurl=https://mirrors.aliyun.com/epel/7Server/x86_64/
安装nginx
[root@ken ~]# yum install nginx -y
配置nginx文件,我们实现这样一个效果,静态文件都被代理商到172.20.10.8,动态文件都被调度到172.20.10.9,实现动静分离。
[root@ken ~]# vim /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;
index index.html index.php;
# Load configuration files for the default server block.
location / {
proxy_pass http://172.20.10.8;
proxy_set_header host $proxy_host;
proxy_set_header realip $remote_addr;
}
location ~^/.*(\.php)$ {
proxy_pass http://172.20.10.9;
proxy_set_header host $proxy_host;
proxy_set_header realip $remote_addr;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
进行语法检测
[root@ken ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
检查没有问题之后进行重启
[root@ken ~]# systemctl start nginx
3.配置web服务器端
安装apache
[root@ken ~]# yum install httpd -y
准备测试文件,172.20.10.8准备静态文件
[root@ken ~]# echo "this is 172.20.10.8 for static test">/var/www/html/index.html
172.20.10.9需要下载php以便支持动态文件
[root@ken html]# yum install php -y
172.20.10.9准备动态文件,
[root@ken ~]# cd /var/www/html/
[root@ken html]# vim index.php
<?php
phpinfo();
?>
4.web服务器重启
[root@ken html]# systemctl restart httpd
5.关闭安全服务
[root@ken ~]# iptables -F
6.浏览器测试
请求静态文件测试

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » nginx实现反向代理商、负载均衡