一。安裝過程
1》下載
http://tengine.taobao.org/download.html 找到下載包並且下載(Tengine-2.2.0.tar.gz)
2》解壓
tar zxvf Tengine-2.2.0.tar.gz
3》配置檢查
進入解壓後的目錄 ./configure
4》安裝
Make && make install 安裝完成 安裝目錄爲/usr/local/nginx
找到 /usr/local/nginx/sbin/nginx -V 查看所有加載的模塊
/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 可以看到拋出錯誤
可以看到在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》常用配置選項
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)
指定保存用戶名和密碼的文件,格式如下:
密碼應該使用crypt()函數加密。 可以用Apache發行包中的htpasswd命令來創建此類文件。
安裝Apache yum -y install httpd
[root@bogon logs]# htpasswd -nb zs 123456
zs:$apr1$8cAQMH7X$AvjTq5Gon0kiKHR/2LW2g/
通過vi創建文件拷貝內容到該文件 /usr/local/nginx/conf/auth.conf
修改
效果圖
二。nginx常用指令
1》proxypass指令
該指令將請求定位到後端服務器 比如如果nginx代理了後端服務器 默認所有靜態資源都會從root下找 可以代理到後臺服務器
比如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請求轉發
參考 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://”之後的文本開始匹配。
實例: