网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

nginx反向代理概述及部署-创新互联

下文给大家带来nginx反向代理概述及部署,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用创新互联在行业内累计的经验来做一个解答。

成都创新互联公司是一家专业提供达孜企业网站建设,专注与网站设计制作、成都网站建设、HTML5、小程序制作等业务。10年已为达孜众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。

1.反向代理概述

反向代理(Reverse Proxy)方式是指以代理云服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

环境准备:

主机名IP地址角色系统
web-node1.cometh0:192.168.90.201web-node1节点CentOS7.2
web-node2.cometh0:192.168.90.202web-node2节点CentOS7.2
lb-node1.cometh0:192.168.90.203Nginx反向代理CentOS7.2

2.Node节点部署

在两台web-node节点中均使用Yum安装一个Apache用于做真实机,监听8080端口

web-node1.com部署

nginx反向代理概述及部署

[root@web-node1 ~]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
[root@web-node1 ~]# yum install -y gcc glibc gcc-c++ make screen tree lrzsz
##部署web-node1 httpd服务
[root@web-node1 ~]# yum install -y httpd
[root@web-node1 ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
[root@web-node1 ~]# systemctl start httpd
[root@web-node1 ~]# echo "web-node1.com" > /var/www/html/index.html
[root@web-node1 ~]# curl  http://192.168.90.201:8080/
web-node1.com

web-node2.com部署

[root@web-node1 ~]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
[root@web-node1 ~]# yum install -y gcc glibc gcc-c++ make screen tree lrzsz
##部署web-node2 httpd服务
[root@web-node1 ~]# yum install -y httpd
[root@web-node1 ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
[root@web-node1 ~]# systemctl start httpd
[root@web-node1 ~]# echo "web-node2.com" > /var/www/html/index.html
[root@web-node1 ~]# curl  http://192.168.90.202:8080/
web-node2.com

3.反向代理部署

Nginx 源码编译安装,使其支持4层,并监听80端口

  1. [root@lb-node1~]# useradd-s/sbin/nologin-M www

  2. [root@lb-node1~]# cd/usr/local/src/

  3. [root@lb-node1 src]# wget http://nginx.org/download/nginx-1.10.2.tar.gz

  4. [root@lb-node1 src]# tar xf nginx-1.10.2.tar.gz

  5. [root@lb-node1 src]# cd nginx-1.10.2

  6. [root@lb-node1 nginx-1.10.2]#./configure--prefix=/usr/local/nginx-1.10.2 \

  7. --user=www--group=www--with-http_ssl_module \

  8. --with-http_stub_status_module--with-file-aio--with-stream

  9. [root@lb-node1 nginx-1.10.2]# make&& make install

  10. [root@web-node1~]# ln-s/usr/local/nginx-1.10.2/ /usr/local/nginx

  11. ## 测试配置并启动Nginx

  12. [root@lb-node1~]#/usr/local/nginx/sbin/nginx-t

  13. nginx: the configuration file/usr/local/nginx-1.10.2/conf/nginx.conf syntaxis ok

  14. nginx: configuration file/usr/local/nginx-1.10.2/conf/nginx.conf testis successful

  15. [root@lb-node1~]#/usr/local/nginx/sbin/nginx

3.1配置Nginx7层反向代理
1.配置Nginx反向代理

  1. ##http段配置

  2.   upstream web-cluster{

  3.     # ip_hash;

  4.     server192.168.90.201:8080 weight=1 max_fails=3 fail_timeout=3;

  5.     server192.168.90.202:8080 weight=1 max_fails=3 fail_timeout=3;

  6.   }

  7.   server{

  8.     listen80;

  9.     server_name192.168.90.203;

  10.   location/{

  11.     proxy_pass http://web-cluster;

  12.     include proxy.conf;

  13.     }

  14.   }

测试代理

  1. [root@lb-node1~]# curl http://192.168.90.203/

  2. web-node1.com

  3. [root@lb-node1~]# curl http://192.168.90.203/

  4. web-node2.com

  5. [root@lb-node1~]# curl http://192.168.90.203/

  6. web-node1.com

  7. [root@lb-node1~]# curl http://192.168.90.203/

  8. web-node2.com

2.通过分组方式,以及User-agent实现不同代理

  1. #http段配置

  2.     upstreamstatic-cluster{

  3.         server192.168.90.201:8080 weight=1 max_fails=3 fail_timeout=3;

  4.     }

  5.     upstreamdynamic-cluster{

  6.         server192.168.90.202:8080 weight=1 max_fails=3 fail_timeout=3;

  7.     }

  8.     upstreamdefault-cluster{

  9.         server192.168.90.202:8080 weight=1 max_fails=3 fail_timeout=3;

  10.     }

  #需要配置本地host解析测试
        server {
                listen 80;
                server_name nginx.jiege.com;
        location / {
        if ($http_user_agent ~* "Firefox"){
                    proxy_pass http://static-cluster;
                    }
        if ($http_user_agent ~* "Chrome") {
            proxy_pass http://dynamic-cluster;
            }
        proxy_pass http://default-cluster;
            }
     }

测试分组

##默认浏览器交给default处理[root@lb-node1 ~]# curl http://nginx.jiege.com
 web-node2.com
 火狐浏览器交给static-cluster处理
 谷歌浏览器交给dynamic-cluster处理
 

 
 配置ssh以及msql反向代理
stream {
        upstream ssh_proxy {
        hash $remote_addr consistent;
        server 192.168.90.201:22;
    }
        upstream mysql_proxy {
        hash $remote_addr consistent;
        server 192.168.90.202:3306;
        }
    server {
    listen 2222;
    proxy_connect_timeout 1s;
        proxy_timeout 300s;
        proxy_pass ssh_proxy;
    }
    server {
    listen 3333;
    proxy_connect_timeout 1s;
        proxy_timeout 300s;
        proxy_pass mysql_proxy;
        }
  }

2222端口代理至于node1的SSH、3333端口代理至于node2的MYSQL

  1. ## 测试连接ssh

  2. [root@lb-node1~]# ssh-p2222 root@192.168.90.203

  3. root@192.168.90.203's password:

  4. Last login: Wed Oct 19 11:53:04 2016 from 192.168.80.143

  5. [root@web-node1 ~]#

  6. ## 测试连接mysql

  7. [root@lb-node1 ~]# mysql -h392.168.90.203 -uroot -p1 -P3333

  8. Welcome to the MariaDB monitor.  Commands end with ; or \g.

  9. Your MariaDB connection id is 273

  10. Server version: 5.5.47-MariaDB MariaDB Server

  11. Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  13. MariaDB [(none)]>

看了以上关于nginx反向代理概述及部署,如果大家还有什么地方需要了解的可以在创新互联行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,创新互联技术工程师在行业内拥有十几年的经验了。

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前名称:nginx反向代理概述及部署-创新互联
当前地址:http://bjjierui.cn/article/ccechh.html

其他资讯