目錄導航
前言
Nginx中文文檔
從這篇開始,我們講Nginx,分爲三小節:
- 1、Nginx的初步認識及配置
- 2、Nginx的應用實戰
- 3、Nginx的擴展
互聯網領域,作爲新人學習一個開源框架或是組件,一般先了解官方文檔,雖然英文,但是實爲第一手資料,包括我在內的其他非專業開發人員寫的都是自己的理解,所以會造成知識的二次傳遞,爲什麼BAT注重面試考察原理與實戰解決問題的能力?更多的看中的是知識的深度與廣度。所以,不是我今天在這裏寫出三篇關於Nginx的使用與原理,你就可以理解了(大佬略過)~
千里之行,始於足下。我想表達的是,看懂未必等於實際操作也可以,行動起來,用代碼改變世界,你我一起創造未來!
什麼是Nginx
是一個高性能的反向代理服務器
-
正向代理代理的是客戶端
-
反向代理代理的是服務端
Apache、Tomcat、Nginx
靜態web服務器
jsp/servlet服務器 tomcat
安裝Nginx
1.下載tar包
2.tar -zxvf nginx.tar.gz
3…/configure [–prefix]
設置Nginx安裝路徑,如果沒有指定,默認爲/usr/local/nginx
執行後:
自定義安裝路徑:
./configure --prefix=/data/program/nginx/ --with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre
4.make && make install
執行後:
如果安裝期間有問題,可以參考:nginx安裝配置Linux版本
啓動和停止
- {path}/sbin/nginx
如果在configure的時候沒有指定安裝路徑,則會默認安裝到/usr/local/nginx目錄。類似於在windows上安裝一個軟件,默認安裝到C盤類似的效果。
不論哪種安裝方式,直接進入安裝目錄的/sbin/nginx ,啓動即可
也可以指定配置文件
最終去瀏覽器,訪問默認的地址:localhost
2. ./nginx -s stop
nginx.conf
Main
event
http
虛擬主機配置
server {
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;
}
}
基於域名的虛擬主機
server {
listen 80;
server_name www.xxx.com;
location / {
root html;
index index.html;
}
}
server {
listen 80;
server_name bbs.xxx.com;
location / {
root html;
index bbs.html;
}
}
server {
listen 80;
server_name ask.xxx.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
-
精準匹配是優先級最高
-
普通匹配(最長的匹配)
-
正則匹配
實際使用建議
location =/ {
}
location / {
}
location ~* \.(gif|....)${
}
Nginx模塊
反向代理、email、nginx core等
模塊分類
-
核心模塊 ngx_http_core_module
-
標準模塊 http模塊
-
第三方模塊
**
ngx_http_core_module**
server{
listen port
server_name
root ...
}
location 實現uri到文件系統路徑的映射
ngx_http_access_module
實現基於ip的訪問控制功能
1、allow address | CIDR | unix: | all;
2、deny address | CIDR | unix: | all;
自上而下檢查,一旦匹配,將生效,條件嚴格的置前
如何添加第三方模塊
1 .原來所安裝的配置,你必在重新安裝新模塊的時候,加上
- 不能直接make install
configure --prefix=/data/program/nginx
**
安裝方法**
./configure --prefix=/安裝目錄 --add-module = /第三方模塊的目錄
./configure --prefix=/data/program/nginx --with-http_stub_status_module --with-http_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
一般情況下,一個站點默認首頁都是定義好的index.html、index.shtml等等,如果想站點下有很多頁面想隨機展示給用戶瀏覽,那得程序上實現,很麻煩,使用nginx的random index即可簡單實現這個功能,凡是以/結尾的請求,都會隨機展示當前目錄下的文件作爲首頁
- 添加random_index on 配置,默認是關閉的
location / {
root html;
random_index on;
index index.html index.htm;
}
- 在html目錄下創建多個html頁面
後記
本節的總結:
-
Nginx在分佈式架構中的應用分析
-
常用的Web服務器及差異
-
Nginx的安裝以及配置分析
-
Nginx虛擬主機配置
-
詳解Location的匹配規則
更多架構知識,歡迎關注本套Java系列文章:Java架構師成長之路