分佈式系列十三: nginx

nginx偏運維, 不過做爲開發應該瞭解它能作什麼事情, 其做爲技術架構的一部分必不可少css

正向代理和反向代理

正向代理是代理的客戶端, 反向代理是代理的服務端. nginx就是一款能夠做反向代理的web服務器.html

常見的Web服務器

Apache, Nginx,Tomcat,WebLogic, iis, jetty, undertowe, resinnginx

Apache,Nginx 是同一類型的服務器, 均可以提供反向代理功能.web

web服務器按提供的內容能夠劃分爲動態web服務器和靜態web服務器, 靜態web服務器提供靜態的文件內容, 好比html,css,js,image等, 動態web服務器提供動態內容, 如jsp,asp等通過服務器程序運行輸出的頁面.算法

Nginx的安裝

  • tar -zxvf 安裝包
  • ./configure --prefix=/user/nginx 沒有則默認安裝到/user/local/nginx
  • 若是報缺乏pcre包, 須要安裝sudo apt-get install libpcre3 libpcre3-dev
  • 若是缺乏zlib包, 須要安裝sudo apt-get install zlib1g-dev
  • make 以及 make install

命令

  • 啓動: ./nginx -c /user/data/nginx/conf/nginx.conf -c 表示指定nginx.conf文件, 默認爲NGINX-HOME/conf/nginx.conf
  • 命令方式中止:
    ./nginx -s stop
    ./nginx -s quit
    ./nginx -s reload
  • 發送信號的方式中止:
    kill -QUIT pid pid是進程號. 安全中止, 可使正在處理的中止
    kill -TERM pid

進程模型

多進程模型: 主進程fork出一個進程處理請求.nginx的方式.瀏覽器

多線程模型: 可能有併發問題緩存

異步方式: 每一個進程採用異步非阻塞模型處理請求.tomcat

配置

主要分三部分:安全

  • main
work_processes 2; //工做進程數, 通常爲cpu核數
work_cpu_affinity 01 10 
error_log /var/log/nginx/error.log warn; //錯誤日誌
worker_rlimit_nofile 10240; // 最大打開文件數
  • event
event{
    use epoll ; // 選項有 select poll epoll kqueue等
    work_connections 10240; //鏈接數
    accept_mutex off; //off 能夠提升效率
}

http{
    include mime.types;
    default_type application/octet-stream; //默認mime
    charset utf-8;
    access_log off;
    sendfile on;
    gzip on;
    ....
    proxy_temp_path /data/
    proxy_cache_path /data/cache; level=1:2
    inclued /etc/nginx/conf.d/*.conf;
}
  • server

虛擬主機

  • 基於域名

下面配置一個虛擬主機:服務器

server{
    listen 80;
    server_name www.my.com;
    location / {
        root html/domain;
        index index.html index.htm;
    }
}
  • 基於IP
  • 基於端口
server{
    listen 8080;
    server_name localhost;
    location / {
        root html/domain;
        index index.html index.htm;
    }
}

nginx日誌配置

log_format formatName '.....' //日誌格式
access_log logs/logfile.log  formatName //指定日誌路徑和格式

日誌切割

mv access.log access.log.001 而後重啓生成, 能夠寫個運維腳本, 定時執行

location 的語法和匹配規則

= 是精確匹配, 如=/mypage.html 優先級最高(相似servlet規範的url的優先級規則)

通常匹配, 優先級高於正則匹配, 如:/myroot/mydir/mypage/

正則匹配, 如^~ /prefix/my/jsp

location[~|=|^~|~*]/uri{

}

rewrite 使用

使用 rewrite 支持 url 重寫. 支持 if,return語句.

只能在server/location/if中使用. 只能對域名後除去參數外的字符串起做用.

if(條件){}: 條件能夠是 = 或者 ~ 後者表示一個正則, 如if($request_uri~*\.sh){ return 403; } 表示若是請求是.sh結尾的, 則返回403.

rewrite的語法

rewrite regex replacement[flag]{last/break/redirect/permant}

last 中止處理後續的rewrite 指令集, 而後對當前重寫的uri在 rewrite 指令集上從新查找; break則不會從新查找;

//重定向到百度
location / {
    rewrite ^/ http://www.baidu.com ;
}
location / {
    rewrite '^/images/([a-z]{3})/(.*)\.(png)$' /images?file=$2.$3 ;
    set $image_file $2;
    set $image_type $3;
}
loation /images {
    root html;
    try_files /$arg_file /image404.html; 
}
location =/image404.html{
    return 404 "image not found";
}

瀏覽器本地緩存

expires 60s s|m|h|d 可用的時間單位

server{
    listen 80;
    server_name localhost;
    location /{
        root html;
        index index.html index.htm
    }
    location ~ \.(png|jpg|js|css|gif)${
        root html/images;
        expires 60s
    }
}

Gzip 壓縮

server{
    ...
    gzip on;
    gzip_buffers 4 16k  //16k爲單位, 4倍申請
    gzip_comp_level 4;  //0-9
    gzip_min_length 500;  //最小壓縮大小,小於此大小不壓縮
    gzip_types text/css; //壓縮的mime類型
    ...
}

反向代理

爲了方便單獨配置, 新增 proxy.conf 而後在主文件中的http節點添加include /path/proxy.conf 固然也能夠直接修改nginx.conf

location =/s {
    proxt_set_header Host $host
    proxt_set_header X-Real_IP $remote_addr // 設置請求的真實ip
    proxy_set_header interface_version $Host //版本號
    proxy_pass http://www.baidu.com;
}

負載均衡

upstream tomcatserver{
    ip_hash; // 也能夠不配置,不配置則輪訓策略, 或者配置爲fair(按響應時間),utl_hash
    server 192.168.1.122:8080;
    server 192.168.1.123:8080 weight=4; //若是是輪訓, 則權重起做用
}

// 修改proxy_pass節點爲負載均衡
prox_pass http://tomcatserver

配置https

生成證書:

  • openssl genrsa -des3 -out server.key 1024
  • openssl req -new -key server.key -out server.csr
  • cp server.key server.key.org
  • openssl rsa -in server.key.org -out server.key
  • openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

配置:

server{
    listen 443;
    server_name www.myhost.com;
    ssl on;
    ssl_certificate /my/host/conf/server.crt;
    ssl_certificate_key /my/host/conf/server.key;
    location / {
        proxy_pass http://myhost;
    }
}

修改tomcat配置(最後兩個配置新增): 這個能夠不配置, 走http

<Connector port="8080" protocal="HTTP/1.1" connectionTimeout="20000" 
proxyPort="443" redirectPort="443" />

nginx+keepalived (至關於nginx主從)

基於 VRRP(虛擬路由器冗餘協議);

  • 下載解壓keepalived包
  • 配置./configure --prefix=/mydir/keepalived/ --sysconf=/etc 最後一個參數是代表把配置文件存儲到指定目錄下
  • make & make install
  • ln -s /.../sbin/keepalived /sbin
  • cp /etc/init.d/keepalived /etc/init.d/
  • chkconfig --add keepalived
  • chkconfig keeepalived on

修改keepalived.conf文件

vrrp_instance_VI_1{
    state MASTER  // 另外一臺爲BACKUP
    interface eth0 //網卡端口, ifconfig後修改成正確端口
    virtual_router_id 51 //主從保持一致
    priority 100 //master必須大於backup, 大的節點會變成master
    ...
    virtual_ipaddress{ //虛擬服務器ip
        192.168.1.123
        192.168.1.124
    }
}

vitrual_server 192.168.1.123{
    delay_loop 6
    lb_glgo rr //loadbalance 算法
    ...

    real_server 192.168.11.123{
        weight 1 //權重
        TCP_CHECK{
            connect_timeout 3
            delay_before_retry 3
            connect_port 80
        }
    }
}
相關文章
相關標籤/搜索