一。安裝過程html
1》下載java
http://tengine.taobao.org/download.html 找到下載包而且下載(Tengine-2.2.0.tar.gz)linux
2》解壓nginx
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/nginxapp
找到 /usr/local/nginx/sbin/nginx -V 查看全部加載的模塊curl
[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.能夠看到在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使用 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
[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
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官網 找到模塊下 嵌入的變量查看 好比查看2》 rewrite指令
rewrite指令 是將當前匹配的location請求 rewrite到其餘的location上 也就是至關於javaee請求轉發
location / { rewrite ^/download/(.*)$ /50x.html permanent; set $id 1; } location = /50x.html { root html; }由於rewrite已經跳轉了 那麼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) { return 403; } }