美文网首页
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如何返回正常信息?

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