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
- 異常1:
- [root@bogon tengine]# ./configure
- checking for OS
- + Linux 3.10.0-327.el7.x86_64 x86_64
- checking for C compiler ... not found
- ./configure: error: C compiler cc is not found
- 也就是c編譯器 gcc找不到
- 安裝gcc yum -y install gcc
- 繼續檢查
- 異常2:
- checking for PCRE library ... not found
- checking for PCRE library in /usr/local/ ... not found
- checking for PCRE library in /usr/include/pcre/ ... not found
- checking for PCRE library in /usr/pkg/ ... not found
- checking for PCRE library in /opt/local/ ... not found
- ./configure: error: the HTTP rewrite module requires the PCRE library.
- You can either disable the module by using --without-http_rewrite_module
- 缺少pcre和pcre-devel 安裝 yum -y install pcre pcre-devel
- 這裏安裝好後 可以通過
- Rpm -qa | grep pcre找到所有pcre的包
- Rpm -ql 完整包名 查看安裝的路徑
- 異常3:
- checking for OpenSSL library ... not found
- 缺少openssl和openssl-devel 安裝 yum -y install openssl openssl-devel
- 繼續檢測成功 可以同構日誌看到 需要pcre openssl zlib(安裝openssl自動安裝)的庫
- checking for PCRE library ... found
- checking for PCRE JIT support ... found
- checking for OpenSSL library ... found
- checking for zlib library ... found
- creating objs/Makefile
4》安裝
Make && make install 安裝完成 安裝目錄爲/usr/local/nginx
找到 /usr/local/nginx/sbin/nginx -V 查看所有加載的模塊
- [root@bogon sbin]# ./nginx -V
- Tengine version: Tengine/2.2.0 (nginx/1.8.1)
- built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
- TLS SNI support enabled
- configure arguments:
- nginx: loaded modules:
- nginx: ngx_core_module (static)
- nginx: ngx_errlog_module (static)
- nginx: ngx_conf_module (static)
- nginx: ngx_dso_module (static)
- 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 可以看到拋出錯誤
- Jun 08 03:08:55 bogon systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start.
- Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
修改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》常用配置選項
- #user nobody;
- #工作的cpu的內核數 默認應該是和當前電腦的cpu一致 設置爲auto自動獲取當前機器
- worker_processes auto;
- #ngx_http_log_module模塊功能
- #語法爲 error_log 存儲的log文件路徑 [debug | info | notice | warn | error | crit]
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #定義存儲nginx主進程ID的file。 nigix可能建立虛擬主機是 會創建多個進程
- #pid logs/nginx.pid;
- #最頂層的指令 events一般用於指定連接處理參數
- events {
- #設置每個工作進程可以打開的最大併發連接數。(數量包含所有連接(比如,和後端服務器建立的連接,還有其他的), 而不僅僅是和客戶端的連接)
- worker_connections 1024;
- }
- # load modules compiled as Dynamic Shared Object (DSO)
- #
- #dso {
- # load ngx_http_fastcgi_module.so;
- # load ngx_http_rewrite_module.so;
- #}
- #配置http服務器
- http {
- #包含其他語法正確的配置文件
- include mime.types;
- #設置默認的mini類型
- 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"';
- #access_log logs/access.log main;
- #是否啓用內核級別拷貝 零拷貝 速度快
- sendfile on;
- #tcp_nopush on;
- #長連接的超時是假呢
- #keepalive_timeout 0;
- keepalive_timeout 65;
- #是否啓用gzip壓縮 壓縮需要時間 但是壓縮後 可以節省帶寬
- #gzip on;
- #配置虛擬主機
- server {
- #虛擬主機監聽端口
- listen 80;
- #虛擬主機監聽的ip(同一電腦可能多個ip)或者域名
- #添加虛擬ip ifconfig 網卡名(可以通過ifconfig查看):1 192.168.58.134 netmask 255.255.255.0 up
- #刪除虛擬ip ifconfig 網卡名(可以通過ifconfig查看):1 down
- server_name localhost;
- //當根路徑訪問時 必須 http://localhost/ 自動進入根目錄 html下找 index.html文件
- location / {
- root html;
- index index.html index.htm;
- }
- //出現以下錯誤狀態碼 自動進入 /50x.html
- error_page 500 502 503 504 /50x.html;
- //自動找 html 實際文件路徑就是 /html/50x.html
- location = /50x.html {
- root html;
- }
- }
- }
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)
- location / {
- auth_basic "密碼驗證";
- auth_basic_user_file conf/htpasswd;
- }
指定保存用戶名和密碼的文件,格式如下:
- name1:password1
- name2:password2:comment
- name3:password3
密碼應該使用crypt()函數加密。 可以用Apache發行包中的htpasswd命令來創建此類文件。
安裝Apache yum -y install httpd
- [root@bogon logs]# htpasswd
- 使用語法
- htpasswd [-cimBdpsDv] [-C cost] passwordfile username
- htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password
- htpasswd -n[imBdps] [-C cost] username
- 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
修改
- location / {
- auth_basic "密碼驗證";
- auth_basic_user_file /usr/local/nginx/conf/auth.conf;
- }
效果圖
二。nginx常用指令
1》proxypass指令
該指令將請求定位到後端服務器 比如如果nginx代理了後端服務器 默認所有靜態資源都會從root下找 可以代理到後臺服務器
- location / {
- root html;
- index index.html index.htm;
- if ($uri ~* "^/admin/(.*)$") {
- return 404;
- }
- if ($uri ~* "^(.*)\.(jpg|png|gif|bmp)$") {
- proxy_pass http://192.168.58.147:8080;
- }
比如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請求轉發
- location / {
- rewrite ^/download/(.*)$ /50x.html permanent;
- set $id 1;
- }
- location = /50x.html {
- root html;
- }
參考 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://”之後的文本開始匹配。
實例:
- location / {
- root html;
- index index.html index.htm;
- valid_referers none blocked server_names 192.168.58.149;
- if ($invalid_referer) {
- <span style="white-space:pre;"> </span>return 403;
- <span style="white-space:pre;"> </span> }
- }