CentOS中nginx負載平衡和反向 Proxy的搭建

來源:互聯網
上載者:User

1: 修改centos命令列啟動(減少記憶體佔用):
vim /etc/inittab     id:5:initdefault:  --> 修改5為3 
若要介面啟動使用 startx
2:安裝jdk
1)解壓:jdk-7u55-linux-i586.tar.gz
       [root@localhost jdk]# tar -zxvf jdk-7u55-linux-i586.tar.gz
2)複製:[root@localhost jdk]# cp -rf jdk1.7.0_55/ /usr/local/jdk
3)配置環境;[root@localhost bin]# vim /etc/profile
  最後面插入:export JAVA_HOME=/usr/local/jdk/jdk1.7.0_79
            export PATH=$JAVA_HOME/bin:$PATH
4)重新整理設定檔:source /etc/profile

    驗證:java   javac
3:安裝tomcat
    1)解壓:tar -zxvf
    2)授權:chmod u+x/usr/local/tomcats/tomcat1/apache-tomcat-7.0.47/bin
    3)啟動:進入tomcat目錄bin 目錄後: ./startup.sh
    4)開放連接埠:vim  /etc/sysconfig/iptables
    5)關閉防火牆:chkconfig iptables off
    6)重啟防火牆: service iptables restart
    7)修改連接埠號碼:vim conf/server.xml
    8)查看進程:ps aux | grep tomcat
4:安裝nginx
    1)安裝環境: 
              yum -y install gcc-c++
              yum -y install pcre pcre-devel
              yum -y install zlib zlib-devel
              yum -y install openssl openssl-devel

    2)解壓:tar -zxvf nginx-1.8.0.tar.gz
    3)移動: mv  nginx-1.8.0 /usr/local/nginx/
    4)建立臨時目錄:var]# mkdir -p temp/nginx
    5)進入目錄:cd nginx-1.8.0/ 
    6)修改參數:
            ./configure \
            --prefix=/usr/local/nginx \
            --pid-path=/var/run/nginx/nginx.pid \
            --lock-path=/var/lock/nginx.lock \
            --error-log-path=/var/log/nginx/error.log \
            --http-log-path=/var/log/nginx/access.log \
            --with-http_gzip_static_module \
            --http-client-body-temp-path=/var/temp/nginx/client \
            --http-proxy-temp-path=/var/temp/nginx/proxy \
            --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
            --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
            --http-scgi-temp-path=/var/temp/nginx/scgi

    7)編譯安裝:
        make
        make install

    8)啟動: cd /usr/local/nginx/sbin/
         ./nginx
    9)查看進程:ps aux | grep nginx
    10)快速停止:./nginx -s stop
    11)完整停止:./nginx  -s quit  此方式停止步驟是待nginx進程處理任務完畢進行停止。推薦使用
    12)重啟: ./nginx -s quit
            ./nginx 
    13)重新載入設定檔: ./nginx -s reload
5:配置虛擬機器主機:
    1、nginx支援的三種虛擬機器主機的配置:
            基於ip的虛擬機器主機
            基於網域名稱的虛擬機器主機
            基於連接埠的虛擬機器主機

    2、nginx設定檔的結構: 每個service就是一個虛擬機器主機
......
            events{
                ......
            }

            http{
                .......
                server{
                    ......
                }

                server{
                    ......
                }
            }

    3、基於ip的虛擬機器主機配置:
        修改設定檔: vim /usr/local/nginx/nginx-1.8.0/conf/nginx.conf
 server{
                listen 80;
                server_name 192.168.31.88;

                location / {
                    root html;
                    index index.html index.htm;
                }
            }
    4、基於網域名稱的虛擬機器主機配置:
        修改設定檔:vim /usr/local/nginx/nginx-1.8.0/conf/nginx.conf
server{
                listen 80;
                server_name www.nginxdns1.com;

                location / {
                    root html_dns1;
                    index index.html index.htm;
                }
            }

            server{
                listen 80;
                server_name www.nginxdns2.com;

                location / {
                    root html_dns2;
                    index index.html index.htm;
                }
            }

    5、基於連接埠的虛擬機器主機配置:
        修改設定檔:vim /usr/local/nginx/nginx-1.8.0/conf/nginx.conf

        監聽連接埠:netstat -an | grep 80
           server{
                listen 88;
                server_name 192.168.31.88;

                location / {
                    root html_port1;
                    index index.html index.htm;
                }
            }

            server{
                listen 89;
                server_name 192.168.31.88;

                location / {
                    root html_port2;
                    index index.html index.htm;
                }
            }

6、nginx 反向 Proxy:
修改hosts:# nginx反向 Proxy環境測試
            192.168.31.88 www.nginxproxy1.com
            192.168.31.88 www.nginxproxy2.com

開啟不同虛擬機器中的兩台tomcat:192.168.31.88:8080 和 192.168.31.89:8081
修改設定檔:
            #代理tomcat1伺服器
            upstream  tomcat_server1{
                server  192.168.31.89:8081;
            }

            #代理tomcat2伺服器
            upstream tomcat_server2{
                server 192.168.31.88:8080;
            }

            #配置虛擬機器主機:
            server{
                listen 80;
                server_name www.nginxproxy1.com;

                location / {
                    #root html_port1;

                    proxy_pass http://tomcat_server1;
                    index index.html index.htm;
                }
            }

            server{
                listen 80;
                server_name www.nginxproxy2.com;

                location / {
                    #root html_port2;
                    proxy_pass http://tomcat_server2;
                    index index.html index.htm;
                }
            }

7、nginx 負載平衡:
    修改hosts :# nginx負載平衡環境測試
                 192.168.31.88 www.nginxbalance.com

    開啟不同虛擬機器中的兩台tomcat:192.168.31.88:8080 和 192.168.31.89:8081
    修改設定檔:
            #代理tomcat2伺服器
            upstream tomcat_server_pool{
                server 192.168.31.88:8080 weight=1;
                server 192.168.31.89:8081 weight=1;
            }

            #配置虛擬機器主機:
            server{
                listen 80;
                server_name www.nginxbalance.com;

                location / {
                    #root html_port1;

                    proxy_pass http://tomcat_server_pool;
                    index index.html index.htm;
                }
            }

hosts檔案配置:

1:nginx基於網域名稱環境測試
192.168.31.88 www.nginxdns1.com
192.168.31.88 www.nginxdns2.com

2:nginx反向 Proxy環境測試
192.168.31.88 www.nginxproxy1.com
192.168.31.88 www.nginxproxy2.com

3:nginx負載平衡環境測試
192.168.31.88 www.nginxbalance.com

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.