一、自定義錯誤狀態碼頁面
支持404,403,500,502,503,504等狀態碼,並且針對於proxy_pass當中realserver出現的錯誤碼,nginx無法提供錯誤狀態碼頁面,也就是不支持反向代理的錯誤狀態碼自定義,nginx只是轉發而已,nginx針對nginx提供本地的web服務才能支持。
error_page 可放在http
, server
, location中
方法一: error_page 404 =200 /404.html; #格式中“=”要去緊跟着數字,不能用空格 location = /404.html { root /var/nginx/html/error; #/var/nginx/html/error/404.html } 方法二: error_page 403 =200 http://www.baidu.com #直接重定向到其他的鏈接上去,並且以200的響應碼來返回
截圖:
二、Nginx的訪問控制
Nginx的訪問控制分爲IP 和用戶名密碼控制
方式一:使用IP來實現訪問控制 location / { root html; index index.html index.htm; allow 192.168.192.202; deny all; #允許192.168.192.202的訪問,拒絕其他所有人的訪問 }
方式二:使用用戶名密碼來實現控制 location / { root html; index index.html index.htm; auth_basic "友情公告:您已進入需要授權頁面,請輸入您的用戶信息"; auth_basic_user_file /etc/nginx/htpasswd; #認證的用戶名密碼文件 } [root@#localhost html]# htpasswd -c -m /etc/nginx/htpasswd tom #將tom用戶添加到文件中去
方式三:同時使用ip+用戶名密碼控制 將IP和用戶名密碼混在一起即可,表示要滿足IP,並且又要認證通過方可訪問。如果不滿足IP則直接403,如果滿足IP,但是用戶密碼錯誤也是403.
三、error_log和access_log發送日誌到遠程rsyslog主機
error_log配置:
Syntax: error_log file | syslog:server=address[,parameter=value] [debug | info | notice| warn | error | crit | alert | emerg]; Default: error_log logs/error.log error; Content: main, http, server, location
要禁用錯誤日誌,不能使用“error_log off;”,而要使用類似如下選項:
error_log /dev/null crit;
Nginx配置:
error_log syslog:server=192.168.112.130,facility=local1 info; #將error中得info以及以上的信息發送到192.168.112.130,定義發送的設備爲local1
192.168.112.130的Rsyslog配置:
# vi /etc/rsyslog.conf ##添加如下行到最前面 local1.* /var/log/nginx.err.log #將接受到得local1得日誌保存在/var/log/nginx.err.log中
驗證截圖:
access_log配置:
Syntax: 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]]; access_log off; Default:access_log logs/access.log combined; Content:http, server, location, if in location, limit_except
Nginx配置:
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 "syslog:server=192.168.112.130,facility=local0" main; ##將日誌保存在130的rsyslog服務器,設備爲local0,格式爲main
130的rsyslog配置:
# vi /etc/rsyslog.conf #在最前面添加如下配置 local0.* /var/log/nginx.acc.log
驗證截圖:
四、開啓Nginx的status監控狀態輸出
location /basic_status { #指定哪個url stub_status on; #開啓stub_status功能 allow x.x.x.x; #acl限制 deny all; }
效果圖:
五、rewrite重寫URL
Syntax: rewrite regex replacement [flag]; Content: server, location, if flag: last 後面的rewrite直接跳過,重頭開始匹配 break 跳出循環 continue 繼續 redirect 重定向到302,url會跟着變化 permanent 重定向到301,url也跟着變化
實例一:將/bbs的訪問請求,重定向到/forum去
1. ..... location / { root /var/www/html; index index.html; location /bbs { rewrite /bbs /forum permanent; #將/bbs重寫爲/forum } } .....
2. # mkdir /var/www/html/forum # echo bbs > /var/www/html/forum/index.html
效果圖:
實例二:根據不同的瀏覽器類型定向到響應瀏覽器的頁面
六、禁止用戶直接以IP方式訪問web
方法一: 在server的上面再添加一個server
server { listen 80 default_server; server_name www.kfc.com; rewrite ^(.*)$ http://$request_uri last; #當用戶以ip直接訪問的時候會被重寫爲www.q.com的訪問 } server { listen 80; server_name www.q.com; #charset koi8-r; ........ .....
方法二:直接在請求報文的host字段上判斷請求的是否爲ip,ip就直接重寫
location / { if ( $host ~ "192.168.192.248" ) { rewrite ^ http://www.q.com last; ##發現host字段爲本機ip地址的時候直接重寫到www.q.com去 } root html; index index.php index.html index.htm; }