> 首页 > 文章 > 八卦 > nginx反向代理配置详解

nginx反向代理配置详解

来源:网络 作者:网友上传 时间:06-07 手机版

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/28web1服务器IP:172.20.10.8/28web2服务器IP:172.20.10.9/282.配置反向代理服务器端yum安装nignx需要配置网络源,复制下面的代码到你的yum仓库中[ken]name=kenenabled=1gpgcheck=0baseurl=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 -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: 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

3.nginx实现负载均衡

1.几个概念调度器:分发用户的请求到一个后端节点上游服务器(真实服务器):每个真正用来处理用户请求的节点都是一个上游服务器CIP:客户端的IP地址RIP:真实服务器的IP地址VIP:虚拟IP,用户所看到的是也是虚拟IP2.指令指令:upstream 作用:定义一个上游服务器组 格式 upstream name { server 上游服务器1 参数 参数; server 上游服务器1 参数 参数; server 上游服务器1 参数 参数; }

3.重要参数 weight=#:设置服务器的权重(数字越大,权重越高) backup: 设置服务器处于备用状态(其他节点出现故障,备用节点才开始工作) down:设置让一个节点处于离线状态(经常用在维护一个节点的情况下) max_fails=number:设置连续几次转发失败就认为该节点出现故障,然后就不再向该节点转发用户请求了 fail_timeout=time: 和上个参数组合使用,作用是设置等待上游服务器响应超时时间

4.nginx实现负载均衡实战案例1.环境准备centos7.5nginx服务器IP:172.20.10.7/28web1服务器端IP:172.20.10.8/28web2服务器端IP:172.20.10.9/282.配置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 -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: 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反向代理配置详解

手机怎么申请苹果id账号

如何用ghost备份

苹果电池怎么保护寿命

win10如何进安全模式

电脑上的快捷键是什么

win10如何设置回收站

标签: 详解 nginx

声明:《nginx反向代理配置详解》一文由排行榜大全(网友上传 )网友供稿,版权归原作者本人所有,转载请注明出处。如果您对文章有异议,可在反馈入口提交处理!

最近更新

  • 手机设置时间在哪里

    当我们长时间玩苹果手机停不下来,或是小孩子玩手机想要限制时间时,我们可以设置屏幕使用时间来达到限制时间的效果。本期小编为大家带来了详...

    八卦 日期:2023-06-07

  • 简单的科学小发明制作方法

    预防水满科技小发明的制作方法:1、首先用一根铁条绕成U形穿插上一块泡沫块;2、接着在铁条两边末端处粘上一块小木板,再粘上两条绕成S形的铜线;3...

    百科 日期:2023-06-07

  • 家里地板砖怎么翻新

    1、清洗翻新。假如是因为灰尘污染等而造成家里地砖陈旧的话,那么应该使用清洁剂把污渍清洗掉。若地砖边缘很黑的话,则可以使用香蕉水来进行清...

    百科 日期:2023-06-07

  • 新奥迪a6l致雅与动感的区别在哪里

    新奥迪a6l致雅与动感的区别在哪里新款奥迪A6L优雅与动感的主要区别在于外观风格。优雅版更加沉稳优雅,强调奢华优雅。动态型就是表现动态和...

    汽车 日期:2023-06-07

  • 2023广州文旅消费券东莞可以用吗

    2023广州文旅消费券东莞可以用吗? 不可以。 核销规则:消费者成功获取消费券后,在有效期内到参与活动企业的指定门店或线上平台,通过云闪付APP支...

    景点 日期:2023-06-07

  • nginx反向代理配置详解

    1.简介本篇博文是《nginx实现动态/静态文件缓存-技术流ken》的二部曲。将详细介绍nginx如何实现反向代理以及负载均衡技术,并辅以实战案例。...

    八卦 日期:2023-06-07

  • 简单的牛奶打堆怎么好看

    1、看准包装箱上比较醒目的标志,然后顺着这个明显的标志摆成任意图案;2、普通摆堆,只要稳固即可;3、在四周摆几箱,然后上面放方板,再在方板中间放...

    百科 日期:2023-06-07

  • 美赫吊顶属于几线品牌

    1、美赫集成吊顶是一家专业的集成吊顶生产制造商,公司自成立以来,一直致力于集成吊顶产品的研发和制造,经过多年的额努力,成功的将吊顶、照明以...

    百科 日期:2023-06-07

邮箱不能为空
留下您的宝贵意见