nginx操做

1、配置yum源:php

1.、打開centos的yum文件夾
  cd /etc/yum.repos.d/
二、用wget下載repo文件
  wget http://mirrors.aliyun.com/repo/Centos-7.repocss

或者(wget http://mirrors.163.com/.help/CentOS6-Base-163.repo)
若是wget命令不生效,說明尚未安裝wget工具,輸入yum -y install wget 回車進行安裝。(yum search wget)html

三、備份系統原來的repo文件
  mv CentOs-Base.repo CentOs-Base.repo.bak
  便是重命名 CentOs-Base.repo -> CentOs-Base.repo.bak
四、替換系統原理的repo文件
  mv Centos-7.repo CentOs-Base.repo
  便是重命名 Centos-7.repo -> CentOs-Base.reponginx

2、nginx yum安裝web

  1.rpm -qa | grep nginx(檢查設備是否已安裝nginx服務)正則表達式

  2.yum search nginx(搜索yum源)算法

 

 若是不存在,執行第三步後端

3.配置nginx源centos

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 
或者
(sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm)

在/etc/yum.repos.d目錄下生成nginx.pro文件,內容以下:瀏覽器

 

4.安裝nginx:

yum install nginx

5.驗證:

nginx -v

nginx -t

curl 127.0.0.1

 

 3、源碼安裝:

 4、配置文件解析:

  三個核心功能是靜態服務器、反向代理和負載均衡。

  配置文件:nginx.conf,以下所示。

  main # 全局配置

  #nginx進程數,建議設置爲等於CPU總核心數。

  worker_processes 8;

  (查看cpu參數  lscpu)(系統分頁大小 getconf PAGESIZE

  #全局錯誤日誌定義類型,[ debug | info | notice | warn | error | crit ] error_log   

  /usr/local/nginx/logs/error.log info;

  events { # 工做模式配置

    #參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];

    use epoll;

  }

  http { # http設置

    #文件擴展名與文件類型映射表

    include mime.types;

    #默認文件類型

    default_type application/octet-stream;

    server { # 服務器主機配置(虛擬主機、反向代理等)

      #log_format,用來設置日誌格式;

      #access_log,用來指定日誌文件的存放路徑、格式和緩存大小

      #日誌格式設定

      #$remote_addr與$http_x_forwarded_for用以記錄客戶端的ip地址;

      #$remote_user:用來記錄客戶端用戶名稱;

      #$time_local: 用來記錄訪問時間與時區;

      #$request: 用來記錄請求的url與http協議;

       #$status: 用來記錄請求狀態;成功是200,

      #$body_bytes_sent :記錄發送給客戶端文件主體內容大小;

      #$http_referer:用來記錄從那個頁面連接訪問過來的;

      #$http_user_agent:記錄客戶瀏覽器的相關信息;

      #一般web服務器放在反向代理的後面,這樣就不能獲取到客戶的IP地址了,經過$remote_addr拿到的IP地址是反向代理服務器的iP地址。

      反向代理服務器在轉發請求的http頭信息中,能夠增長x_forwarded_for信息,用以記錄原有客戶端的IP地址和原來客戶端的請求的服務器地址。

      例如:

      log_format  access   '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for';

      #定義本虛擬主機的訪問日誌

       access_log   /usr/local/nginx/logs/host.access.log  main;

       access_log   /usr/local/nginx/logs/host.access.404.log  log404;

       access_log   /usr/local/nginx/logs/host.access.my.log  access;

       location語法:

       location [ = | ~ | ~* | ^~ ] uri { ... }

      例如:

      location ~* .*\.(jpg|gif|png|js|css)$ {
           root  /data/av/;
      }    

      [=|~|~*|^~|@] 被稱做 location modifier 

   $不匹配具體的字符,而是匹配字符串的結尾位置,就是說必須在字符串的結尾匹配到$前面的部分才行,好比:x.gif 能匹配成功而 x.gifa 就不行

  與之相似,^ 匹配字符串開始的位置

nginx 常見正則匹配符號表示

  一、  ^  :   匹配字符串的開始位置;

  二、   $  :  匹配字符串的結束位置;

  三、  .*:    :  匹配任意字符,*匹配數量0到正無窮;

  四、  \.   :  斜槓用來轉義,\.匹配 . ;

  五、  (值1|值2|值3|值4)  :  或匹配模式,例:(jpg|gif|png|bmp)匹配jpg或gif或png或bmp

  六、  i     :  不區分大小寫
一.正則表達式匹配,其中:

   ~     區分大小寫匹配
   ~*      不區分大小寫匹配
   !~    區分大小寫不匹配及

  !~*     不區分大小寫不匹配    

  =    精確匹配 

  ^~     以某個字符串開頭 
  /       通用匹配,任何請求都會匹配到 

1.(基本配置)
   location { # 路由配置(虛擬目錄等)

   root    /nginx/www;
     index    index.php index.html index.htm;

   }

2..(反向代理配置)

  location path {

    proxy_pass   http://localhost:8888;

    proxy_set_header   X-real-ip $remote_addr;

     proxy_set_header   Host $http_host;

  }

3.(uwsgi配置

  location otherpath {

    include uwsgi_params;

     uwsgi_pass localhost:8888;

  }
 }

server {

  location {
      ....
  }

  location / {

    deny  192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny  all;
    }
  }

upstream name { # 負載均衡配置

 ip_hash;
  server 192.168.1.100:8000 weight=9;
  server 192.168.1.100:8001 down;
  server 192.168.1.100:8002 max_fails=3;
  server 192.168.1.100:8003 fail_timeout=20s;
  server 192.168.1.100:8004 max_fails=3 fail_timeout=20s;

#upstream的負載均衡,weight是權重,能夠根據機器配置定義權重。weigth參數表示權值,權值越高被分配到的概率越大。

  server 192.168.80.121:80 weight=3;

  server 192.168.80.122:80 weight=2;

  server 192.168.80.123:80 weight=3;

#nginx的upstream目前支持4種方式的分配

#一、輪詢(默認) #每一個請求按時間順序逐一分配到不一樣的後端服務器,若是後端服務器down掉,能自動剔除。

#二、weight #指定輪詢概率,weight和訪問比率成正比,用於後端服務器性能不均的狀況。

 #例如:

  upstream bakend {

    server 192.168.0.14 weight=10;

     server 192.168.0.15 weight=10;

  }

#3、ip_hash #每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器,能夠解決session的問題。

#例如:

  upstream bakend{

    ip_hash;

    server 192.168.0.14:88;

    server 192.168.0.15:80;

  }

#4、fair(第三方)

#按後端服務器的響應時間來分配請求,響應時間短的優先分配。

   upstream backend { 

     server server1;

     server server2;

     air;

  }

#5、url_hash(第三方)

#按訪問url的hash結果來分配請求,使每一個url定向到同一個後端服務器,後端服務器爲緩存時比較有效。

#例:在upstream中加入hash語句,server語句中不能寫入weight等其餘的參數,hash_method是使用的hash算法

   upstream backend {

    server squid1:3128;

    server squid2:3128;

    hash $request_uri;

    hash_method crc32;

   }

#tips:

  #upstream bakend{

   #定義負載均衡設備的Ip及設備狀態

   }

   {

    # ip_hash;

    # server 127.0.0.1:9090 down;

    # server 127.0.0.1:8080 weight=2;

    # server 127.0.0.1:6060;

    # server 127.0.0.1:7070 backup;

    #}

  #在須要使用負載均衡的server中增長 proxy_pass http://bakend/;

  #每一個設備的狀態設置爲:

  #1.down表示單前的server暫時不參與負載

  #2.weight爲weight越大,負載的權重就越大。

  #3.max_fails:容許請求失敗的次數默認爲1.當超過最大次數時,返回proxy_next_upstream模塊定義的錯誤

  #4.fail_timeout:max_fails次失敗後,暫停的時間。

  #5.backup: 其它全部的非backup機器down或者忙的時候,請求backup機器。因此這臺機器壓力會最輕。

  #nginx支持同時設置多組的負載均衡,用來給不用的server來使用。   }}

相關文章
相關標籤/搜索