Nginx核心配置詳解
全局配置
user nginx nginx;
worker_processes [number | auto];
worker_cpu_affinity 00000001 00000010 00000100 00001000;
worker_cpu_affinity 0001 0010 0100 1000;
error_log /apps/nginx/logs/error.log error;
pid /apps/nginx/logs/nginx.pid;
worker_priority 0;
worker_rlimit_nofile 65536;
[root@s2 ~]
daemon off;
master_process off|on;
events {
worker_connections 65536;
use epoll;
accept_mutex on;
multi_accept on;
}
http詳細配置
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ /passwd.html {
deny all;
}
}
location /linux38/passwd.ht {
deny all;
}
核心配置示例
- 基於不同的IP、不同的端⼝以及不⽤得域名實現不同的虛擬主機,依賴於核⼼模塊ngx_http_core_module實現。
//修改主配置文件設置,子配置文件路徑
vim /apps/nginx/conf/nginx.conf
//http塊中添加,子配置文件路徑
include /apps/nginx/conf/conf.d/*.conf;
--------------------------
//新建編輯pc端子配置文件
vim /apps/nginx/conf/conf.d/pc.conf
//配置文件內容
server{
listen 80;
server_name www.pc.com;
location / {
root /data/nginx/html/pc;
}
}
---------------------------
//新建編輯mobile端子配置文件
vim /apps/nginx/conf/conf.d/mobile.conf
//配置文件內容
server{
listen 80;
server_name www.mobile.com;
location / {
root /data/nginx/html/mobile;
}
}
-----------------------------
//創建測試頁面
echo "this is pc web" > /data/nginx/html/pc/index.html
echo "this is mobile web" > /data/nginx/html/mobile/index.html
//重新加載配置文件
/apps/nginx/sbin/nginx -s reload
//修改windows中hosts文件內容添加
172.20.26.104 www.pc.com
172.20.26.104 www.mobile.com
- 瀏覽器訪問http://www.mobile.com/得
- root與alias
- root:指定web的家⽬錄,在定義location的時候,⽂件的絕對路徑等於 root+location,如:
//編輯pc端子配置文件
vim /apps/nginx/conf/conf.d/pc.conf
//配置文件如下
server{
listen 80;
server_name www.pc.com;
location / {
root /data/nginx/html/pc;
}
location /images{
root /data/nginx/html/pc
index index.html
}
}
----------------------------------------
//查看圖片存放位置
tree /data/nginx/html/pc/images/
/data/nginx/html/pc/images/
└── ms.jpg
- 瀏覽器訪問http://www.pc.com/images/ms.jpg得
- alias:定義路徑別名,會把訪問的路徑重新定義到其指定的路徑,如:
//編輯pc端子配置文件
vim /apps/nginx/conf/conf.d/pc.conf
server{
listen 80;
server_name www.pc.com;
location / {
root /data/nginx/html/pc;
}
location /images{
alias /data/nginx/html/pc
內容。
index index.html
}
}
-------------------------------------------
//查看圖片存放位置
tree /data/nginx/html/pc/
/data/nginx/html/pc/
├── images
├── index.html
└── ms.jpg
- 瀏覽器訪問http://www.pc.com/images/ms.jpg得
Nginx四層訪問控制
location /about {
alias /data/nginx/html/pc;
index index.html;
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}
Nginx賬戶認證功能
//安裝httpd工具包
yum -y install httpd-tools
apt -y isntall apache2-utils
//設置用戶1以及密碼
htpasswd -cbm /apps/nginx/conf/.htpasswd bokebi1 123456
Adding password for user bokebi1
//設置用戶2以及密碼
htpasswd -bm /apps/nginx/conf/.htpasswd bokebi2 123456
Adding password for user bokebi2
//查看密碼文件
tail /apps/nginx/conf/.htpasswd
user1:$apr1$Rjm0u2Kr$VHvkAIc5OYg.3ZoaGwaGq/
user2:$apr1$nIqnxoJB$LR9W1DTJT.viDJhXa6wHv.
//編輯pc端子配置文件
vim /apps/nginx/conf/conf.d/pc.conf
location = /login/ {
root /data/nginx/html/pc;
index index.html;
auth_basic "login password";
auth_basic_user_file /apps/nginx/conf/.htpasswd;
}
重啓nginx並訪問測試
自定義錯誤頁面
listen 80;
server_name www.magedu.net;
error_page 500 502 503 504 404 /error.html;
location = /error.html {
root html;
}
重啓nginx並訪問不存在的⻚⾯進⾏測試
自定義訪問日誌
[root@s2 ~]
listen 80;
server_name www.magedu.net;
error_page 500 502 503 504 404 /error.html;
access_log /data/nginx/logs/www-magedu-net_access.log;
error_log /data/nginx/logs/www-magedu-net_error.log;
location = /error.html {
root html;
}
重啓nginx並訪問不存在的⻚⾯進⾏測試並驗證是在指定⽬錄⽣成新的⽇志⽂件
檢測文件是否存在
- try_files會按順序檢查⽂件是否存在,返回第⼀個找到的⽂件或⽂件夾(結尾加斜線表⽰爲⽂件夾),如果所有⽂件或⽂件夾都找不到,會進⾏⼀個內部重定向到最後⼀個參數。只有最後⼀個參數可以引起⼀個內部重定向,之前的參數只設置內部URI的指向。最後⼀個參數是回退URI且必須存在,否則會出現內部500錯誤。
location /about {
root /data/nginx/html/pc;
index index.html;
try_files $uri $uri/index.html $uri.html =489;
}
[root@s2 ~]
重啓nginx並測試,當訪問到http://www.pc.com/about/xx.html等不存在的uri會顯⽰default,如果
是⾃定義的狀態碼則會顯⽰在返回數據的狀態碼中,如:
[root@s2 about]
HTTP/1.1 489
Server: nginx
Date: Thu, 21 Feb 2019 00:11:40 GMT
Content-Length: 0
Connection: keep-alive
Keep-Alive: timeout=65
長連接配置
- keepalive_timeout number; #設定保持連接超時時⻓,0表⽰禁⽌⻓連接,默認爲75s,通常配置在http字段作爲站點全局配置 keepalive_requests number; #在⼀次⻓連接上所允許請求的資源的最⼤數量,默認爲100次
keepalive_requests 3;
keepalive_timeout 65 65;
開啓⻓連接後,返回客⼾端的會話保持時間爲60s,單次⻓連接累計請求達到指定次數請求或65秒就會被斷開,後⾯的
60爲發送給客⼾端應答報⽂頭部中顯⽰的超時時間設置爲60s:如不設置客⼾端將不顯⽰超時時間。
Keep-Alive:timeout=60
如果設置爲0表⽰關閉會話保持功能,將如下顯⽰:
Connection:close
//使⽤命令測試:
telnet www.magedu.net 80
Trying 172.18.200.102...
Connected to www.pc.com
Escape character is '^]'.
GET / HTTP/1.1
HOST: www.pc.com
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Thu, 14 Mar 2019 17:23:46 GMT
Content-Type: text/html
Content-Length: 7
Last-Modified: Thu, 14 Mar 2019 14:54:50 GMT
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "5c8a6b3a-7"
Accept-Ranges: bytes
pc web
作爲下載服務器配置
//創建所需目錄
mkdir /data/nginx/html/pc/download
//編輯配置文件
vim /apps/nginx/conf/conf.d/pc.conf
location /download {
autoindex on;
autoindex_exact_size on;
gb)
autoindex_localtime on;
root /data/nginx/html/pc;
}
limit_rate rate;
限速與不限速的對⽐:
limit_rate 10k;
- 瀏覽器訪問http://www.pc.com/download/得
作爲上傳服務器配置
client_max_body_size 1m;
client_body_buffer_size size;
此⼤⼩時,其將被暫存到磁盤上的由下⾯client_body_temp_path指令所定義的位置
client_body_temp_path path [level1 [level2 [level3]]];
值從後往前截取1位、2位、2位作爲⽂件名:
[root@s3 ~]
95f6f65f498c74938064851b1bb 96 3d 4 /data/nginx/html/pc/index.html
1級⽬錄佔1位16進制,即2^4=16個⽬錄 0-f
2級⽬錄佔2位16進制,即2^8=256個⽬錄 00-ff
3級⽬錄佔2位16進制,即2^8=256個⽬錄 00-ff
配置⽰例(一般作爲默認配置,所以放置在主配置文件裏):
client_max_body_size 10m;
client_body_buffer_size 16k;
client_body_temp_path /apps/nginx/temp 1 2 2;
其他配置
limit_except
- 限制客戶端使用除了指定的請求方法之外的其它方法,只能用在location上下文。
GET, HEAD, POST, PUT, DELETE
MKCOL, COPY, MOVE, OPTIONS, PROPFIND,
PROPPATCH, LOCK, UNLOCK, PATCH
limit_except GET HEAD POST {
deny 192.168.111.200
allow 192.168.111.0/24;
deny all;
}
- 表示除了GET、HEAD、POST方法其他方法都限制,
- 主機範圍爲:禁止192.168.111.200、允許192.168.111.0/24、禁止所有。
- 即僅允許192.168.111.0網段訪問,但是禁止192.168.111.200的地址訪問
aio
- linux 2.6以上內核提供以下⼏個系統調⽤來⽀持aio:
- 1、SYS_io_setup:建⽴aio 的context
- 2、SYS_io_submit: 提交I/O操作請求
- 3、SYS_io_getevents:獲取已完成的I/O事件
- 4、SYS_io_cancel:取消I/O操作請求
- 5、SYS_io_destroy:毀銷aio的context
directio
- 是否同步寫磁盤
- 操作完全和aio相反,aio是讀取⽂件⽽directio是寫⽂件到磁盤
- 啓⽤直接I/O,默認爲關閉,當⽂件⼤於等於給定⼤⼩時,例如directio 4m,同步(直接)寫磁盤,⽽⾮寫緩存。
open_file_cache
- 是否緩存打開過的⽂件信息
- nginx可以緩存以下三種信息:
(1) ⽂件元數據:⽂件的描述符、⽂件⼤⼩和最近⼀次的修改時間
(2) 打開的⽬錄結構
(3) 沒有找到的或者沒有權限訪問的⽂件的相關信息
- max=N:可緩存的緩存項上限數量;達到上限後會使⽤LRU(Least recently used,最近最少使⽤)算法實現
管理
- inactive=time:緩存項的⾮活動時⻓,在此處指定的時⻓內未被命中的或命中的次數少於
open_file_cache_min_uses指令所指定的次數的緩存項即爲⾮活動項,將被刪除
open_file_cache_errors
- 是否緩存查找時發⽣錯誤的⽂件⼀類的信息(默認值爲off)
open_file_cache_min_uses
- open_file_cache指令的inactive參數指定的時⻓內,⾄少被命中此處指定的次數⽅可被歸類爲活動項(默認值爲1)
open_file_cache_valid
- 緩存項有效性的檢查驗證頻率(默認值爲60s)
- 示例
aio on;
directio 4m;
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;