Nginx 是什么
Nginx 是一款輕量級的web服務(wù)器,也是一款輕量級的反向服務(wù)器。
Nginx主要作用
- 作為反向代理服務(wù)器。
- 作為郵件代理服務(wù)器。
- 實現(xiàn)前端動靜分離。
Nginx 特點
高穩(wěn)定, 高性能,資源占用少,功能豐富,模塊化結(jié)構(gòu),支持熱部署。
Nginx 配置文件
- /conf/nginx.conf: 應(yīng)用程序的基本配置文件。
- /mime.types: MIME類型關(guān)聯(lián)的擴展文件。
- fastcgi.conf: 與fastcgi 相關(guān)的配置。
- proxy.conf: 與proxy 相關(guān)的配置。
- sites.conf: 配置Nginx提供的網(wǎng)站,包括虛擬主機
·
Nginx 的進(jìn)程結(jié)構(gòu)
啟動Nginx 的時候,會啟動一個Master進(jìn)程,這個進(jìn)程不處理任何客戶端請求,主要用來產(chǎn)生
worker進(jìn)程,一個worker 進(jìn)程用來處理一個request。
進(jìn)程
Nginx 文檔
指令格式
Syntax: client_body_timeout time;
Default: client_body_timeout 60s;
Context: http, server, location
- 第一行語法格式。
- 第二行默認(rèn)值。
- 第三行 指令可以放在的位置。
Nginx模塊分為
核心模塊,事件模塊,標(biāo)準(zhǔn)http模塊,可選http模塊,郵件模塊,第三方模塊和補丁。
- 核心模塊:基本功能和指令,如進(jìn)程管理和安全。
- 事件模塊:在Nginx內(nèi)配置網(wǎng)絡(luò)使用能力。
- 配置模塊:提供包含機制。
nginx.conf 文檔結(jié)
文檔結(jié)構(gòu)
核心模塊
<a target="_blank">指令文檔</a>
1,error_log
日志 級別debug, info, notice, warn, error, crit, alert, or emerg.
可以設(shè)置的位置main, http, mail, stream, server, location
error_log logs/error.log error;
2, include
引入文件
可以在任意存放位置
include mime.types;
3, pid
nginx的pid 存放位置
只能在main 位置配置
pid nginx.pid;
4, user
所在用戶組
5, worker_processes
定義進(jìn)程數(shù)
Syntax: worker_processes number | auto;
Default:
worker_processes 1;
Context: main
Location 區(qū)段配置
Location 基本語法
location [=|*|^|@] pattern{....}
1,沒有修飾符 表示:必須以指定模式開始
server{
server_name xxx.com
location /abc{
....
}
.....
}
以下url 都可以匹配
http://xxx.com/abc
http://xxx.com/abc?p1=1&p2=11
http://xxx.com/abc/
http://xxx.com/abcefg
2, = 表示:必須與指定的模式精確匹配
server{
server_name xxx.com
location = /abc{
....
}
.....
}
以下url 都可以匹配
http://xxx.com/abc
http://xxx.com/abc?p1=1&p2=11
以下url 不能匹配
http://xxx.com/abc/
http://xxx.com/abcefg
3, ~ 表示: 指定的正則表達(dá)式要區(qū)分大小寫
server{
server_name xxx.com
location ~ ^/abc${
....
}
.....
}
以下url 都可以匹配
http://xxx.com/abc
http://xxx.com/abc?p1=1&p2=11
以下url 不能匹配
http://xxx.com/abc/
http://xxx.com/abcefg
http://xxx.com/ABC
4, ~* 表示:指定正則表達(dá)式不區(qū)分大小寫
server{
server_name xxx.com
location ~* ^/abc${
....
}
.....
}
以下url 都可以匹配
http://xxx.com/abc
http://xxx.com/abc?p1=1&p2=11
http://xxx.com/ABC
以下url 不能匹配
http://xxx.com/abc/
http://xxx.com/abcefg
5, ^~ 類似于無修飾符的行為,也是以指定模式開始,不同的是,如果模式匹配,那么
就停止搜索其它模式了。
6, @ 定義命名location區(qū)段,這些區(qū)段用戶不能訪問,只可以由內(nèi)部產(chǎn)生的請求來訪問
如error_page
查找順序和優(yōu)先級
- 帶有“=” 的精確匹配優(yōu)先。
- 沒有修飾符的精確匹配。
- 正則表達(dá)式按照他們在配置文件中定義順序。
- 帶有“^~” 修飾符的,開頭匹配。
- 帶有“~” 或“~*” 修飾符的,如果正則表達(dá)式與uri匹配。
- 沒有修飾符的,如果指定字符串與uri開頭匹配
負(fù)載均衡 反向代理
在http 模塊設(shè)置
upstream ceshi {
server 127.0.0.1:8484 weight=5;
server 127.0.0.1:8888 weight=5;
}
location /root {
#http:// + “upstream”
proxy_pass http://ceshi/;
}
動靜分離
location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff)$ {
#所有靜態(tài)文件直接讀取硬盤
root /Users/lihao/Desktop/work/tomcat/tomcat/webapps/ROOT/;
expires 30d; #緩存30天
}
若是訪問不到 用chmod 修改文件夾權(quán)限。