tengine安裝及配置

tengine安裝及配置

一。安裝過程

  1》下載

        http://tengine.taobao.org/download.html 找到下載包並且下載(Tengine-2.2.0.tar.gz

  2》解壓

         tar zxvf Tengine-2.2.0.tar.gz  

  3》配置檢查 

       進入解壓後的目錄  ./configure

[java] view plain copy
  1. 異常1:  
  2.    [root@bogon tengine]# ./configure  
  3. checking for OS  
  4.  + Linux 3.10.0-327.el7.x86_64 x86_64  
  5. checking for C compiler ... not found  
  6.    
  7. ./configure: error: C compiler cc is not found  
  8. 也就是c編譯器 gcc找不到  
  9.   安裝gcc  yum -y install gcc  
  10. 繼續檢查  
  11. 異常2:  
  12.  checking for PCRE library ... not found  
  13. checking for PCRE library in /usr/local/ ... not found  
  14. checking for PCRE library in /usr/include/pcre/ ... not found  
  15. checking for PCRE library in /usr/pkg/ ... not found  
  16. checking for PCRE library in /opt/local/ ... not found  
  17.    
  18. ./configure: error: the HTTP rewrite module requires the PCRE library.  
  19. You can either disable the module by using --without-http_rewrite_module  
  20.  缺少pcre和pcre-devel 安裝 yum -y install pcre pcre-devel  
  21.  這裏安裝好後 可以通過  
  22.   Rpm -qa | grep pcre找到所有pcre的包  
  23.   Rpm -ql 完整包名  查看安裝的路徑  
  24.    
  25. 異常3:  
  26.  checking for OpenSSL library ... not found  
  27.  缺少openssl和openssl-devel 安裝 yum -y install openssl openssl-devel  
  28.    
  29. 繼續檢測成功 可以同構日誌看到 需要pcre openssl zlib(安裝openssl自動安裝)的庫  
  30. checking for PCRE library ... found  
  31. checking for PCRE JIT support ... found  
  32. checking for OpenSSL library ... found  
  33. checking for zlib library ... found  
  34. creating objs/Makefile  
  35.    

4》安裝

    Make && make install 安裝完成 安裝目錄爲/usr/local/nginx

找到 /usr/local/nginx/sbin/nginx -V 查看所有加載的模塊

[java] view plain copy
  1. [root@bogon sbin]# ./nginx -V  
  2. Tengine version: Tengine/2.2.0 (nginx/1.8.1)  
  3. built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)  
  4. TLS SNI support enabled  
  5. configure arguments:  
  6. nginx: loaded modules:  
  7. nginx:     ngx_core_module (static)  
  8. nginx:     ngx_errlog_module (static)  
  9. nginx:     ngx_conf_module (static)  
  10. nginx:     ngx_dso_module (static)  
  11. nginx:     ngx_events_module (static)  

 /usr/local/nginx/sbin/nginx start|stop|restart 啓動和關閉ngix服務

 訪問 http://ip地址

 5》nginx命令參數

   nginx -m 顯示所有加載的模塊
   nginx -l 顯示所有可以使用的指令
   nginx -t 檢查nginx的配置文件是否正確
   nginx -s 啓動nginx
   nginx -s reload 重啓nginx
   nginx -s stop 停止nginx

 6》開機啓動nginx

  nigix官網上找到  https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/ 查看linux下的啓動腳本 

進入官網 www.nginx.com 點擊resources-community wiki


進入後 點擊 下圖 Getting Started按鈕進入

點擊下界面的Ngix init Scipts

點擊 Red hast nginx init script 即可打開腳本



  保存內容到  /etc/init.d/nginx (服務的教程參考http://blog.csdn.net/liaomin416100569/article/details/72876349)

 拷貝後 需要修改其中的兩個變量 nginx安裝目錄的配置文件和nginx程序的目錄 

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"
修改爲

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"


注意 nginx文件的內容是否完整拷貝 有時拷貝 缺少一些字符導致報錯 

 service nginx start  或者 systemctl start nginx

一般啓動會卡住  通過 systemctl status nginx 可以看到拋出錯誤

[java] view plain copy
  1. Jun 08 03:08:55 bogon systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start.  
  2. Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.  
可以看到在nginx 在啓動時候 自動會去 /var/run下面去找pid文件所以必須 要將pid文件生成在/var/run下 

修改conf/nginx.conf文件 

將  #pid=logs/nginx.pid(可以看到pid默認生成在logs目錄下) 修改爲 pid=/var/run/nginx.pid 即可 去掉註釋的#   重啓即可


添加到系統服務 設置開機啓動

[root@bogon init.d]# chkconfig --add nginx
[root@bogon init.d]# chkconfig nginx on

 使用 http://192.168.58.131/訪問 看是否出現 歡迎使用tengine的英文

二。配置文件

 默認配置文件位於 

/usr/local/nginx/conf/nginx.conf

 1》常用配置選項

[java] view plain copy
  1. #user  nobody;  
  2. #工作的cpu的內核數  默認應該是和當前電腦的cpu一致  設置爲auto自動獲取當前機器  
  3. worker_processes  auto;  
  4. #ngx_http_log_module模塊功能    
  5. #語法爲 error_log 存儲的log文件路徑 [debug | info | notice | warn | error | crit]  
  6. #error_log  logs/error.log;  
  7. #error_log  logs/error.log  notice;  
  8. #error_log  logs/error.log  info;  
  9. #定義存儲nginx主進程ID的file。 nigix可能建立虛擬主機是 會創建多個進程   
  10. #pid        logs/nginx.pid;  
  11.   
  12. #最頂層的指令 events一般用於指定連接處理參數  
  13. events {  
  14.     #設置每個工作進程可以打開的最大併發連接數。(數量包含所有連接(比如,和後端服務器建立的連接,還有其他的), 而不僅僅是和客戶端的連接)  
  15.     worker_connections  1024;  
  16. }  
  17.   
  18. # load modules compiled as Dynamic Shared Object (DSO)  
  19. #  
  20. #dso {  
  21. #    load ngx_http_fastcgi_module.so;  
  22. #    load ngx_http_rewrite_module.so;  
  23. #}  
  24. #配置http服務器  
  25. http {  
  26.     #包含其他語法正確的配置文件   
  27.     include       mime.types;  
  28.     #設置默認的mini類型  
  29.     default_type  application/octet-stream;  
  30.   
  31.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  32.     #                  '$status $body_bytes_sent "$http_referer" '  
  33.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  34.   
  35.     #access_log  logs/access.log  main;  
  36.     #是否啓用內核級別拷貝 零拷貝 速度快  
  37.     sendfile        on;  
  38.     #tcp_nopush     on;  
  39.     #長連接的超時是假呢  
  40.     #keepalive_timeout  0;  
  41.     keepalive_timeout  65;  
  42.     #是否啓用gzip壓縮 壓縮需要時間 但是壓縮後 可以節省帶寬  
  43.     #gzip  on;  
  44.     #配置虛擬主機  
  45.     server {  
  46.         #虛擬主機監聽端口  
  47.         listen       80;  
  48.         #虛擬主機監聽的ip(同一電腦可能多個ip)或者域名  
  49.         #添加虛擬ip ifconfig 網卡名(可以通過ifconfig查看):1 192.168.58.134 netmask 255.255.255.0 up  
  50.         #刪除虛擬ip ifconfig 網卡名(可以通過ifconfig查看):1 down  
  51.         server_name  localhost;  
  52.     //當根路徑訪問時 必須 http://localhost/ 自動進入根目錄 html下找 index.html文件  
  53.         location / {  
  54.             root   html;  
  55.             index  index.html index.htm;  
  56.         }  
  57.         //出現以下錯誤狀態碼 自動進入 /50x.html     
  58.         error_page   500 502 503 504  /50x.html;  
  59.         //自動找 html 實際文件路徑就是 /html/50x.html  
  60.         location = /50x.html {  
  61.             root   html;  
  62.         }  
  63.     }  
  64.   
  65. }  

 2》location的路徑匹配

該配置位於 ngx_http_core_module 模塊 (http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#location)

nginx中location的路徑匹配規則 

 使用正則表達式需要在路徑開始添加“~*”前綴 (不區分大小寫),或者“~”前綴(區分大小寫)
匹配的路徑以“^~”開始,那麼nginx不再檢查後面的所有條件和正則表達式。
使用“=”前綴可以定義URI和路徑的精確匹配。如果發現匹配,則終止路徑查找
#這個表示 路徑精確就是 /就匹配A 比如 http://localhost/
location = / {
    [ configuration A ]
}
location / {
    [ configuration B ]
}
location /documents/ {
    [ configuration C ]
}
location ^~ /images/ {
    [ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ { 
    [ configuration E ] 

請求“/”匹配配置A, 請求“/index.html”匹配配置B, 請求“/documents/document.html”匹配配置C, 請求“/images/1.gif”匹配配置D, 請求“/documents/1.jpg”匹配配置E。
 2》location其他配置

   》ngx_http_access_module模塊 限制客戶端訪問ip(http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_access_module.html)

   配置範例
location / {
    deny  192.168.58.1;
    allow  all;
}
這裏 192.168.58.1被禁止訪問  除了58.1其他都可以訪問

58.1訪問 出現 403被禁止  其他ip都可以正常訪問(linux訪問可以使用 wget 地址 或者 curl地址) 可以通過logs/access.log查看訪問服務的客戶端信息


  》模塊ngx_http_auth_basic_module 允許使用“HTTP基本認證”協議驗證用戶名和密碼來限制對資源的訪問。
(http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_auth_basic_module.html)

[java] view plain copy
  1. location / {  
  2.    auth_basic           "密碼驗證";  
  3.    auth_basic_user_file conf/htpasswd;  
  4. }  


指定保存用戶名和密碼的文件,格式如下:
[java] view plain copy
  1. name1:password1  
  2. name2:password2:comment  
  3. name3:password3  


密碼應該使用crypt()函數加密。 可以用Apache發行包中的htpasswd命令來創建此類文件。


安裝Apache  yum -y install httpd  
[java] view plain copy
  1. [root@bogon logs]# htpasswd  
  2.    使用語法  
  3.         htpasswd [-cimBdpsDv] [-C cost] passwordfile username  
  4.         htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password  
  5.   
  6.   
  7.         htpasswd -n[imBdps] [-C cost] username  
  8.         htpasswd -nb[mBdps] [-C cost] username password  

[root@bogon logs]# htpasswd -nb zs 123456
zs:$apr1$8cAQMH7X$AvjTq5Gon0kiKHR/2LW2g/
通過vi創建文件拷貝內容到該文件 /usr/local/nginx/conf/auth.conf
修改
 
[java] view plain copy
  1. location / {  
  2.     auth_basic           "密碼驗證";  
  3.     auth_basic_user_file /usr/local/nginx/conf/auth.conf;  
  4.  }  

效果圖


二。nginx常用指令

1》proxypass指令
     該指令將請求定位到後端服務器  比如如果nginx代理了後端服務器 默認所有靜態資源都會從root下找 可以代理到後臺服務器
  

[html] view plain copy
  1. location / {  
  2.            root   html;  
  3.            index  index.html index.htm;  
  4.         if ($uri ~* "^/admin/(.*)$") {  
  5.             return 404;  
  6.         }  
  7.         if ($uri ~* "^(.*)\.(jpg|png|gif|bmp)$") {  
  8.             proxy_pass http://192.168.58.147:8080;  
  9.         }  

比如nginx的ip是 192.168.58.1 當我訪問 http://192.168.58.1/ttt/abc.png 會自動訪問  http://192.168.58.147:8080/ttt/abc.png

if語法參考rewrite模塊 (http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_rewrite_module.html#if)

具體這些參數可以去tengine官網 找到模塊下 嵌入的變量查看 比如查看
比如http_core模塊 
  http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#variables

2》 rewrite指令

   rewrite指令 是將當前匹配的location請求 rewrite到其他的location上 也就是相當於javaee請求轉發

[html] view plain copy
  1. location / {  
  2.      rewrite ^/download/(.*)$ /50x.html permanent;  
  3.      set $id 1;       
  4.   }  
  5. location = /50x.html {  
  6.            root   html;  
  7.        }  
因爲rewrite已經跳轉了 那麼rewrite後面的指令要不要繼續執行 就要看最後一個參數的標誌了
參考 http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_rewrite_module.html#rewrite

3》valid_referers指令

“Referer”請求頭爲指定值時,內嵌變量$invalid_referer被設置爲空字符串, 否則這個變量會被置成“1”。查找匹配時不區分大小寫。
該指令的參數可以爲下面的內容:
none
缺少“Referer”請求頭;
blocked
“Referer” 請求頭存在,但是它的值被防火牆或者代理服務器刪除; 這些值都不以“http://” 或者 “https://”字符串作爲開頭;
server_names
“Referer” 請求頭包含某個虛擬主機名;
任意字符串
定義一個服務器名和可選的URI前綴。服務器名允許在開頭或結尾使用“*”符號。 當nginx檢查時,“Referer”請求頭裏的服務器端口將被忽略。
正則表達式
必須以“~”符號作爲開頭。 需要注意的是表達式會從“http://”或者“https://”之後的文本開始匹配。
實例:

[html] view plain copy
  1.       location / {  
  2.             root   html;  
  3.             index  index.html index.htm;  
  4.         valid_referers none blocked server_names 192.168.58.149;  
  5.             if ($invalid_referer) {  
  6. <span style="white-space:pre;">             </span>return 403;  
  7. <span style="white-space:pre;"> </span>    }  
  8. }  

發佈了40 篇原創文章 · 獲贊 33 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章