Nginx(二)

利用include功能優化nginx的配置文件

  1. [root@lnmp conf]# cat nginx.conf
  2. worker_processes 1;
  3. events {
  4.     worker_connections 1024;
  5. }
  6. http {
  7.     include mime.types;
  8.     default_type application/octet-stream;
  9.     sendfile on;
  10.     keepalive_timeout 65;
  11.     #nginx vhosts config
  12.     include extra/www.conf;
  13.     include extra/bbs.conf;
  14.     include extra/blog.conf;
  15. }

寫配置文件 html

  1. [root@lnmp conf]# mkdir extra
  2. [root@lnmp conf]# sed -n '18,25p' nginx.conf.base-name >extra/bbs.conf
  3. [root@lnmp conf]# cat extra/bbs.conf
  4.     server {
  5.         listen 80;
  6.         server_name bbs.etiantian.org;
  7.         location / {
  8.             root html/bbs;
  9.             index index.html index.htm;
  10.         }
  11.     }
  12. [root@lnmp conf]# sed -n '26,33p' nginx.conf.base-name >extra/blog.conf
  13. [root@lnmp conf]# cat extra/blog.conf
  14.    server {
  15.         listen 80;
  16.         server_name blog.etiantian.org;
  17.         location / {
  18.             root html/blog;
  19.             index index.html index.htm;
  20.         }
  21.     }
  22. [root@lnmp conf]# cat extra/www.conf
  23.     server {
  24.         listen 80;
  25.         server_name www.etiantian.org;
  26.         location / {
  27.             root html/www;
  28.             index index.html index.htm;
  29.         }
  30.     }

重啓服務 前端

  1. [root@lnmp conf]# /application/nginx/sbin/nginx -t
  2. nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
  3. nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
  4. [root@lnmp conf]# /application/nginx/sbin/nginx -s reload

測試 nginx

  1. [root@lnmp conf]# curl www.etiantian.org
  2. www.etiantian.org
  3. [root@lnmp conf]# curl bbs.etiantian.org
  4. bbs.etiantian.org
  5. [root@lnmp conf]# curl blog.etiantian.org
  6. blog.etiantian.org

nginx虛擬主機別名的配置

  1. [root@lnmp conf]# cat extra/www.conf
  2.     server {
  3.         listen 80;
  4.         server_name www.etiantian.org etiantian.org;
  5.         location / {
  6.             root html/www;
  7.             index index.html index.htm;
  8.         }
  9.     }
  10. [root@lnmp conf]# cat /etc/hosts
  11. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  12. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  13.  
  14. 192.168.31.132 server
  15. 192.168.31.133 lamp
  16. 192.168.31.134 lnmp www.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org
  17. 192.168.31.136 backup
  18. [root@lnmp conf]# /application/nginx/sbin/nginx -t
  19. nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
  20. nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
  21. [root@lnmp conf]# /application/nginx/sbin/nginx -s reload
  22. [root@lnmp conf]# curl etiantian.org
  23. www.etiantian.org
  24. [root@lnmp conf]# curl www.etiantian.org
  25. www.etiantian.org

啓動報錯: web

  1. [root@lnmp ~]# /application/nginx/sbin/nginx -s reload
  2. nginx: [error] invalid PID number "" in "/application/nginx-1.6.3/logs/nginx.pid"

解決辦法: 瀏覽器

  1. [root@lnmp ~]# /application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
  2. [root@lnmp ~]# /application/nginx/sbin/nginx -s reload

nginx狀態信息配置

  1. [root@lnmp extra]# cat status.conf
  2. #status
  3. server{
  4.     listen 80;
  5.     server_name status.etiantian.org;
  6.     location / {
  7.         stub_status on;
  8.         access_log off;
  9.         }
  10.  
  11.     }

添加包含 服務器

  1. include extra/status.conf;

重啓nginx,瀏覽器訪問 app

第一個server表示nginx啓動到如今共處理了多少個鏈接。 dom

第二個accepts表示nginx啓動到如今共建立了多少次握手,請求丟失次數=(握手次數-鏈接次數)。 curl

第三個handled request表示總共處理了多少次請求。 工具

Reading:nginx讀取到客戶端的header信息數。

writing:nginx返回給客戶端的header信息素。

waiting:nginx已經處理完正在等候下一次請求指令的駐留鏈接,開啓keep-alive的狀況下,這個值等於active-(reading+writing)。

nginx錯誤日誌

常見的錯誤日誌級別有[debug|info|notice|warn|error|crit|alert|emerg],級別越高記錄的信息越少,生產場景通常是warm|error|crit這三個級別之一,注意不要配置info等級較低的級別,會帶來磁盤I/O消耗。

error_log的默認值爲:error_log logs/error.log error

  1. [root@lnmp conf]# cat nginx.conf
  2. worker_processes 1;
  3.  
  4. error_log logs/error.log error;
  5. events {
  6.     worker_connections 1024;
  7. }
  8. http {
  9.     include mime.types;
  10.     default_type application/octet-stream;
  11.     sendfile on;
  12.     keepalive_timeout 65;
  13.     #nginx vhosts config
  14.     include extra/www.conf;
  15.     include extra/bbs.conf;
  16.     include extra/blog.conf;
  17. }

能夠放置的標籤段爲:main,http,server,location。

訪問日誌

log_format:用來定義記錄日誌的格式(能夠定義多種日誌格式,取不一樣名字便可)。

access_log:用來指定日誌文件的路徑及使用的何種日誌格式記錄日誌。

  1. [root@lnmp conf]# cat nginx.conf
  2. worker_processes 1;
  3.  
  4. error_log logs/error.log error;
  5. events {
  6.     worker_connections 1024;
  7. }
  8. http {
  9.     include mime.types;
  10.     default_type application/octet-stream;
  11.     sendfile on;
  12.     keepalive_timeout 65;
  13.  
  14.     log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15.                       '$status $body_bytes_sent "$http_referer" '
  16.                       '"$http_user_agent" "$http_x_forwarded_for"';
  17.  
  18.  
  19.     #nginx vhosts config
  20.     include extra/www.conf;
  21.     include extra/bbs.conf;
  22.     include extra/blog.conf;
  23. }

$remote_addr:記錄訪問網站的客戶端地址。

$http_x_forwarded_for:當前端有代理服務器時,設置web節點記錄客戶端地址的配置,此參數生效的前提是代理服務器上也要進行相關的x_forwarded_for設置。

$remote_user:遠程客戶端用戶名稱。

$time_local:記錄訪問時間與時區。

$request:用戶的http請求起始行信息。

$status:http狀態碼,記錄請求返回的狀態,例如:200、40四、301等。

$body_bytes_sent:服務器發送給客戶端的響應body字節數。

$http_referer:記錄這次請求是從哪一個連接訪問過來的,能夠根據referer進行防盜鏈設置。

$http_user_agent:記錄客戶端訪問信息,例如:瀏覽器、手機客戶端等。

access_log off;這裏的off表示不記錄訪問日誌。

默認配置:access_log logs/access.log combined;

放置位置:http,server,location,if in location,limit_except。

  1. [root@lnmp conf]# cat extra/www.conf
  2.     server {
  3.         listen 80;
  4.         server_name www.etiantian.org etiantian.org;
  5.         location / {
  6.             root html/www;
  7.             index index.html index.htm;
  8.         }
  9.         access_log logs/access_www.log main;
  10.     }

重啓nginx

  1. [root@lnmp conf]# /application/nginx/sbin/nginx -t
  2. nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
  3. nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
  4. [root@lnmp conf]# /application/nginx/sbin/nginx -s reload
  5. [root@lnmp conf]# tail -f ../logs/access_www.log #訪問日誌
  6. 192.168.31.1 - - [16/Feb/2017:23:35:38 +0800] "GET / HTTP/1.1" 200 18 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" "-"
  7. 192.168.31.1 - - [16/Feb/2017:23:35:38 +0800] "GET /favicon.ico HTTP/1.1" 404 570 "http://www.etiantian.org/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" "-"

日誌輪詢

  1. [root@lnmp scripts]# cat cut_nginx_log.sh
  2. #!/bin/sh
  3. Dateformat=`date +%Y%m%d`
  4. Basedir="/application/nginx"
  5. Nginxlogdir="$Basedir/logs"
  6. Logname="access_www"
  7. [ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1
  8. [ -f ${Logname}.log ]||exit 1
  9. /bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
  10. $Basedir/sbin/nginx -s reload

執行

  1. [root@lnmp scripts]# sh cut_nginx_log.sh
  2. [root@lnmp scripts]# ll /application/nginx/logs/
  3. total 24
  4. -rw-r--r--. 1 root root 978 Feb 16 23:35 20170216_access_www.log

設置定時任務

  1. #nginx log cut
  2. 00 00 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1

nginx經常使用日誌收集及分析工具備rsyslog、awstats、flume、ELK、storm等。

rewrite

rewrite指令語法

指令語法:rewrite regex replacement [flag];

默認值:none

應用位置:server,location,if。

  1. [root@lnmp conf]# cat extra/www.conf
  2.     server {
  3.         listen 80;
  4.         server_name etiantian.org;
  5.         rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
  6.     }
  7.  
  8.     server {
  9.         listen 80;
  10.         server_name www.etiantian.org;
  11.         location / {
  12.             root html/www;
  13.             index index.html index.htm;
  14.         }
  15.         access_log logs/access_www.log main;
  16.     }

建立oldboy.html而後進行訪問

  1. [root@lnmp www]# curl etiantian.org/oldboy.html -I
  2. HTTP/1.1 301 Moved Permanently
  3. Server: nginx/1.6.3
  4. Date: Thu, 16 Feb 2017 16:30:47 GMT
  5. Content-Type: text/html
  6. Content-Length: 184
  7. Connection: keep-alive
  8. Location: http://www.etiantian.org/oldboy.html

rewrite指令最後一項參數flag標記說明

last:本條規則匹配完成後,繼續向下匹配新的location URI規則。

break:本條規則匹配完成即終止。再也不匹配後面的任何規則。

redirect:返回302臨時重定向,瀏覽器地址欄會顯示跳轉後的URL地址。

permanent:返回301永久重定向,瀏覽器地址欄會顯示後的URL地址。

相關文章
相關標籤/搜索