Nginx負載均衡靜態資源代理

總體流程:1.搭建tomcat項目集羣(默認完成) 2.安裝nginx須要的庫 3.安裝Nginx並修改配置文件 4.啓動測試javascript

1.1.1. 安裝Nginx

1.1.1.1. 安裝環境:

安裝pcre庫php

yum -y install pcre-develcss

安裝zlib庫html

yum install -y zlib-develjava

安裝openssl庫node

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

編譯安裝openssl:git

1.上傳openssl壓縮包緩存

 

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

2.解壓壓縮包

         [root@-01 ~]# tar –zxvf openssl-1.0.1t.tar.gz

         [root@-01 ~]#cd openssl-1.0.1t

3.編譯安裝

         設置安裝參數

[root@-01 openssl-1.0.1t]# ./config

         編譯並安裝

[root@-01 nginx-1.7.7]#make

[root@-01 nginx-1.7.7]#make install

準備安裝Nginx:

上傳Nginx

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

1.1.1.2. 解壓

解壓

[root@-01 ~]# tar -zxvf nginx-1.10.2.tar.gz

進入解壓文件夾

[root@-01 ~]# cd nginx-1.10.2

1.1.1.3. 編譯安裝

設置安裝參數

[root@-01 nginx-1.10.2]#./configure --prefix=/usr/local/nginx --with-http_ssl_module(這個是採用https方式訪問須要安裝的模塊,用http不須要安裝)

編譯並安裝

[root@itcast-01 nginx-1.10.2]# make

編譯成功效果

[root@itcast-01 nginx-1.10.2]# make install

openssl生成測試CA證書:

# 一、首先,進入你想建立證書和私鑰的目錄,例如: cd /usr/local/nginx/ # 二、建立服務器私鑰,命令會讓你輸入一個口令: openssl genrsa -des3 -out server.key 2048
[lenin@archer ~]$ openssl genrsa -des3 -out root.key 
Generating RSA private key, 512 bit long modulus
……………..++++++++++++
..++++++++++++
e is 65537 (0×10001)
Enter pass phrase for root.key: ← 輸入一個新密碼
Verifying – Enter pass phrase for root.key: ← 從新輸入一遍密碼

Enter pass phrase for root.key: ← 輸入前面建立的密碼
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [AU]:CN ← 國家代號,中國輸入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 能夠不輸入
Common Name (eg, YOUR name) []: ← 此時不輸入
Email Address []:admin@mycompany.com ← 電子郵箱,可隨意填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 能夠不輸入
An optional company name []: ← 能夠不輸入# 三、建立簽名請求的證書(CSR):
openssl req -new -key server.key -out server.csr


# 四、在加載SSL支持的Nginx並使用上述私鑰時除去必須的口令:
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
# 五、最後標記證書使用上述私鑰和CSR:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

1.1.1.4. 修改nginx.conf文件,實現負載均衡:

需求:1.用戶經過https訪問

     2.實現頁面壓縮gzip

     3.記錄用戶真實ip地址

     4.使用ip-hash方式建立集羣信息,解決session粘滯問題 

     5.nginx管理靜態資源

配置文件以下:(如下參數詳細介紹參考http://www.cnblogs.com/zclzhao/p/5033391.html)

 

#user  nobody;

worker_processes  2;#根據本身系統cpu數定,通常等於核心數或者核心數的兩倍

user  root;#開啓root用戶權限,有時候訪問盤符下資源會報403沒有權限,開啓root能夠解決

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

 

 worker_rlimit_nofile   65535;

events {

    worker_connections  65535;#鏈接數

   use epoll;#牛逼模式

}

 

 

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  120;

 

    server_tokens off;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 128k;
    large_client_header_buffers 4 128k;
    autoindex on;
    tcp_nopush on;
    tcp_nodelay on;

    charset utf-8;

    #開啓壓縮

    gzip  on;

    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

#

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

    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 ~ .*\.(ico|png|jpg|eot|svg|ttf|woff|js|css)$
       {
              #全部靜態文件直接讀取硬盤
              root /root;
              expires 30d; #緩存30天

       }

        location / {

           #配置用戶真實ip,部分虛擬機配置真實ip後報404,可注掉.

            #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.

圖片地址src除去被監聽的域名後能和root後邊的路徑組成被訪問的路徑。

以上配置文件location正則uri僅爲示例。

2.進入location塊root路徑的前提是nginx必需要先監聽到,進入相應的server塊後,因此圖片路徑必定要帶上要監聽的域名。

1.1.1.5. 啓動Nginx

查看安裝文件,conf是配置文件,sbin是啓動目錄

[root@-01 nginx-1.10.2]# cd /usr/local/nginx/

 

進入啓動文件目錄,啓動Nginx

[root@itcast-01 nginx]# cd sbin/

[root@itcast-01 sbin]# ./nginx

查看啓動進程

 

關閉防火牆

[root@itcast-01 sbin]# service iptables stop

訪問測試

 

1.1.1.6. Nginx相關擴充:

訪問流程:在不添加‘可選匹配規則’模塊時,Nginx服務器首先在server塊的多個location塊中搜索是否有標準uri和請求字符串匹配,若是有多個能夠匹配,就記錄匹配度最高的一個。而後,服務器再用location塊中的正則uri和請求字符串匹配,當地一個正則uri匹配成功,就再也不進行搜索,並使用這個location塊處理此請求,若是正則匹配所有失敗,就使用剛纔記錄的匹配度最高的location塊處理此請求。

查看liunx最大鏈接數等狀態

[root@itcast-01]# ulimit –a

 

修改最大進程數

[root@itcast-01]# ulimit -u 65535

 

修改每一個進程可打開的文件數,缺省值是 1024。

[root@itcast-01]# ulimit -n 65535

 

命令:

                     ./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

相關文章
相關標籤/搜索