首页 > 代码库 > Linux 课程笔记 Nginx深入应用实践

Linux 课程笔记 Nginx深入应用实践

1 关于Nginx模块

Nginx使用不同的模块实现不同的功能,主要有2组重要的模块:

(1) Nginx core modules(必需的)

包括Main、Events

 

(2) Standard  HTTP modules(虽然不是必需的,但是缺省都会安装,不建议改动)

典型的包括

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

 

2 Nginx目录结构

/application/nginx

|-- 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文件中调用此配置文件

技术分享

技术分享

 

 

Linux 课程笔记 Nginx深入应用实践