美文网首页
Nginx 负载均衡如何配置,高并发报502如何返回正常信息?

Nginx 负载均衡如何配置,高并发报502如何返回正常信息?

作者: 程序猿帅帅 | 来源:发表于2020-11-18 14:20 被阅读0次

负载均衡入口服务器配置

入口

upstream mServer{

server 149.129.114.100:8080 weight=2 fail_timeout=30s max_fails=0;

server 149.129.75.101:8080 weight=4 fail_timeout=30s max_fails=0;

server 149.129.76.102:8080 weight=4 fail_timeout=30s max_fails=0;

}

server {

listen 80;

server_name www.xxx.com;

#charset koi8-r;

#access_log logs/host.access.log main;

#遇到502、503、504等状态码时,我们可以改变将之成200,这样在调用接口时,就可以正常的返回信息,给用户提供良好的交互环境。

error_page 502 503 504 =200 /dealwith_502?callback=$arg_callback;

location /dealwith_502{

#下面这些header,是为了防止跨域问题

add_header 'Content-Type' 'application/json; charset=utf-8';

add_header 'Access-Control-Allow-Origin' '*';

add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept';

add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT';

set $ret_body '{ "status": 0, "info": "系统繁忙,请稍后访问", "data": [] }';

if ( $arg_callback != "" )

{

return 200 'try{$arg_callback($ret_body)}catch(e){}';

}

return 200 $ret_body;

}

location / {

root html;

proxy_pass http://mServer;

proxy_set_header Host $host;

index index.php index.html index.htm;

}

}

服务器1

server

{

listen 8080;

#listen [::]:80;

server_name 149.129.114.100;

index home.html index.htm index.php default.html default.htm default.php;

root /home/wwwroot/xxx;

#error_page 404 /404.html;

error_page 502 503 504 =200 /dealwith_502?callback=$arg_callback;

location /dealwith_502{

add_header 'Content-Type' 'application/json; charset=utf-8';

add_header 'Access-Control-Allow-Origin' '*';

add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept';

add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT';

set $ret_body '{ "status": 0, "info": "系统繁忙,请稍后访问", "data": [] }';

if ( $arg_callback != "" )

{

return 200 'try{$arg_callback($ret_body)}catch(e){}';

}

return 200 $ret_body;

}

location / {

if (!-e $request_filename) {

rewrite ^(.*)$ /index.php/$1 last;

break;

}

}

location ~ [^/]\.php(/|$)

{

# comment try_files $uri =404; to enable pathinfo

#try_files $uri =404;

fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_index index.php;

include fastcgi.conf;

include pathinfo.conf;

fastcgi_param PHP_ADMIN_VALUE "open_basedir=/home/wwwroot/:/tmp/:/proc/";

}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 30d;

}

location ~ .*\.(js|css)?$

{

expires 12h;

}

}

其他服务器配置和服务器1的配置是相同的,这样就可以避免高并发的出现。我做的项目就是这样处理的。

相关文章

  • Nginx 负载均衡如何配置,高并发报502如何返回正常信息?

    负载均衡入口服务器配置 入口 upstream mServer{ server 149.129.114.100:8...

  • nginx的upstream几种轮询方式

    上一篇文章中介绍了如何使用nginx配置负载均衡,nginx如何实现反向代理负载均衡。接下来将来介绍下负载均衡的几...

  • Nginx负载均衡小知识

    Nginx 负载均衡配置Nginx 重试次数限制Nginx 超时重试 Nginx 负载均衡 负载均衡策略 roun...

  • nginx相关资料

    nginx负载均衡及配置 服务器Nginx并发压力测试工具AB测试WordPress站点 如何对Nginx日志文件...

  • Nginx + Spring Boot如何实现负载均衡

    前言 本篇文章主要介绍的是Nginx如何实现负载均衡。 负载均衡介绍 在介绍Nginx的负载均衡实现之前,先简单的...

  • Nginx负载均衡配置

    基于轮询(Round Robin)的负载均衡配置 Nginx的负载均衡策略默认就是轮询。 Nginx负载均衡策略支...

  • centos6 php nginx负载均衡并代转主服务器

    centos6 nginx负载均衡并代转主服务器(作为中转服也可以作为代理服) 参考地址(nginx负载均衡配置...

  • HTTP 负载均衡

    编译自:load-balancer 本文讲述如何配置 nginx 为 HTTP 负载均衡调度器。 目录: 概述 将...

  • Nginx作为负载均衡服务

    Nginx负载均衡 应用情况 Nginx作为负载均衡主要有以下几点理由: 高并发连接 内存消耗少 配置文件非常简单...

  • nginx

    nginx的配置、虚拟主机、负载均衡和反向代理一nginx的配置、虚拟主机、负载均衡和反向代理二nginx的配置、...

网友评论

      本文标题:Nginx 负载均衡如何配置,高并发报502如何返回正常信息?

      本文链接:https://www.haomeiwen.com/subject/kydciktx.html