簡介
百度百科:Nginx是一款輕量級、高性能的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,在BSD-like 協議下發行。其特點是佔有內存少,併發能力強。
常見應用場景
- 反向代理
- 負載均衡
- 動靜分離
- 緩存服務
常用命令
【操作nginx命令需要進入nginx安裝目錄的sbin下】:
*** sbin下有nginx文件,由於要操作該文件,.代表當前目錄,./代表當前目錄下的文件,因此就是 ./nginx
查看nginx版本號: ./nginx -v
啓動nginx: ./nginx
關閉nginx: ./nginx -s stop
重新加載nginx: ./nginx -s reload
nginx配置文件介紹
位置:nginx安裝目錄的conf文件夾下,nginx.conf
... // 全局塊
events { // events塊
...
}
http { // http塊
... // http全局塊
server { // server塊
... // server全局塊
location [Pattern] { // location塊
...
}
location [Pattern] {
...
}
}
server {
...
location [Pattern] {
...
}
location [Pattern] {
...
}
}
...
}
全局塊
主要包括配置運行Nginx服務器的用戶、允許生成的worker process數、日誌存放路徑、配置文件引入等;worker process值越大,支持的併發量越高。
events塊
設置每個work process最大連接數等,該部分主要影響Nginx性能。
http塊
- http全局塊:包含日誌定義、超時時間等
- server塊:每個server塊相當於一個虛擬主機
- server全局塊:配置本虛擬機監聽配置
- location塊:基於Nginx接收到的請求,對其進行匹配處理、地址定向、數據緩存等
反向代理舉例
1、客戶端訪問nginx服務器http:80端口,轉發至nginx所在服務器的8080端口,假設nginx所在服務器IP爲1.1.1.1
server{
listen 80;
server_name 1.1.1.1(或域名);
location / {
proxy_pass http://127.0.0.1:8080;
}
}
2、 根據客戶端訪問的不通路徑跳轉到不通服務端口
- 訪問http://1.1.1.1:6666/test1/ 轉發至8081端口
- 訪問http://1.1.1.1:6666/test2/ 轉發至8082端口
server{
listen 6666;
server_name 1.1.1.1(或域名);
location /test1 {
proxy_pass http://127.0.0.1:8081;
}
location /test2 {
proxy_pass http://127.0.0.1:8082;
}
}
負載均衡舉例
根據客戶端訪問nginx服務器http:80端口,平均轉發至nginx所在服務器的8080、8081、8082端口,實現負載均衡效果。假設nginx所在服務器IP爲1.1.1.1
upstream代表負載均衡服務器列表
http{
upstream servertest{
server 1.1.1.1:8080 weight=2;
server 1.1.1.1:8081;
server 1.1.1.1:8002;
}
server{
listen 80;
server_name 1.1.1.1(或域名);
location / {
proxy_pass http://servertest;
}
}
}
分配策略:
- 輪詢(默認)
- weight(權重):默認爲1,權重越高分配的客戶端越多,例如upstream中 server 1.1.1.1:8080 weight=2;
- ip_hash:每個客戶端請求按照訪問的ip哈希值分配,這樣只要IP不變,每個客戶端固定訪問一個後端服務器,可以解決session的問題
upstream{
ip_hash;
server xxxx;
server xxxx;
}
日誌
Nginx在安裝之後會默認開始日誌,分爲error_log(錯誤日誌)、 access_log(訪問日誌),默認日誌路徑爲~nginx/logs。在全局塊、http塊、server塊、location塊都可以配置日誌,作用域不同。
error_log
記錄http請求錯誤以及nginx本身的運行錯誤
不能設置日誌打印格式,但可設置打印級別,低--->高:info、notice、warn、error、crit、alert、emert等,設置某一級別會打印>=該級別日誌。
默認配置:error_log logs/error.log error; 會打印error及以上
支持的作用域:全局、http、server、location
access_log
可以通過log_format設置日誌打印格式
默認配置:access_log logs/access.log main;
其中main是log_format的一個格式名稱。 假設你可以把main改成temp,那麼相應的就要有一個log_format temp 'xxx'來定義格式
支持的作用域:http、server、location
log_format
默認格式:log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
支持的作用域:http
如果沒有對應的數據則默認使用-代替
---------------------------部分Nginx內置變量-------------------------
$remote_addr:客戶端的IP地址
$remote_user:客戶端用戶名
$time_local:當前日誌條目的時間
$request:HTTP請求的方法、URL和HTTP協議版本
$status:HTTP請求狀態 code
$body_bytes_sent:nginx響應客戶端請求時,發送到客戶端的字節數,不包含響應頭的大小
$http_referer:引用地址
$http_user_agent:客戶端瀏覽器信息
$http_x_forwarded_for:網絡訪問路徑
將log_format格式修改成如下格式,然後重啓nginx纔可生效。對比效果如下圖。
log_format main 'SURPRISE ~~~ | $time_local | $remote_addr | $http_host | $status | $request_time | $http_host | $request | $http_referer';