Linux 課程筆記 Nginx深刻應用實踐

1 關於Nginx模塊html

Nginx使用不一樣的模塊實現不一樣的功能,主要有2組重要的模塊:nginx

(1) Nginx core modules(必需的)apache

包括Main、Events瀏覽器

 

(2) Standard  HTTP modules(雖然不是必需的,可是缺省都會安裝,不建議改動)bash

典型的包括服務器

Core、Access、FastCGI、Gzip、Log、Proxy、Rewrite、Upstream併發

 

2 Nginx目錄結構app

/application/nginxtcp

|-- client_body_temp工具

|-- conf

|   |-- fastcgi.conf   #fastcgi配置文件

|   |-- fastcgi.conf.default   #default文件均屬於備份文件

|   |-- fastcgi_params

|   |-- fastcgi_params.default

|   |-- koi-utf

|   |-- koi-win

|   |-- mime.types

|   |-- mime.types.default

|   |-- nginx.conf   #Nginx主配置文件

|   |-- nginx.conf.default

|   |-- nginx.conf.qinbf-20131101

|   |-- scgi_params

|   |-- scgi_params.default

|   |-- uwsgi_params

|   |-- uwsgi_params.default

|   `-- win-utf

|-- fastcgi_temp

|-- html

|   |-- 50x.html  #錯誤優雅顯示文件

|   `-- index.html

|-- logs

|   |-- access.log  #訪問日誌

|   |-- error.log   #錯誤日誌

|   `-- nginx.pid 

|-- proxy_temp

|-- sbin

|   `-- nginx

|-- scgi_temp

`-- uwsgi_temp

 

9 directories, 22 files

 

3 Nginx.conf配置文件

worker_processes  1;  #ps -ef |grep nginx能夠查看到nginx的子線程數

 

events {

    worker_connections  1024;  #能夠理解爲最大併發數

}

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

 

    server {  #一個server至關於apache的一個vhost,能夠複製多個server模塊配置多個主機

        listen       80;

        server_name  localhost;

 

        location / {   #至關於htdocs

            root   html;

            index  index.html index.htm;

        }

 

        error_page   500 502 503 504  /50x.html;  #優雅顯示頁面

        location = /50x.html {

            root   html;

 

        }

 

    }

 

}

 

4 基於域名的虛擬主機配置

http {

 10     include       mime.types;

 11     default_type  application/octet-stream;

 12     sendfile        on;

 13     keepalive_timeout  65;

 14

 15     server {

 16         listen       80;

 17         server_name  www.etiantian.org;

 18

 19         location / {

 20             root   html;

 21             index  index.html index.htm;

 22         }

 23

 24         error_page   500 502 503 504  /50x.html;

 25         location = /50x.html {

 26             root   html;

 27

 28         }

 29

 30     }

 31

 32 }

 

而後檢查語法,優雅重啓

[root@test2 conf]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.2.9/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.2.9/conf/nginx.conf test is successful

[root@test2 conf]# /application/nginx/sbin/nginx -s reload

[root@test2 conf]# netstat -tupnl |grep 80

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      21475/nginx  

 

配置虛擬主機流程:

1)  複製server標籤段,到結尾,注意放到http的結束大括號前

2)  更改server_name及對應網頁的根目錄

3)  建立對應網頁的根目錄,並創建測試文件

4)  檢查語法,重啓服務

5)  在host文件作解析

6)  瀏覽器訪問

 

5 禁止ip訪問

爲防止域名惡意解析到本身的服務器上,必需要配置禁止ip訪問

server  {

     listen  80  default;

     return 500;

}

#這段配置,是將訪問沒有配置爲本服務器虛擬主機的域名,默認返回500錯誤

 

#也能夠利用rewrite規則,把惡意解析到本服務器的域名訪問流量,導入到本身的站點

server  {

       listen  80  default;

       rewrite  ^(.*)  http://www.etiantian.com  permanent;

}

#域名解析到本地服務器,可是並未爲該域名配置本地服務器的虛擬主機,將跳轉到rewrite定義的站點上

 

#server_name  _;  這個的含義表明輸入ip地址直接訪問的結果

#listen  80  default_server;  default_server這個參數用於nginx 0.8版本以後

 

6 nginx日誌配置及切割

目前尚未比較好的Nginx日誌切割工具

日誌的格式定義:

log_format  commonlog  '$remote_addr - $remote_user [$time_local] "$request" '

                           '$status $body_bytes_sent "$http_referer" '

                           '"$http_user_agent" "$http_x_forwarded_for"';

 

    server {

        listen       80;

        server_name  www.etiantian.com;

 

        location / {

            root   /data0/www/www;

            index  index.html index.htm;

        }

 

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

 

        }

 

        access_log /app/logs/www_access.log commonlog;

        #日誌格式的調用

     }

 

192.168.1.1 - - [22/Nov/2013:00:27:32 +0800] "GET /favicon.ico HTTP/1.1" 404 570 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36" "-"

 

 

日誌切割腳本

#!/bin/bash

date=`date +%Y%m%d`

Nginx_Dir="/application/nginx"

Nginx_Logs="/app/logs"

Log_Name="www_access"

 

cd /tmp

[ -d $Nginx_Logs ] && cd $Nginx_Logs || exit 1

[ -f $Log_Name.log ] && /bin/mv $Log_Name.log  ${Log_Name}.${date}.log || exit 1

if [ $? -eq 0 -a -f $Nginx_Dir/logs/nginx.pid ]

   then

      kill -USR1 `cat  $Nginx_Dir/logs/nginx.pid` 

#把nginx的日誌重命名至關於刪除文件,須要重啓nginx服務

fi

 

 

而後天天晚上12點切割

crontab  -e

00  00  * * * /bin/sh /root/scripts/cut_nginx_log.sh >/dev/null 2>&1

 

統計IP並排序

awk '{print $1}' www_access.log  | sort | uniq -c | sort -rn -k 1 | head

 

 

7  Nginx配置文件優化

worker_processes  1;

 

 

events {

    worker_connections  1024;

}

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    log_format  commonlog  '$remote_addr - $remote_user [$time_local] "$request" '

                           '$status $body_bytes_sent "$http_referer" '

                           '"$http_user_agent" "$http_x_forwarded_for"';

   

    include extra/www.conf;

    include extra/bbs.conf;

    include extra/blog.conf;

    include extra/default.conf;

 

}

模仿apache配置文件,把虛擬主機的配置寫在extra目錄的配置文件內,而後用include的方式調用。

 

8  Nginx別名及鏈接狀態信息配置

#別名其實就是以相應的別名配置虛擬主機,而後利用rewrite規則,跳轉到主域名上。

專門寫一個配置文件內容以下:

server {

        listen       80;

        server_name  etiantian.com;

       

        rewrite ^(.*) http://www.etiantian.com permanent;

 

 }

 

而後在nginx文件將調用此文件:include  extra/www_alias.conf

 

 

便是配置一個虛擬主機文件,內容以下:

server {

        listen       80;

        server_name  status.etiantian.com;

 

        location / {

        stub_status on;

        access_log off;

        }

}

而後在nginx.conf文件中調用此配置文件

相關文章
相關標籤/搜索