標籤:匹配 配置 and 判斷 http alt 虛擬 home localhost
為什麼選擇Nginx,nginx有諸多優點:
nginx是輕量級web伺服器,支援AIO、mmap、event-driven,解決了c10k問題、虛擬機器主機、基於名字和IP訪問、nginx平滑升級 、熱部署、自訂日誌格式、3XX 5XX錯誤碼重新導向、重寫url、支援http referer 使用防盜鏈(判斷請求過來的url)、支援flv和mp4流、速度限制。
架構:
一個主進程master,多個子進程worker
支援sendfile/sendfile64
設定檔結構:
server{}:虛擬機器主機
location{}:
location /URI/ {
root “/web/htdocs”
}
location URI:對當前路徑和自路徑下的所有對象都生效,匹配範圍大
location = URI:對當前路徑生效,精確匹配指定路徑,不包括子路徑
location ~ URI {}:
location ~* URI {}:
模式比對URI,此處的URI可以使用Regex,~區分字元大小寫,~*不區分
location ^~ URI {}:不使用Regex
匹配優先順序”=” > “/” “^~” > “~”
例子:
location = / { [ configuration A ]}location / { [ configuration B ]}location /documents/ { [ configuration C ]}location ^~ /images/ { [ configuration D ]}location ~* \.(gif|jpg|jpeg)$ { [ configuration E ]}
The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.
location / { root /web/web1; index index.html index.htm;}
location /web2/ { root /web; index index.html index.htm;}
介紹幾個http相關模組:
- 存取控制法則:基於IP,Module ngx_http_access_module
deny
allow
location /web2/ { root /web; index index.html index.htm; deny 192.168.31.85; }
- 基於使用者:Module ngx_http_auth_basic_module
location / { satisfy any; allow 192.168.1.0/32; deny all; auth_basic "closed site"; auth_basic_user_file conf/htpasswd;}location /web2/ { root /web; index index.html index.htm; auth_basic "closed site"; auth_basic_user_file /etc/nginx/.users;}
建立使用者:
-c 第一次建立檔案時使用:
# htpasswd -c -m /etc/nginx/.users tom
# htpasswd -m /etc/nginx/.users tom2
- 允許列出其檔案:Module ngx_http_autoindex_module
找不到首頁面時,預設拒絕訪問:
location / { root /web/web1; index home.html; }
location / { root /web/web1; index home.html; autoindex on;}
- 統計訪問資訊:Module ngx_http_stub_status_module
location /basic_status { stub_status;}
In versions prior to 1.7.5, the directive syntax required an arbitrary argument, for example, “stub_status on”.
訪問:http://192.168.2.168/basic_status
當前處理活動的連結個數
已經接受的串連數 已經處理的串連數 已經處理的請求數
Reading
The current number of connections where nginx is reading the request header.
Writing
The current number of connections where nginx is writing the response back to the client.
Waiting
The current number of idle client connections waiting for a request.(長串連模式保持的個數)
- https訪問:Module ngx_http_ssl_module
server { listen 443 ssl; server_name localhost; ssl on; ssl_certificate /etc/nginx/ssl/nginx.crt; ssl_certificate_key /etc/nginx/ssl/nginx.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root /web/ssl; index index.html index.htm; } }
- Module ngx_http_fastcgi_module
location ~ ^(.+.php)(.*)$ {fastcgi_split_path_info ^(.+.php)(.*)$;include fastcgi.conf;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param PATH_INFO $fastcgi_path_info;}
參見前面Zabbix系統搭建,就是一個很好LEMP應用的實踐。
總結:
Nginx以其優秀的效能和提供的各種模組,作為web伺服器越來越被多數企業選擇,這裡沒有詳細介紹Nginx作為web伺服器相關的各個模組,大家可以參考官方文檔或其他途徑學習更多Nginx特性,官方文檔http://nginx.org/en/docs/
Nginx作為web伺服器