Nginx負載平衡實現https訪問

來源:互聯網
上載者:User

標籤:pst   virt   one   format   推薦   openssl   設定   quit   idt   

整體流程:1.搭建tomcat項目叢集(預設完成) 2.安裝nginx需要的庫 3.安裝Nginx並修改設定檔 4.啟動測試

1.1.1. 安裝Nginx1.1.1.1. 安裝環境:

安裝pcre庫

yum -y install pcre-devel

安裝zlib庫

yum install -y zlib-devel

安裝openssl庫

yum install -y openssl openssl-devel   或者  編譯安裝

編譯安裝openssl:

1.上傳openssl壓縮包

 

按alt+p進入上傳介面,上傳openssl-1.0.1t.tar.gz

2.解壓壓縮包

         [[email protected] ~]# tar –zxvf openssl-1.0.1t.tar.gz

         [[email protected] ~]#cd openssl-1.0.1t

3.編譯安裝

         設定安裝參數

[[email protected] openssl-1.0.1t]# ./config

         編譯並安裝

            [[email protected] nginx-1.7.7]#make

    [[email protected] nginx-1.7.7]#make install

準備安裝Nginx:上傳Nginx

按alt+p進入上傳介面,上傳Nginx

1.1.1.2. 解壓

解壓

[[email protected] ~]# tar -zxvf nginx-1.10.2.tar.gz

進入解壓檔案夾

[[email protected] ~]# cd nginx-1.10.2

1.1.1.3. 編譯安裝

設定安裝參數

[[email protected] nginx-1.10.2]#./configure --prefix=/usr/local/nginx --with- http_ssl_module

編譯並安裝

[[email protected] nginx-1.10.2]# make

[[email protected] nginx-1.10.2]# make install

openssl產生測試CA認證:詳見參考資料

1.1.1.4. 修改nginx.conf檔案,實現負載平衡:

需求:1.使用者通過https訪問,通過nginx反向 Proxy實現http內部跳轉2.實現頁面壓縮gzip 3.記錄使用者真實ip地址 4.使用ip-hash方式建立叢集資訊,解決session粘滯問題 5.訪問地址為DNS註冊網域名稱 6.nginx管理靜態資源

設定檔如下:

 

#user  nobody;

worker_processes  1;

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

 

 

events {

    worker_connections  1024;

}

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

 

    #log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘

    #                  ‘$status $body_bytes_sent "$http_referer" ‘

    #                  ‘"$http_user_agent" "$http_x_forwarded_for"‘;

    #配置叢集資訊

    upstream xxx {

      ip_hash;

      server xxx.xx.xxx.xx:8083;

      server xxx.xx.xxx.xx:8085;

    }

 

    #access_log  logs/access.log  main;

 

    sendfile        on;

    #tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

    #開啟壓縮

    gzip  on;

 

    server {

        listen       80;

        server_name  localhost;

       

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            root   html;

            index  index.html index.htm;

        }

 

        #error_page  404              /404.html;

 

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

 

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #    proxy_pass   http://127.0.0.1;

        #}

 

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        #location ~ \.php$ {

        #    root           html;

        #    fastcgi_pass   127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        #}

 

        # deny access to .htaccess files, if Apache‘s document root

        # concurs with nginx‘s one

        #

        #location ~ /\.ht {

        #    deny  all;

        #}

}

 

    # another virtual host using mix of IP-, name-, and port-based configuration

    #

    #server {

    #    listen       8000;

    #    listen       somename:8080;

    #    server_name  somename  alias  another.alias;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

 

 

    # HTTPS server

    #

    server {

        listen       443 ssl;

        server_name  www.xxx.com;

 

        ssl_certificate      /usr/local/nginx/server.crt;

        ssl_certificate_key  /usr/local/nginx/server.key;

 

        ssl_session_cache    shared:SSL:1m;

        ssl_session_timeout  5000m;

 

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

        ssl_prefer_server_ciphers  on;

          location ~ \.png$

 {

            root /home;

 

  }

 

        location / {

            #root html;

            #index  index.html index.htm;

           #配置使用者真實ip

            proxy_set_header Host $host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

           #使用之前配置的叢集

            proxy_pass http://xxx;

        }

    }

 

}

注意:1.location塊root的路徑問題:例如:location塊配置如下

         location ~ \.png$

 {

            root /home;

 

  }

項目圖片訪問路徑為/images/a.png,那麼匹配正則後路徑變為/home/images/a.png

以上設定檔location正則uri僅為樣本。

1.1.1.5. 啟動Nginx

查看安裝檔案,conf是設定檔,sbin是啟動目錄

[[email protected] nginx-1.10.2]# cd /usr/local/nginx/

 

 

進入開機檔案目錄,啟動Nginx

[[email protected] nginx]# cd sbin/

[[email protected] sbin]# ./nginx

查看啟動進程

 

 

關閉防火牆

[[email protected] sbin]# service iptables stop

訪問測試

 

1.1.1.6. Nginx相關擴充:

訪問流程:在不添加‘可選匹配規則’模組時,Nginx伺服器首先在server塊的多個location塊中搜尋是否有標準uri和請求字串匹配,如果有多個可以匹配,就記錄匹配度最高的一個。然後,伺服器再用location塊中的正則uri和請求字串匹配,當地一個正則uri匹配成功,就不再進行搜尋,並使用這個location塊處理此請求,如果正則匹配全部失敗,就使用剛才記錄的匹配度最高的location塊處理此請求。

命令:

                     ./nginx

                     ./nginx -s stop

                     ./nginx -s quit

                     ./nginx -s reload

                     ./nginx -s quit:此方式停止步驟是待nginx進程處理任務完畢進行停止。

                     ./nginx -s stop:此方式相當於先查出nginx進程id再使用kill命令強制殺掉進程。

                    

                     查詢nginx進程:

 

                     ps aux|grep nginx

                    

       8.2.6 重啟服務:

             1.先停止再啟動(推薦):

                     對 nginx 進行重啟相當於先停止再啟動,即先執行停止命令再執行啟動命令。如下:

 

                     ./nginx -s quit

                     ./nginx

                     2.重新載入設定檔:

                     當 ngin x的設定檔 nginx.conf 修改後,要想讓配置生效需要重啟 nginx,使用-s reload不用先停止 nginx再啟動 nginx 即可將配置資訊在 nginx 中生效,如下:

                     ./nginx -s reload

       8.2.7 訪問: http://xx.xx.xx.xxx/ 是否成功載入nginx歡迎頁面,如果看到Welcome to nginx!字樣則證明安裝成功

       8.2.8 查看nginx 安裝路徑 whereis nginx

Nginx負載平衡實現https訪問

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.