#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)對(duì)應(yīng)的http狀態(tài)碼時(shí),使用下面的頁面回應(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,并且在配置目錄,我們可以新建一個(gè)目錄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í)候設(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.瀏覽器輸入域名進(jìn)行訪問:http://status.index.ui/
Active connections: 4
server accepts handled requests
42 42 47
Reading: 0 Writing: 1 Waiting: 3
- Active connections:表示nginx正在處理的活動(dòng)連接數(shù)
- server :表示nginx啟動(dòng)到現(xiàn)在共處理了42個(gè)連接
- accepts :nginx啟動(dòng)到現(xiàn)在共成功建立了42次握手
丟失數(shù)=(握手?jǐn)?shù)-連接數(shù)),可以看到本此狀態(tài)顯示沒有丟失請(qǐng)求 - handled requests:表示共處理了47次請(qǐng)求
- Reading: 0 表示nginx讀取到客戶端的hander信息數(shù)
- Writing: 1 表示nginx返回給客戶端的hander信息數(shù)
- Waiting: 3 表示nginx已經(jīng)處理完正在等候下一次請(qǐng)求指令的駐留連接。在開啟keepalive的情況下,這個(gè)值等于active-(reading+writing)
為了安全。這個(gè)狀態(tài)信息要防止外部用戶查看??梢栽谂渲弥性O(shè)置允許和禁止的IP段訪問
location /nginx_conf {
stub_status on;
access_log off;
allow 10.0.0.0/24;
deny all;
}
allow和deny是設(shè)置允許和禁止的IP段訪問
nginx的錯(cuò)誤日志
nginx的錯(cuò)誤日志參數(shù)名為error_log,可以放在Main區(qū)塊中全局配置,也可以放置不同的虛擬主機(jī)中單獨(dú)記錄
語法:error_log file level
例如:error_log logs/error.log info;
日志級(jí)別有:debug、info、notice、warn、error、crit、alert、emerg,級(jí)別越高,記錄的信息越少。生產(chǎn)場(chǎng)景中一般是warn、error、crit這三個(gè)級(jí)別之一
可以放置的標(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:記錄訪問時(shí)間與時(shí)區(qū)
- time_local:用戶的http請(qǐng)求起始行信息
- status :http狀態(tài)碼,記錄請(qǐng)求返回的狀態(tài),例如:200、404、301等
- body_bytes_sent :服務(wù)器發(fā)送給客戶端的響應(yīng)body字節(jié)數(shù)
- http_referer:記錄此次請(qǐng)求是從哪個(gè)鏈接訪問過來的,可以根據(jù)referer進(jìn)行防盜鏈設(shè)置
- http_user_agent:記錄客戶端訪問信息,例如瀏覽器、手機(jī)客戶端等
- http_x_forwarded_for:當(dāng)前端有代理服務(wù)器時(shí),設(shè)置web節(jié)點(diǎn)記錄客戶端地址的配置,此參數(shù)生效的前提是代理服務(wù)器上也進(jìn)行了相關(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ū)的日志刷到磁盤的時(shí)間
gzip[=level] #表示壓縮級(jí)別
[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
腳本的實(shí)質(zhì)是將正在寫入的nginx日志改名為帶日期的格式,然后平滑重啟,生成新的nginx日志。
然后在定時(shí)任務(wù)增加每天0點(diǎn)執(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:此文件會(huì)被/etc/logrotate.conf引入進(jìn)來進(jìn)行輪訓(xùn)
/usr/local/nginx/logs/access.log{
daily
rotate 30
missingok
create
dateext
postrotate
/usr/local/nginx/sbin/nginx -s reload
endscript
}
使用debug方式驗(yàn)證:
logrotate -d /etc/logrotate.d/nginx
測(cè)試沒有問題的話,可以在定時(shí)任務(wù)執(zhí)行前,手動(dòng)執(zhí)行一次
logrotate -f /etc/logrotate.d/nginx
試驗(yàn)時(shí)發(fā)現(xiàn)沒有在0點(diǎn)進(jìn)行輪訓(xùn),是因?yàn)?etc/anacrontab 里設(shè)置的原因
參數(shù)如下:
RANDOM_DELAY=45 設(shè)置最大延遲時(shí)間 45分鐘
START_HOURS_RANGE=3-22 在3點(diǎn)到22點(diǎn)之間執(zhí)行

最下面的delay in minutes是延遲時(shí)間,cron.daily是指5分鐘后執(zhí)行
即使設(shè)置RANDOM_DELAY=0;START_HOURS_RANGE=0;daily的延遲時(shí)間也是0,但是還是沒有執(zhí)行在0點(diǎn)準(zhǔn)確切割,會(huì)有一分鐘的延遲
然后執(zhí)行:systemctl restart crond.service
nginx的location
location語法:
location [=|~|~*|^~|@] uri{
.....
}
- =是完全匹配
- ~區(qū)分大小寫(大小寫敏感)
- ~*大小寫不敏感
- ^~在進(jìn)行常規(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都不匹配則匹配此處 |
測(cè)試訪問
配置文件如下
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實(shí)現(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指括號(hào)內(nèi)容。
rewrite指令最后的flag標(biāo)記說明
last:本條規(guī)則匹配完成后,繼續(xù)向下匹配新的location uri規(guī)則
break:本條匹配規(guī)則完成即終止,不再向后匹配
redirect:返回302臨時(shí)重定向,瀏覽器地址會(huì)顯示跳轉(zhuǎn)后的url地址
permanent:返回301永久重定向,瀏覽器地址欄會(huì)顯示跳轉(zhuǎn)后的url地址。
last和break實(shí)現(xiàn)url重寫,地址不變,但是在服務(wù)端訪問路徑改變。redirect和permanent實(shí)現(xiàn)url跳轉(zhuǎn),瀏覽器地址欄會(huì)顯示跳轉(zhuǎn)后url地址。
使用alias指令時(shí)必須用last標(biāo)記,使用proxy_pass指令時(shí)要使用break標(biāo)記。last標(biāo)記會(huì)在本條rewrite規(guī)則執(zhí)行完畢后,對(duì)其所在的server{....}標(biāo)簽重新發(fā)起請(qǐng)求。而break標(biāo)記則會(huì)在本條規(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)證文件里,注意密碼是加密的。
生成證號(hào)密碼方式如下:
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啟動(dòng)用戶對(duì)首頁沒有讀的權(quán)限
3.存在index首頁但是首頁文件不對(duì)(實(shí)質(zhì)還是找不到首頁)
location / {
root html;
index index.html;
}