使用nginx tcp proxyproxy 为什么总是502了

Nginx反向代理的配置 -- 简明现代魔法杨云1028 的BLOG
用户名:杨云1028
文章数:72
评论数:462
访问量:247416
注册日期:
阅读量:5863
阅读量:12276
阅读量:318228
阅读量:1030265
51CTO推荐博文
目录1、nginx软件安装2、nginx proxy负载均衡搭建过程3、不同域名站点分离4、WEB日志客户端IP记录5、根据扩展名实现服务器的动静分离6、http proxy参数7、Location指令8、upstream参数9、nginx负载均衡调度算法#####################################################本文内容来自《老男孩linux运维实战培训》学生―MR杨欢迎广大运维同仁一起交流linux/unix网站运维技术!QQ:E-mail:#####################################################1、nginx软件安装1.1硬件准备4台虚拟机,一台做负载均衡,两台做RS,一台做客户端hostnameIP功能nginx-110.0.0.239nginx代理与负载均衡服务器RS110.0.0.237真实WEB服务器RS210.0.0.240真实WEB服务器Client10.0.0.238客户端1.2软件准备centos 5.8 x86_64nginx-1.2.3.tar.gzpcre-8.30.tar.gz1.3下载地址wgethttp://nginx.org/download/nginx-1.2.9.tar.gzwget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz我使用的源包目录为/usr/local/src/1.4安装pcretar zxf pcre-8.30.tar.gzcd pcre-8.30/./configuremake && make installcd ../安装pcre库是为了兼容nginx rewrite1.5安装nginxuseradd nginx -M -s /sbin/nologintar -zxf nginx-1.2.9.tar.gzcd nginx-1.2.9./configure --user=nginx --group=nginx --prefix=/application/nginx-1.2.9 --with-http_stub_status_module --with-http_ssl_module --with-http_realip_modulemake && make install安装完成后,检查语法,发现有错误![root@nginx-1 nginx-1.2.9]# /application/nginx-1.2.9/sbin/nginx -t /application/nginx-1.2.9/sbin/nginx: error while loading sharedlibraries: libpcre.so.1: cannot open shared object file: No such file ordirectory[root@nginx-1 nginx-1.2.9]#echo "/usr/local/lib" &&/etc/ld.so.conf #配置lib库,解决上述错误[root@nginx-1 nginx-1.2.9]#ldconfig
#使ld.so.conf修改生效[root@nginx-1 nginx-1.2.9]#/application/nginx-1.2.9/sbin/nginx -t#检查语法,成功!nginx: the configuration file/application/nginx-1.2.9/conf/nginx.conf syntax is oknginx: configuration file/application/nginx-1.2.9/conf/nginx.conf test is successful[root@nginx-1 nginx-1.2.9]#ln -s /application/nginx-1.2.9 /application/nginx #增加软链接,方便升级[root@nginx-1 nginx-1.2.9]#ll -d /application/nginx
#查看软链接是否成功,如果闪动就没成功lrwxrwxrwx 1 root root 24 Sep 8 18:42 /application/nginx -&/application/nginx-1.2.9[root@nginx-1 nginx-1.2.9]# echo 'export PATH=$PATH:/application/nginx/sbin' &&/etc/profile#将Nginx命令加入系统全局变量,启动nginx[root@nginx-1 nginx-1.2.9]#source /etc/profile
#使全局变量修改生效[root@nginx-1 nginx-1.2.9]#nginx #启动nginx[root@nginx-1 nginx-1.2.9]#echo'/application/nginx/sbin/nginx' && /etc/rc.local
#加入开机自启动[root@nginx-1nginx-1.2.9]#netstat -plnt |grep 80tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1352/nginx 1.6测试:2、nginx proxy负载均衡搭建过程2.1nginx proxy配置把下面内容添加到nginx.conf中,位置在最后一个大括号的前面。upstream webserver {server 10.0.0.240:80 weight=3;server 10.0.0.237:80 weight=3;}server {listen 80;server_name www.etiantian.location / {proxy_pass http://}}参数解释:upstream webserver { } 定义真实服务器组server {} 定义一个服务配置listen 80 监听80端口server_name 监听的地址或IPlocation / 匹配server_name后的url或IPproxy_pass 代理参数,后接upstream定义的服务器组注:nginx proxy默认使用的是rr算法修改完后,检查语法重启[root@nginx-1 conf]# nginx -tnginx: the configuration file /application/nginx-1.2.9/conf/nginx.confsyntax is oknginx: configuration file/application/nginx-1.2.9/conf/nginx.conf test is successful[root@nginx-1 conf]# nginx -s reload2.2真实服务器配置两台操作一样,不需要动nginx.conf配置文件[root@RS2 nginx]# echo 240-www &/application/nginx/html/index.html[root@RS1 nginx]# echo 237-www &/application/nginx/html/index.html2.3客户端测试:[root@Client ~]# echo "10.0.0.239 www.etiantian.org" &&/etc/hosts [root@Client~]# curl www.etiantian.org240-www[root@Client ~]# curl www.etiantian.org237-www[root@Client ~]# curl www.etiantian.org240-www[root@Client ~]# curl www.etiantian.org237-www[root@Client ~]# curl www.etiantian.org240-www[root@Client ~]# curl www.etiantian.org237-www当前已经可以进行负载均衡,但还存在一些问题。2.4问题描述1:访问blog与bbs时,请求没有转发到真实服务器,而返回是的代理服务器本地的web内容复现问题:2.4.1在真实服务器上配置创建blog,bbs站点目录[root@RS2 nginx]# mkdir/data/html/blog -p[root@RS2 nginx]# mkdir/data/html/bbs -p [root@RS2 nginx]# echo240-blog & /data/html/blog/index.html[root@RS2 nginx]# echo240-bbs & /data/html/bbs/index.html2.4.2新增站点配置把下面内容添加到240的nginx.conf中,位置在最后一个大括号前 server { listen 80; server_name blog.etiantian. #指定匹配域名 location / { root /data/html/ #指定站点目录 index index.html index. }} server { listen 80; server_name bbs.etiantian. #指定匹配域名 location / { root /data/html/ #指定站点目录 index index.html index. }}配置完后检查语法重启[root@RS2 nginx]#/application/nginx/sbin/nginx -tnginx: the configuration file/application/nginx-1.2.9/conf/nginx.conf syntax is oknginx: configuration file/application/nginx-1.2.9/conf/nginx.conf test is successful[root@RS2 nginx]# /application/nginx/sbin/nginx-s reload2.4.3客户端测试修改hosts解析文件如下[root@Client ~]# tail -1 /etc/hosts10.0.0.239 www.etiantian.org blog.etiantian.org bbs.etiantian.org测试:[root@Client ~]# curl www.etiantian.org240-www [root@Client ~]# curl bbs.etiantian.org 239-www[root@Client ~]# curl blog.etiantian.org 239-www可以看出上面返回的都是nginx proxy本地的数据2.4.4找到问题:查看nginx proxy的配置文件,发现只有匹配www.etiantian.org的会被代理到webserver组中的地址,其它的都匹配上访问本地的web站点(下面红色显示)。[root@nginx-1 conf]# grep -v "#"nginx.confworker_processes 1;events { worker_connections 1024;}http { include mime. default_type application/octet- keepalive_timeout 65; server { listen 80; server_ location / { index index.html index. } error_page 500 502 503 504 /50x. location = /50x.html { } }upstream webserver {server 10.0.0.240:80 weight=3;server 10.0.0.237:80 weight=3;}server {listen 80;server_name www.etiantian.location / {proxy_pass http://}}}2.4.5解决方法1)修改nginx proxy配置文件:2)把改为localhosts3)把上面红色字体的server定义注释,本地不需要提供服务,只留下代理配置就OK修改后的配置如下:[root@nginx-1 conf]# grep -v "#"nginx.confworker_processes 1;events { worker_connections 1024;}http { include mime. default_type application/octet- keepalive_timeout 65;upstream webserver {server 10.0.0.240:80 weight=3;server 10.0.0.237:80 weight=3;}server {listen 80;server_namelocation / {proxy_pass http://}}}[root@nginx-1 conf]# nginx -t
#检查语法nginx: the configuration file/application/nginx-1.2.9/conf/nginx.conf syntax is oknginx: configuration file/application/nginx-1.2.9/conf/nginx.conf test is successful[root@nginx-1 conf]# nginx -s reload继续在客户端测试[root@Client ~]# curl www.etiantian.org 237-www[root@Client ~]# curl blog.etiantian.org 240-www[root@Client ~]# curl bbs.etiantian.org 237-www可看出上面请求已经转发到真实服务器上去了。因为localhost是指的本机IP,只要目的IP是本机端口80的就代理到真实服务器上。3、不同域名站点分离上步测试为什么都是www站点?3.1直接访问测试修改hosts文件,直接访问是正常的[root@Client ~]# tail -1 /etc/hosts10.0.0.240 www.etiantian.org blog.etiantian.org bbs.etiantian.org[root@Client ~]# curl bbs.etiantian.org240-bbs[root@Client ~]# curl blog.etiantian.org 240-blog[root@Client ~]# curl www.etiantian.org 240-www直接访问结果是正确的,但为什么通过nginx代理过去的请求,不能区分blog,bbs,www站点呢?原因是:nginx代理转发请求的时候,不携带原始url内容,所以匹配默认www站点了。3.2添加proxy_set_header参数修改nginx代理上的nginx.conf文件,在proxy_pass下添加红色内容 [root@nginx-1 conf]# grep -v "#" nginx.conf|grep proxyproxy_pass http://proxy_set_header Host $/application/nginx/sbin/nginx -t/application/nginx/sbin/nginx -s reload3.3客户端测试:[root@Client ~]# curl bbs.etiantian.org240-bbs[root@Client ~]# curl bbs.etiantian.org237-bbs访问已经正常!4、WEB日志客户端IP记录4.1日志查看日志路径在/application/nginx/logs/下[root@RS2 nginx]# tail -1 logs/access.log 10.0.0.239 - - [09/Sep/:16 +0800]"GET / HTTP/1.0" 200 9 "-" "curl/7.15.5(x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3libidn/0.6.5"发现只记录了nginx代理的地址,没有客户机的地址,不方便统计客户IP访问次数!4.2在nginx proxy配置文件添加X-Forwarded-For参数 在nginx proxy服务器nginx.conf里添加红色参数[root@nginx-1 nginx]# grep -v "#"conf/nginx.conf|grep proxyproxy_pass http://proxy_set_header Host $proxy_set_header X-Forwarded-For $remote_注:X-Forwarded-For(XFF)是用来识别通过HTTP代理或负载均衡方式连接到Web服务器的客户端最原始的IP地址的HTTP请求头字段。4.3修改真实服务器的nginx.conf上修改日志参数 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 logs/access.取消上面内容前面的#号,不然系统还会按默认的日志格式输出检查语法并重启,只要修改了配置文件,就需要重启服务/application/nginx/sbin/nginx -t/application/nginx/sbin/nginx -s reload4.4日志WEBIP测试:[root@Client ~]# curl bbs.etiantian.org240-bbs[root@RS2 nginx]# tail logs/access.log10.0.0.239 - - [09/Sep/:11 +0800]"GET / HTTP/1.0" 200 8 "-" "curl/7.15.5(x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3libidn/0.6.5"10.0.0.239 - - [09/Sep/:01 +0800]"GET / HTTP/1.0" 200 8 "-" "curl/7.15.5(x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3libidn/0.6.5"10.0.0.239 - - [09/Sep/:51 +0800]"GET / HTTP/1.0" 200 8 "-" "curl/7.15.5(x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3libidn/0.6.5" "10.0.0.238"已经在日志中加入了真实客户端地址!5、根据扩展名实现服务器的动静分离模拟RS1为图片存储服务器,RS2为动态内容存储服务器,以bbs域名举例修改nginx proxy上面配置,添加下列配置(红色部分)upstream webserver {server 10.0.0.240:80 weight=3;server 10.0.0.237:80 weight=3;}upstream static_pools {server 10.0.0.240:80 weight=3;}upstream dynamic_pools {server 10.0.0.237:80 weight=3;}server {listen 80;server_name blog.etiantian.location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {proxy_pass http://static_include proxy.}location ~ .*\.(php|php3|php5)$ {proxy_pass http://dynamic_include proxy. #当参数比较多,且参数相同时,可使用包含文件功能,使配置文件显得简洁,该路径配置使用相对路径}location / {proxy_pass http://proxy_set_header Host $proxy_set_header X-Forwarded-For $remote_}}#server {#listen 80;#server_name blog.etiantian.#location / {#proxy_pass http://#proxy_set_header Host $#proxy_set_header X-Forwarded-For $remote_#}#}}proxy.conf文件里参数配置[root@nginx-1 conf]# vi /application/nginx/conf/proxy.conf client_max_body_size 300m;client_body_buffer_size 128k;proxy_connect_timeout 600;proxy_read_timeout 600;proxy_send_timeout 600;proxy_buffer_size 16k;proxy_buffers 4 32k;proxy_busy_buffers_size 64k;proxy_set_header Host $proxy_set_header X-Forwarded-For $remote_参数解释:#允许客户端请求的最大的单个文件字节数client_max_body_size 300m; #缓冲区代理缓冲用户端请求的最大字节数可以理解为先保存到本地再传给用户client_body_buffer_size 128k; #跟后端服务器连接的超时时间_发起握手等候响应超时时间proxy_connect_timeout 600; #连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理proxy_read_timeout 600; #后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据proxy_send_timeout 600; #代理请求缓存区_这个缓存区间会保存用户的头信息以供Nginx进行规则处理_一般只要能保存下头信息即可proxy_buffer_size 16k; #同上告诉Nginx保存单个用的几个Buffer 最大用多大空间proxy_buffers 4 32k; #如果系统很忙的时候可以申请更大的proxy_buffers 官方推荐*2proxy_busy_buffers_size 64k;#获取url头部信息,用于真实服务器区分域名proxy_set_header Host$#用于真实服务器获取客户端IP信息proxy_set_headerX-Forwarded-For $remote_模拟动态静态服务器1)查看blog站点目录[root@RS2 conf]# cat /application/nginx/conf/nginx.conf|grep bbs server_name bbs.etiantian. root /data/html/ #RS1路径也一样2)模拟动态和静态站点目录下文件[root@RS2 conf]# mkdir /data/html/bbs/soft -p[root@RS2 conf]# echo 240-static-blog& /data/html/bbs/soft/3011.jpg#图片放在RS2上[root@RS1 conf]# mkdir /data/html/bbs/soft -p[root@RS1 conf]# echo 237-dynamic-blog& /data/html/bbs/soft/3011.php#php文件放在RS1上3)客户端测试[root@Client ~]# tail -1/etc/hosts10.0.0.239 www.etiantian.org blog.etiantian.org bbs.etiantian.org #bbs域名解析到nginx proxy上[root@Client ~]# curl bbs.etiantian.org/soft/3011.jpg240-static-blog[root@Client ~]# curl bbs.etiantian.org/soft/3011.php237-dynamic-bbs[root@Client ~]# curl bbs.etiantian.org/soft/3011.php237-dynamic-bbs[root@Client ~]# curl bbs.etiantian.org/soft/3011.jpg240-static-blog [root@Client ~]# curl bbs.etiantian.org 240-bbs[root@Client ~]# curl bbs.etiantian.org237-bbs#域名访问还是分担的,但是对于.jpg或.php结尾的后缀,都会转交到专门的服务器上,由此就可以实现动静分离。这是LVS不能实现的。6、http proxy参数proxy_next_upstream语法:proxy_next_upstream [error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off]确定在何种情况下请求将转发到下一个服务器。转发请求只发生在没有数据传递到客户端的过程中。proxy_connect_timeout后端服务器连接的超时时间_发起握手等候响应超时时间proxy_read_timeout连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)proxy_send_timeout后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据proxy_pass这个指令设置被代理服务器的地址和被映射的URIproxy_pass 这个指令设置被代理服务器的地址和被映射的URIproxy_set_header Host $nginx均衡器转发到真实服务器时,不携带请求的url,设置该参数时,就会携带原始urlproxy_set_headerX-Forwarded-For $remote_ 当服务端需要获取客户机ip时,需要加上该选项,会在访问日志中有客户IP记录7、Location指令语法: location [=|~|~*|^~] /uri/ { …}解释:=精确匹配,如果找到,立即停止搜索,并立即处理请求(优先级最高)^~表示uri以某个常规字符串开头,理解为匹配url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。~区分大小写的正则匹配~*不区分大小写的正则匹配/通用匹配,任何请求都会匹配到。@指定一个命名的location,一般只用于内部重定向请求匹配顺序首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。8、upstream参数每个设备的状态参数如下:参数描述weight默认为1,weight越大,负载的权重就越大backup热备配置(RS高可用),当前面激活的RS都失败后会自动启用热备RSmax_fails最大尝试失败的次数,默认为1,0表示禁止偿试。企业场景,2-3次比较合理。fail_timeout失败超时时间,默认是10s,常规业务2-3s比较合理down表示这个server暂时不参与负载注:如果使用ip_hash负载时,后面的状态不能是weight和backup。9、nginx负载均衡调度算法1)轮询(默认)每个请求按时间顺序注意分配到不同的机器,相当于LVS中rr算法,如果后端服务器宕机(默认情况下只检测80端口,如果后端报502,404,403,503,还是会直接返回给用户),则会跳过该服务器,将请求分配给下一个服务器。默认算法2)weight(权重)在指定的轮询的基础上加上权重(默认是rr+weight),权重轮询和访问成正比,权重越大,转发的请求也就越多。可以根据服务器的配置和性能指定权重值大小,可以有效解决新旧服务器分配问题。示例: upstream bakend { server 192.168.0.14weight=10; server 192.168.0.15 weight=10;}3)ip_hash每个请求按访问的Ip的hash结果分配,当新的请求到达时,先将其客户端ip通过哈希算法哈希出一个值,在随后请求客户端Ip的哈希值只要相同,就会被分配至同一个服务器,该调度算法可以解决session问题,但有时会导致分配不均即,无法保证负载均衡。提示:必须是最前端的服务器,后端也必须直接接应用服务器示例: upstream engine {server 192.168.18.211:80;server 192.168.18.212:80;server 192.168.18.213:80ip_}4)fair(第三方)按照后端服务器的响应时间来分配请求,响应时间短的优先分配。示例:upstream backend { server server1; server server2;}5)url_hash(第三方)按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法。示例:upstream engine {server squid1:3128;server squid2:3128;hash $request_hash_method crc32;}url_hash在缓存服务器组中将非常有用本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)404错误 中国站长 会员中心Nginx 502错误的几种解决方法_服务器应用_Linux公社-Linux系统门户网站
你好,游客
Nginx 502错误的几种解决方法
来源:Linux社区&
作者:Linux
Nginx 502 Bad Gateway错误是FastCGI有问题,造成NGINX 502错误的可能性比较多。将网上找到的一些和502 Bad Gateway错误有关的问题和排查方法列一下,先从FastCGI配置入手:
1.FastCGI进程是否已经启动
2.FastCGI worker进程数是否不够运行 netstat -anpo | grep “php-cgi” | wc -l 判断是否接近FastCGI进程,接近配置文件中设置的数值,表明worker进程数设置太少
3.FastCGI执行时间过长根据实际情况调高以下参数值fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;
4.FastCGI Buffer不够nginx和apache一样,有前端缓冲限制,可以调整缓冲参数fastcgi_buffer_size 32k;fastcgi_buffers 8 32k;
5.Proxy Buffer不够如果你用了Proxying,调整proxy_buffer_size& 16k;proxy_buffers&&&&& 4 16k;
6.https转发配置错误正确的配置方法server_location /myproj/repos {set $fixed_destination $http_if ( $http_destination ~* ^https(.*)$ ){set $fixed_destination http$1;}proxy_set_header Host $proxy_set_header X-Real-IP $remote_proxy_set_header Destination $fixed_proxy_pass http://subversion_}
当然,还要看你后端用的是哪种类型的FastCGI,我用过的有php-fpm,流量约为单台机器40万PV(动
相关资讯 & & &
& (10/08/:17)
& (08/25/:05)
& (11/09/:48)
& (09/23/:56)
& (08/15/:22)
图片资讯 & & &
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款使用nginx proxy 为什么总是502了_百度知道
使用nginx proxy 为什么总是502了
我有更好的答案
一台一台的去掉. 你最好是增加几个后台节点服务器502你的后面的应用服务器出问题了,排除法
为您推荐:
nginx的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 nginx proxy buffers 的文章

 

随机推荐