资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Nginx中怎么实现负载均衡与反向代理

今天就跟大家聊聊有关Nginx 中怎么实现负载均衡与反向代理,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联是一家集网站建设,榆阳企业网站建设,榆阳品牌网站建设,网站定制,榆阳网站建设报价,网络营销,网络优化,榆阳网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

一、轮询配置

#定义后端服务器组
    upstream nginx-test{
        server 192.168.0.128;
        server 192.168.0.127;
    }
    server {
        listen       8080;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        root   "G:/phpstudy/nginx/html";
        
        location / {
           index  index.html index.htm index.php l.php;
           autoindex  on;
           proxy_pass http://nginx-test;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
        location ~ \.php(.*)$  {
            fastcgi_pass   127.0.0.1:9001;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
        
    }

二、轮询加权配置

#定义后端服务器组
    upstream nginx-test{
        server 192.168.0.128 weight=2;
        server 192.168.0.127;
        server 192.168.0.126 backup; # 备份服务器,其他服务器宕机后启动
    }
    server {
        listen       8080;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        root   "G:/phpstudy/nginx/html";
        
        location / {
           index  index.html index.htm index.php l.php;
           autoindex  on;
           proxy_pass http://nginx-test;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

三、IP Hash配置

#定义后端服务器组
    upstream nginx-test{
        ip_hash;
        server 192.168.0.128;
        server 192.168.0.127;
        server 192.168.0.126;
    }
    server {
        listen       8080;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        root   "G:/phpstudy/nginx/html";
        
        location / {
           index  index.html index.htm index.php l.php;
           autoindex  on;
           proxy_pass http://nginx-test;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }    
    }

四、负载均衡与反向代理区别

1、负载均衡需要通过反向代理来实现
2、反向代理就是指nginx作为前端服务器,将请求转发到后端,再将后端服务器的结果,返回给客户端
它在中间做了一个代理服务器的角色
3、负载均衡对反向代理增加了一些策略,因为后端是多台服务器,nginx会根据设定的策略将请求转发给一个相对空闲的服务器,对负载进行分流,减轻服务器压力

Nginx 中怎么实现负载均衡与反向代理

五、反向代理配置

#定义后端服务器组
    upstream nginx-test{
        server 192.168.0.127;
    }
    server {
        listen       8080;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        root   "G:/phpstudy/nginx/html";
        
      #代理配置参数
        proxy_connect_timeout 180;
        proxy_send_timeout 180;
        proxy_read_timeout 180;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarder-For $remote_addr;
        
        location / {
          proxy_pass http://nginx-test;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

看完上述内容,你们对Nginx 中怎么实现负载均衡与反向代理有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


当前标题:Nginx中怎么实现负载均衡与反向代理
转载源于:http://cdkjz.cn/article/posgdj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220