#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
#nginx支持的媒體類型庫文件
include mime.types;
#默認(rèn)的媒體類型
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
#開啟高效傳輸
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.index.ui;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#出現(xiàn)對應(yīng)的http狀態(tài)碼時椎木,使用下面的頁面回應(yīng)客戶
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
基于域名的配置(核心配置如下)
server {
listen 80;
server_name www.index.ui;
location / {
root html/www;
index index.html index.htm;
}
}
nginx的多server規(guī)范使用
nginx的主配置文件為/usr/local/nginx/conf/nginx.conf,并且在配置目錄茅信,我們可以新建一個目錄esp用來存放各類server配置文件
例如:/usr/local/nginx/conf/esp/www.conf
server {
listen 80;
server_name www.index.ui;
location / {
root html/www;
index index.html index.htm;
}
}
最后只需要在/usr/local/nginx/conf/nginx.conf主配置文件使用include引入即可。
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include esp/www.conf;
}
nginx的status模塊介紹
如果想要使用status模塊,需要在./configure編譯時候設(shè)置--with-http_stub_status_module
[root@localhost nginx]# sbin/nginx -V
nginx version: nginx/1.15.4
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx-1.15.4 --with-http_stub_status_module --with-http_ssl_module
配置過程如下:
1.增加配置到配置文件
server {
listen 80;
server_name status.index.ui;
location / {
stub_status on; #打開狀態(tài)信息開關(guān)
access_log off;
}
}
2.添加server到主配置文件
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include esp/www.conf;
include esp/qwer.conf;
include esp/status.conf;
}
3.配置host解析(在windows的C:\Windows\System32\drivers\etc\hosts目錄)
192.168.100.24 status.index.ui
4.瀏覽器輸入域名進行訪問:http://status.index.ui/
Active connections: 4
server accepts handled requests
42 42 47
Reading: 0 Writing: 1 Waiting: 3
- Active connections:表示nginx正在處理的活動連接數(shù)
- server :表示nginx啟動到現(xiàn)在共處理了42個連接
- accepts :nginx啟動到現(xiàn)在共成功建立了42次握手
丟失數(shù)=(握手?jǐn)?shù)-連接數(shù))资昧,可以看到本此狀態(tài)顯示沒有丟失請求 - handled requests:表示共處理了47次請求
- Reading: 0 表示nginx讀取到客戶端的hander信息數(shù)
- Writing: 1 表示nginx返回給客戶端的hander信息數(shù)
- Waiting: 3 表示nginx已經(jīng)處理完正在等候下一次請求指令的駐留連接楞慈。在開啟keepalive的情況下幔烛,這個值等于active-(reading+writing)
為了安全。這個狀態(tài)信息要防止外部用戶查看囊蓝∷当矗可以在配置中設(shè)置允許和禁止的IP段訪問
location /nginx_conf {
stub_status on;
access_log off;
allow 10.0.0.0/24;
deny all;
}
allow和deny是設(shè)置允許和禁止的IP段訪問
nginx的錯誤日志
nginx的錯誤日志參數(shù)名為error_log,可以放在Main區(qū)塊中全局配置,也可以放置不同的虛擬主機中單獨記錄
語法:error_log file level
例如:error_log logs/error.log info;
日志級別有:debug慎颗、info、notice言询、warn俯萎、error、crit运杭、alert夫啊、emerg,級別越高辆憔,記錄的信息越少撇眯。生產(chǎn)場景中一般是warn、error虱咧、crit這三個級別之一
可以放置的標(biāo)簽段為:main熊榛、http、server腕巡、location
nginx的訪問日志
nginx的訪問日志由log_format和access_log參數(shù)控制
nginx默認(rèn)配置如下
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
nginx記錄日志默認(rèn)參數(shù)配置如下
access_log logs/access.log main;
nginx的日志變量:
- remote_addr :記錄訪問客戶端的地址
- remote_user :客戶端用戶名
- time_local:記錄訪問時間與時區(qū)
- time_local:用戶的http請求起始行信息
- status :http狀態(tài)碼玄坦,記錄請求返回的狀態(tài),例如:200绘沉、404煎楣、301等
- body_bytes_sent :服務(wù)器發(fā)送給客戶端的響應(yīng)body字節(jié)數(shù)
- http_referer:記錄此次請求是從哪個鏈接訪問過來的,可以根據(jù)referer進行防盜鏈設(shè)置
- http_user_agent:記錄客戶端訪問信息车伞,例如瀏覽器择懂、手機客戶端等
- http_x_forwarded_for:當(dāng)前端有代理服務(wù)器時,設(shè)置web節(jié)點記錄客戶端地址的配置另玖,此參數(shù)生效的前提是代理服務(wù)器上也進行了相關(guān)的x_forwarded_for設(shè)置困曙。
access_log參數(shù)說明:
access_log off; #關(guān)閉access_log,即不記錄訪問日志
access_log path [format [buffer=size [flush=time]] [if=condition]];
access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition];
access_log syslog:server=address[,parameter=value] [format [if=condition]];
buffer=size #為存放訪問日志的緩沖區(qū)大小
flush=time #為緩沖區(qū)的日志刷到磁盤的時間
gzip[=level] #表示壓縮級別
[if = condition] #表示其他條件
nginx訪問日志輪訓(xùn)切割
切割腳本如下:
#!/bin/bash
Dateformat=`date +%Y%m%d`
Basedir="/usr/local/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access.log"
[ -d $Nginxlogdir ]&&cd $Nginxlogdir||exit 1
[ -f $Logname ]&&mv $Logname ${Dateformat}_$Logname||exit 1
$Basedir/sbin/nginx -s reload
腳本的實質(zhì)是將正在寫入的nginx日志改名為帶日期的格式日矫,然后平滑重啟赂弓,生成新的nginx日志。
然后在定時任務(wù)增加每天0點執(zhí)行腳本
cat >>/var/spool/cron/root <<EOF
cut nginx access log
00 00 * * * /bin/bash /root/cut_nginx_log.sh >/dev/null 2>&1
EOF
第二種方法:使用日志輪訓(xùn)哪轿,直接切割指定文件日志
新建文件:/etc/logrotate.d/nginx
ps:此文件會被/etc/logrotate.conf引入進來進行輪訓(xùn)
/usr/local/nginx/logs/access.log{
daily
rotate 30
missingok
create
dateext
postrotate
/usr/local/nginx/sbin/nginx -s reload
endscript
}
使用debug方式驗證:
logrotate -d /etc/logrotate.d/nginx
測試沒有問題的話盈魁,可以在定時任務(wù)執(zhí)行前,手動執(zhí)行一次
logrotate -f /etc/logrotate.d/nginx
試驗時發(fā)現(xiàn)沒有在0點進行輪訓(xùn)窃诉,是因為/etc/anacrontab 里設(shè)置的原因
參數(shù)如下:
RANDOM_DELAY=45 設(shè)置最大延遲時間 45分鐘
START_HOURS_RANGE=3-22 在3點到22點之間執(zhí)行
最下面的delay in minutes是延遲時間杨耙,cron.daily是指5分鐘后執(zhí)行
即使設(shè)置RANDOM_DELAY=0赤套;START_HOURS_RANGE=0;daily的延遲時間也是0珊膜,但是還是沒有執(zhí)行在0點準(zhǔn)確切割容握,會有一分鐘的延遲
然后執(zhí)行:systemctl restart crond.service
nginx的location
location語法:
location [=|~|~*|^~|@] uri{
.....
}
- =是完全匹配
- ~區(qū)分大小寫(大小寫敏感)
- ~*大小寫不敏感
- ^~在進行常規(guī)的字符串匹配檢查后,不做正則表達(dá)式檢查
location匹配的優(yōu)先順序
順序 | 不用URI及特殊字符組合匹配 | 說明 |
---|---|---|
1 | location = / | 精確匹配/ |
2 | location ^~/image/ | 匹配常規(guī)字符串车柠,不做正則匹配檢查 |
3 | location ~* .(gif|jpg|png) | 正則匹配 |
4 | location /document/ | 匹配常規(guī)字符串剔氏,如果有正則,則優(yōu)先匹配正則 |
5 | location / | 所有l(wèi)ocation都不匹配則匹配此處 |
測試訪問
配置文件如下
server {
listen 80;
server_name localhost;
location / {
return 401;
}
location =/ {
return 402;
}
location /documents/ {
return 403;
}
location ^~ /images/ {
return 404;
}
location ~* \.(gif|jpg|png)$ {
return 500;
}
}
curl 訪問命令:
curl -s -I -o /dev/null -w "%{http_code}\n" 192.168.100.148
nginx rewrite語法
語法:rewrite regex replacement [flag]竹祷;
默認(rèn)值:none
應(yīng)用位置:server谈跛、location、if
rewrite實現(xiàn)url重寫塑陵,根據(jù)regex部分的內(nèi)容感憾,重定向到replacement 部分,結(jié)尾flag標(biāo)記
例如:rewrite ^/(.*)$ http://www.baidu.com/$1 permanent;
這里^/(.*)$ 指的是以/開頭令花,任意內(nèi)容結(jié)尾阻桅,即匹配所有,匹配成功后跳轉(zhuǎn)到百度兼都,$1指括號內(nèi)容嫂沉。
rewrite指令最后的flag標(biāo)記說明
last:本條規(guī)則匹配完成后,繼續(xù)向下匹配新的location uri規(guī)則
break:本條匹配規(guī)則完成即終止扮碧,不再向后匹配
redirect:返回302臨時重定向输瓜,瀏覽器地址會顯示跳轉(zhuǎn)后的url地址
permanent:返回301永久重定向,瀏覽器地址欄會顯示跳轉(zhuǎn)后的url地址芬萍。
last和break實現(xiàn)url重寫尤揣,地址不變,但是在服務(wù)端訪問路徑改變柬祠。redirect和permanent實現(xiàn)url跳轉(zhuǎn)北戏,瀏覽器地址欄會顯示跳轉(zhuǎn)后url地址。
使用alias指令時必須用last標(biāo)記漫蛔,使用proxy_pass指令時要使用break標(biāo)記嗜愈。last標(biāo)記會在本條rewrite規(guī)則執(zhí)行完畢后,對其所在的server{....}標(biāo)簽重新發(fā)起請求莽龟。而break標(biāo)記則會在本條規(guī)則匹配完成后蠕嫁,終止匹配,不再匹配后面的規(guī)則毯盈。
nginx訪問認(rèn)證
有一些無需密碼即可訪問的內(nèi)容剃毒,可以用nginx設(shè)置訪問認(rèn)證,防止信息泄露
location / {
auth_basic "closed site";
auth_basic_user_file /usr/local/nginx/passwd.db;
}
參數(shù):
- auth_basic
意思是輸入信息提示
語法:auth_basic string|off ;
默認(rèn)值auth_basic off;
使用位置:http、server赘阀、location益缠、limit_except - auth_basic_user_file
語法:auth_basic_user_file file
默認(rèn)值:----
使用位置:http、server基公、location幅慌、limit_except
auth_basic_user_file 參數(shù)后接認(rèn)證密碼文件,file內(nèi)容如下:
name1:password1
name2:password2:comment
name3:password3
可以使用apache自帶的htpasswd和oppenssl passwd命令設(shè)置用戶和密碼到認(rèn)證文件里轰豆,注意密碼是加密的胰伍。
生成證號密碼方式如下:
yum install -y httpd
htpasswd -bc /usr/local/nginx/pd baihua 123456
chmod 400 /usr/local/nginx/pd
chown nginx /usr/local/nginx/pd
訪問出現(xiàn)403的條件
1.沒有首頁文件
location / {
root html;
}
2.當(dāng)前nginx啟動用戶對首頁沒有讀的權(quán)限
3.存在index首頁但是首頁文件不對(實質(zhì)還是找不到首頁)
location / {
root html;
index index.html;
}