Nginx Web 服務及負載均衡服務

一、Nginx web 服務

1、http協議包含很多功能。
上網的×××w是http功能之一。
×××w服務默認端口80,OSI 第七層 應用層協議。
https 默認端口443,加密的http服務。

2、實現×××w服務的常用web軟件。
產品:nginx,apache(解決靜態web軟件)

3、經典流行的web組合。
lamp(linux apache mysql php) ===》經典
lnmp(linux nginx mysql php)====》國內流行

4、nginx(engine x)介紹
nginx,×××w服務軟件,俄羅斯人開發,開源的×××w服務軟件,一共780k,c語言開發的。
nginx本身是一款靜態(html,js,css,jpg等)的×××w軟件,不能解析動態的php,jsp,do。

5、nginx服務從大的方面的功能:
a. ×××w web服務,郵件服務,郵件代理
b.負載均衡(反向代理proxy)
c.web cache(web緩存),相當於squid(CDN主要使用squid)。

6、nginx特點:
最大的特點:靜態小文件(低於1M),支持高併發,同時佔用的資源很少。3w併發,10個進程,消耗150M。
1)配置簡單,靈活,輕量。
2)支持高併發(靜態小文件),靜態幾萬的併發。
3)佔用資源少。2w併發,開10個線程服務,內存消耗幾百M。
4)功能種類比較多(web,cache,proxy),每一個功能都不是特別強。
5)支持epoll模型。使得nginx可以支持高併發。apache使用select模型。
6)nginx可以配合動態服務(fastcgi接口)。
7)利用nginx可以對ip限速,可以限制連接數。

它所具備的其他×××w服務特性如下:
支持基於名字、端口以及IP的多虛擬主機站點。
支持rewrite模塊,支持URL重寫及正則表達式匹配。
支持基於客戶端IP地址和HTTP基本認證的訪問控制。
支持http響應速率限制。
支持同一IP地址的併發連接或請求數限制。

7、nginx的應用場景:
1)提供靜態服務(圖片,視頻服務),另一個lighttpd。併發:幾萬併發。
html,js,css,.flv,jpg,gif等。類似lighttpd。
2)提供動態服務,nginx+fastcgi的方式運行php,jsp。動態併發:500-1500.
apache+php,lighttpd+fcgi php。
3)提供反向代理(proxy)服務,或稱爲負載均衡。
日PV2000W以下,併發1萬以下,都可以直接用nginx做反向代理。
軟件:haproxy,硬件:F5,A10
4)提供緩存服務。類似squid,varnish,ats。

8、nginx支持虛擬主機:
一個server標籤段就是一個虛擬主機。
a、基於域名的虛擬主機,通過域名來區分虛擬主機
==>應用:外部網站**
b、基於端口的虛擬主機,通過端口來區分虛擬主機
==>應用:公司內部網站,網站的後臺

c、基於IP的虛擬主機,幾乎不用。不支持ifconfig別名,配置文件可以。

9、安裝Nginx:(http://nginx.org)
a、首先安裝PCRE
Pcre全稱(Perl Compatible Regular Expressions),中文perl兼容正則表達式,官方網站:http://×××w.pcre.org/
HTTP rewrite module requires the PCRE library。
安裝: rpm -qa pcre pcre-devel
yum install pcre pcre-devel -y
rpm -qa pcre pcre-devel
b、然後安裝OpenSSL
SSL modules require the OpenSSL library。
安裝: yum install openssl-devel -y
c、安裝nginx

    mkdir -p /home/oldboy/tools
    cd /home/oldboy/tools
    wget -q http://nginx.org/download/nginx-1.6.3.tar.gz    # -q 不顯示輸出
    useradd nginx -s /sbin/nologin -M
    tar xf nginx-1.6.3.tar.gz
    cd nginx-1.6.3
    ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module    
   ` # --prefix=/application/nginx-1.6.3/ 設置安裝路徑`
     ` --with-http_stub_status_module 加密的ssl模塊 --with-http_ssl_module 狀態模塊`
    make
    make install
    ln -s /application/nginx-1.6.3 /application/nginx  # 創建軟鏈接
    ls -l /application/nginx/

啓動nginx服務: /application/nginx/sbin/nginx
查看配置信息(怎麼編譯的):/application/nginx/sbin/nginx -V
查看日誌文件:cat /var/log/messages
查看nginx下的日誌文件:cat /application/nginx/logs/error.log
[root@lnmp02 nginx]# ls -l|grep -v temp
總用量 36
drwxr-xr-x 2 root root 4096 12月 18 09:56 conf # 配置文件的目錄
drwxr-xr-x 2 root root 4096 12月 18 09:56 html # 默認網站目錄(站點)
drwxr-xr-x 2 root root 4096 12月 18 10:07 logs # 錯誤,訪問日誌
drwxr-xr-x 2 root root 4096 12月 18 09:56 sbin # 啓動命令

10、基於域名的虛擬主機配置步驟:
×××w.etiantian.org ====>html/×××w,瀏覽域名顯示×××w.etiantian.org
bbs.etiantian.org====>html/bbs,瀏覽域名顯示bbs.etiantian.org
a、配置nginx.conf

    [root@lnmp02 conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  ×××w.etiantian.org;
        location / {
            root   html/×××w;
            index  index.html index.htm;
        }
    }
        server {
                listen       80;
                server_name  bbs.etiantian.org;
                location / {
                    root   html/bbs;
                    index  index.html index.htm;
            }                                                                
        }
}

將配置文件裏所有的註釋行都過濾出去:
[root@lnmp02 conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
b、創建站點目錄

    [root@lnmp02 conf]# mkdir ../html/{×××w,bbs} -p
    [root@lnmp02 conf]# echo "×××w.etiantian.org" > ../html/×××w/index.html
    [root@lnmp02 conf]# echo "bbs.etiantian.org" > ../html/bbs/index.html

c、檢查語法,重新加載nginx

    [root@lnmp02 conf]# /application/nginx/sbin/nginx -t
    [root@lnmp02 conf]# /application/nginx/sbin/nginx -s reload

d、配置hosts,測試:
linux client:

       [root@lnmp02 conf]# tail -1 /etc/hosts
       192.168.153.135 ×××w.etiantian.org bbs.etiantian.org    # 添加這一行
       [root@lnmp02 conf]# curl ×××w.etiantian.org           # 測試
       ×××w.etiantian.org
       [root@lnmp02 conf]# curl bbs.etiantian.org
       bbs.etiantian.org
windows:
  C:\Windows\System32\drivers\etc\hosts
  添加這一行:192.168.153.135 ×××w.etiantian.org bbs.etiantian.org  # Ctrl + s 保存
  瀏覽器裏訪問×××w.etiantian.org bbs.etiantian.org域名。 

11、利用include功能優化Nginx的配置文件。
a、配置nginx.conf文件``

   [root@lnmp02 conf]# cat nginx.conf
   worker_processes  1;
   events {
   worker_connections  1024;
   }
   http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        #nginx vhosts config
        include extra/×××w.conf;
        include extra/bbs.conf;
        include extra/blog.conf;
   }

b、創建extra目錄然後在目錄下對應的文件添加配置。

    [root@lnmp02 conf]# mkdir extra
    [root@lnmp02 conf]# sed -n "10,17p" nginx.conf.ori.1 >extra/×××w.conf
    [root@lnmp02 conf]# sed -n "18,25p" nginx.conf.ori.1 >extra/bbs.conf
    [root@lnmp02 conf]# sed -n "26,33p" nginx.conf.ori.1 >extra/blog.conf

c、重新加載nginx服務
` [root@lnmp02 conf]# ../sbin/nginx -s reload `

12、Nginx別名作用及配置實戰。
a、在extra/×××w.conf下添加別名。

  [root@lnmp02 conf]# cat extra/×××w.conf ``
    server {
        listen       80;``
        server_name  ×××w.etiantian.org etiantian.org;
        location / {
            root   html/×××w;
            index  index.html index.htm;
        }
    }

b、在linux下/etc/hosts下做DNS解析。

        [root@lnmp02 conf]# tail -1 /etc/hosts
        192.168.153.135 ×××w.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org

c、重加加載nginx服務。

13、Nginx狀態信息配置實戰。
a、查看狀態配置信息``

    [root@lnmp02 conf]# ../sbin/nginx -V
    nginx version: nginx/1.6.3
    built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 
    TLS SNI support enabled
    configure arguments: --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-  http_stub_status_module --with-http_ssl_module

b、生成狀態配置,並增加狀態配置參數。

cat >>/application/nginx/conf/extra/status.conf<<EOF
##status 
server {
       listen 80;
       server_name status.etiantian.org;
       location / {
          stub_status on;
          access_log off;
       }
    }
EOF

c、在配置文件裏寫入狀態的文件名,並配置DNS解析,最後重新加載。

[root@lnmp02 conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    #nginx vhosts config
        include extra/×××w.conf;
        include extra/bbs.conf;
        include extra/blog.conf;
        include extra/status.conf;
 }   

14、一、訪問日誌作用及格式。
a、找出日誌配置

         [root@lnmp02 conf]# sed -n "21,23p" nginx.conf.default 
         #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
         #                  '$status $body_bytes_sent "$http_referer" '
         #                  '"$http_user_agent" "$http_x_forwarded_for"';
b、將上面的日誌配置放到nginx.conf主配置裏。````
        [root@lnmp02 conf]# cat nginx.conf
        worker_processes  1;
        events {
        worker_connections  1024;
        }
        http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
       #nginx vhosts config
        include extra/×××w.conf;
        include extra/bbs.conf;
        include extra/blog.conf;
        include extra/status.conf;
       }
 二、記錄日誌
   a、默認配置:access_log logs/access.log combined
                           訪問日誌       日誌文件             格式
        將配置分別放到extra/×××w.conf extra/bbs.conf extra/blog.conf文件下。
            [root@lnmp02 conf]# cat extra/×××w.conf                
            server {
                   listen       80;
                   server_name  ×××w.etiantian.org etiantian.org;
                   location / {
                   root   html/×××w;
                  index  index.html index.htm;
            }
                  access_log logs/access_×××w.log main;
            }        #在網頁上訪問可以查看日誌的實時變化:tail -f ../logs/access_×××w.log
  b、訪問日誌優化及工作日誌輪詢。
        [root@lnmp02 scripts]# cat cut_nginx_log.sh 
#########################################################################
# File Name: cut_nginx_log.sh
# Author: yushanshuai
# mail: [email protected]
# Created Time: 2017年12月21日 星期四 07時38分10秒
#########################################################################
#!/bin/bash
Dateformat=`date +%Y%m%d`
Basedir="/application/nginx"``
Nginxlogdir="$Basedir/logs"
Logname="access×××w"
[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1
[ -f ${Logname}.log ]||exit 1
/bin/mv ${Logname}.log ${Dateformat}${Logname}.log
$Basedir/sbin/nginx -s reload

然後放到定時任務裏:規定每天00點生成一個日誌。(查看:ll /application/nginx/logs/)

[root@lnmp02 scripts]# crontab -l``
     echo yushanshuai >>/server/log/oldboy.txt
#cron job for ett by oldboy 2017-6-30
00 09,14   6,0 /bin/sh /server/script/oldboy.sh >/dev/null 2>&1
00 /2    /bin/sh /server/scripts/tar.sh >/dev/null 2>&1
################nginx log
00 00    /bin/sh /server/scripts/cutnginxlog.sh & >/dev/null

15、nginx rewrite 301跳轉實戰案例
a、將配置放到配置文件裏。

  ``  [root@lnmp02 conf]# cat extra/×××w.conf 
    server {
            listen       80;
            servername etiantian.org;
            rewrite ^/(.) http://×××w.etiantian.org/$1 permanent;
    }
    server {
        listen       80;
        servername  ×××w.etiantian.org;
        location / {
            root   html/×××w;
            index  index.html index.htm;
        }
            accesslog logs/access×××w.log main;
    }

b、重新加載後,查看:curl etiantian.org -I*

二、Nginx 負載均衡服務

1、安裝nginx(上面有詳細過程)
2、負載均衡需要哪些模塊:
1)、upstream模塊(定義有哪些節點,調度算法是什麼,調度算法默認是輪詢。定義節點池)
ngx_http_upstream_module
2)、http_proxy模塊(負責請求的轉發,用戶訪問先找到它。調用節點池)
proxy_pass 發給定義好的指定的upstream名字。
3、配置

[root@lb01 conf]# cat nginx.conf
worker_processes  1;
events {
        worker_connections  1024;
}
http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';

upstream bbs_server_pools{
# ip_hash;                               ##########默認是輪詢,加上是保持會話,只在一臺web服務器上顯示。
   server 192.168.153.135:80; # weight=3;   ########weight是權重,=3,是3:1輪詢。
   server 192.168.153.133:80;
}
#include extra/lb_www.conf;
        include extra/lb_bbs.conf;
#include extra/lb_blog.conf;
}
[root@lb01 conf]# cat extra/lb_bbs.conf 
server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
                proxy_pass http://bbs_server_pools;
                proxy_set_header Host      $host;    ########訪問bbs就會指定域名bbs。
                proxy_set_header X-Forwarded-For $remote_addr;  #######$remote_addr 這是客戶端的地址,將客戶端的地址傳給 X-Forwarded-For ,X-Forwarded-For 通過反向代理傳給web服務器的功能。
        }
}   

檢查:(用mysql-server服務器作爲客戶端檢查)
1、做一下DNS解析。

[root@mysql-server ~]# cat /etc/hosts
127.0.0.1       localhost.localdomain   localhost.localdomain   localhost4    localhost4.localdomain4 mysql-server
::1     localhost.localdomain   localhost.localdomain   localhost6   localhost6.localdomain6  localhost       mysql-server
192.168.153.141 www.etiantian.org bbs.etiantian.org blog.etiantian.org

2、a、輪詢(默認)

[root@mysql-server ~]# for n in `seq 10`;do curl bbs.etiantian.org/index.html;sleep 1;done
bbs.etiantian.org 133
bbs.etiantian.org 135
bbs.etiantian.org 133
bbs.etiantian.org 135
bbs.etiantian.org 133
bbs.etiantian.org 135
bbs.etiantian.org 133
bbs.etiantian.org 135
bbs.etiantian.org 133
bbs.etiantian.org 135
   b、會話保持(ip_hash)
[root@mysql-server ~]# for n in `seq 10`;do curl bbs.etiantian.org/index.html;sleep 1;done
bbs.etiantian.org 135
bbs.etiantian.org 135
bbs.etiantian.org 135
bbs.etiantian.org 135
bbs.etiantian.org 135
bbs.etiantian.org 135
bbs.etiantian.org 135
bbs.etiantian.org 135
bbs.etiantian.org 135
bbs.etiantian.org 135
      c、權重輪詢(weight=3  3:1)
[root@mysql-server ~]# for n in `seq 10`;do curl bbs.etiantian.org/index.html;sleep 1;done
bbs.etiantian.org 135
bbs.etiantian.org 135
bbs.etiantian.org 133
bbs.etiantian.org 135
bbs.etiantian.org 135
bbs.etiantian.org 135
bbs.etiantian.org 133
bbs.etiantian.org 135
bbs.etiantian.org 135
bbs.etiantian.org 135
   d、X-Forwarded-For(返回客戶端的ip地址到後端服務器)
[root@lnmp02 bbs]# tail -1 /application/nginx/logs/access_bbs.log 
192.168.153.141 - - [02/Jan/2018:14:37:25 +0800] "GET /index.html HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "192.168.153.130" 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章