Nginx(一)--nginx的初步認識及配置
什麽是Nginx
是一個高性能的反向代理服務器
正向代理代理的是客戶端
反向代理代理的是服務端
Apache、Tomcat、Nginx
靜態web服務器
jsp/servlet服務器 tomcat
安裝Nginx
1. 下載tar包
2. tar -zxvf nginx.tar.gz
3. ./configure [--prefix]
4. make && make install
啟動和停止
1. sbin/nginx
2. ./nginx -s stop
nginx.conf
Main
event
http
虛擬主機配置
erver { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } }
基於ip的虛擬主機
不演示
基於端口號的虛擬主機
server { listen 8080; server_name localhost; location / { root html; index index.html; } }
基於域名的虛擬主機
www.guapaoedu.com / ask.gupaoedu.com / git.gupaoedu.com / bbs.gupaoedu.com
server { listen 80; server_name www.gupaoedu.com; location / { root html; index index.html; } } server { listen 80; server_name bbs.gupaoedu.com; location / { root html; index bbs.html; } } server { listen 80; server_name ask.gupaoedu.com; location / { root html; index ask.html; } }
location
配置語法
location [= | ~* | ^~ ] /uri/ {...}
配置規則
location = /uri 精準匹配
location ^~ /uri 前綴匹配
location ~ /uri
location / 通用匹配
規則的優先級
1 location = / 2 location = /index 3 location ^~ /article/ 4 location ^~ /article/files/ 5 location ~ \.(gif|png|js|css)$ 6 location / http://192.168.11.154/ http://192.168.11.154/index ->2 http://192.168.11.154/article/files/1.txt ->4 http://192.168.11.154/mic.png ->5
1. 精準匹配是優先級最高
2. 普通匹配(最長的匹配)
3. 正則匹配
實際使用建議
location =/ { } location / { } location ~* \.(gif|....)${ }
Nginx模塊
反向代理、email、nginx core。。。
模塊分類
1. 核心模塊 ngx_http_core_module
2. 標準模塊 http模塊
3. 第三方模塊
ngx_http_core_module
server{ listen port server_name root ... }
location 實現uri到文件系統路徑的映射
2. error_page
ngx_http_access_module
實現基於ip的訪問控制功能 1、allow address | CIDR | unix: | all; 2、deny address | CIDR | unix: | all; 自上而下檢查,一旦匹配,將生效,條件嚴格的置前
如何添加第三方模塊
1. 原來所安裝的配置,你必在重新安裝新模塊的時候,加上
2. 不能直接make install
configure --prefix=/data/program/nginx
安裝方法
./configure --prefix=/安裝目錄 --add-module = /第三方模塊的目錄 ./configure --prefix=/data/program/nginx --with-http_stub_status_module --withhttp_random_index_module cp objs/nginx $nginx_home/sbin/nginx
http_stub_status_module
location /status { stub_status; }
Active connections:當前狀態,活動狀態的連接數
accepts:統計總值,已經接受的客戶端請求的總數
handled:統計總值,已經處理完成的客戶端請求的總數
requests:統計總值,客戶端發來的總的請求數
Reading:當前狀態,正在讀取客戶端請求報文首部的連接的連接數
Writing:當前狀態,正在向客戶端發送響應報文過程中的連接數
Waiting:當前狀態,正在等待客戶端發出請求的空閑連接數
http_random_index_module
www.gupaoedu.com
隨機顯示主頁
一般情況下,一個站點默認首頁都是定義好的index.html、index.shtml等等,如果想站點下有很多頁面想隨機展示給
用戶瀏覽,那得程序上實現,很麻煩,使用nginx的random index即可簡單實現這個功能,凡是以/結尾的請求,都
會隨機展示當前目錄下的文件作為首頁
\1. 添加random_index on 配置,默認是關閉的
location / { root html; random_index on; index index.html index.htm; }
\2. 在html目錄下創建多個html頁面
Nginx(一)--nginx的初步認識及配置