本篇博文是《nginx实现动态/静态文件缓存-技术流ken》的二部曲。将详细介绍nginx如何实现反向代理以及负载均衡技术,并辅以实战案例。
反向代理--“反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。”
负载均衡--“网络专用术语,负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。”
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
4.web服务器重启
[root@ken html]# systemctl restart httpd
5.关闭安全服务
[root@ken ~]# iptables -F
6.浏览器测试
请求静态文件测试
静态文件请求已经成功转发至172.20.10.8。
测试成功!
请求动态文件测试
动态文件请求已经成功转发至172.20.10.9.
测试成功!
7.补充
补充一
补充1:
location如下
location /admin {
proxy_pass http://www.ken.com/;
proxy_pass http://www.ken.com;
}
请求的url 是http://www.ken.com/admin/a.html
如果代理方式是 proxy_pass http://www.ken.com/; 那么去www.ken.com的跟目录下找a.html,/代表完全代理。
如果代理方式是 proxy_pass http://www.ken.com; 那么去www.ken.com的跟目录下的admin找a.html
补充二
补充2:
如果location中使用了模式匹配(正则),那么,location中的url会直接补充到代理节点的后面.
此时,上游服务器的的后面不能有任何内容,包括 /
location ~ \.php$ {
proxy_pass http://www.ken.com; [正则表达式proxy_pass转发的地址后面什么都不能加] <<< 正确写法
proxy_pass http://www.ken.com:80; <<< 正确写法
proxy_pass http://www.ken.com/; <<< 错误写法
proxy_pass http://www.ken.com/img; <<< 错误写法
}
此时,如果请求的url是 http://www.baidu.com/book/stu/a.php ,就会代理成 http://www.ken.com/book/stu/a.php
补充三
补充3:
在location中如果有重定向的话,那么就用重定向后的uri替换掉代理节点中的uri
location / {
rewrite /(.*)$ /index.php?name=$1 break;
proxy_pass http://www.baidu.com:80/img;
}
此时,如果请求的url是 http://www.ken.com/bajie ,就会代理成 www.baidu.com/index.php?name=bajie
1.几个概念
调度器:分发用户的请求到一个后端节点
上游服务器(真实服务器):每个真正用来处理用户请求的节点都是一个上游服务器
CIP:客户端的IP地址
RIP:真实服务器的IP地址
VIP:虚拟IP,用户所看到的是也是虚拟IP
2.指令
指令:upstream 作用:定义一个上游服务器组 格式 upstream name { server 上游服务器1 参数 参数; server 上游服务器1 参数 参数; server 上游服务器1 参数 参数; }
3.重要参数
weight=#:设置服务器的权重(数字越大,权重越高) backup: 设置服务器处于备用状态(其他节点出现故障,备用节点才开始工作) down:设置让一个节点处于离线状态(经常用在维护一个节点的情况下) max_fails=number:设置连续几次转发失败就认为该节点出现故障,然后就不再向该节点转发用户请求了 fail_timeout=time: 和上个参数组合使用,作用是设置等待上游服务器响应超时时间
4.nginx实现负载均衡实战案例
1.环境准备
centos7.5
nginx服务器IP:172.20.10.7/28
web1服务器端IP:172.20.10.8/28
web2服务器端IP:172.20.10.9/28
2.配置nginx服务器端
安装nginx略
配置nginx文件
[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;
upstream ken {
server 172.20.10.8 weight=1 max_fails=3 fail_timeout=5;
server 172.20.10.9 weight=2 max_fails=3 fail_timeout=5;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;
index index.php index.html;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://ken/;
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
重启nginx
[root@ken ~]# systemctl restart nginx
3.配置web服务器端
略.和上面反向代理配置一样。
4.浏览器测试
输入nginx服务器端的IP地址
因为172.20.10.9的权重为2,即出现两次172.20.10.9才会出现一次172.20.10.8.进行刷新测试
测试成功!
nginx的三大功能,缓存,反向代理,负载均衡,已经全部讲解完毕,是否对nginx有了全新的认识那?马上自己动手实验一下吧
版权声明:我们致力于保护作者版权,注重分享,被刊用文章【nginx正向代理配置(nginx实现反向代理)】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!;
工作时间:8:00-18:00
客服电话
电子邮件
beimuxi@protonmail.com
扫码二维码
获取最新动态
