`
agapple
  • 浏览: 1583028 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

nginx使用小记

阅读更多

中文wiki社区:http://wiki.codemongers.com/NginxChs

一 . nginx安装

1. 下载nginx : http://sysoev.ru/nginx/download.html(官方下载页面)
wget http://sysoev.ru/nginx/nginx-0.7.19.tar.gz

2. 依赖模块下载
gzip 模块需要 zlib 库  (http://www.zlib.net/)
rewrite 模块需要 pcre 库  (http://www.pcre.org/)
ssl 功能需要 openssl 库 (http://www.openssl.org/)

./configure --prefix=/home/ljh/program/nginx --with-http_stub_status_module --with-pcre=./external/pcre-7.8 --with-zlib=./external/zlib-1.2.3 --with-openssl=./external/openssl-0.9.8i

make & make install


通过信号对 Nginx 进行控制

Nginx 支持下表中的信号:

信号名  作用描述 
TERM, INT  快速关闭程序,中止当前正在处理的请求 
QUIT  处理完当前请求后,关闭程序 
HUP  重新加载配置,并开启新的工作进程,关闭就的进程,此操作不会中断请求 
USR1  重新打开日志文件,用于切换日志,例如每天生成一个新的日志文件 
USR2  平滑升级可执行程序 
WINCH  从容关闭工作进程 

有两种方式来通过这些信号去控制 Nginx,第一是通过 logs 目录下的 nginx.pid 查看当前运行的 Nginx 的进程 ID,
通过 kill – XXX <pid> 来控制 Nginx,其中 XXX 就是上表中列出的信号名。如果您的系统中只有一个 Nginx 进程,那您也可以通过 killall 命令来完成,例如运行 killall – s HUP nginx 来让 Nginx 重新加载配置。

killall -s HUP nginx

二  nginx 配置

1. 监控配置
location /nginx_status {
    # copied from http://blog.kovyrin.net/2006/04/29/monitoring-nginx-with-rrdtool/
    stub_status on;
    access_log   off;
    allow SOME.IP.ADD.RESS;
    deny all;
}

页面结果解释:
Active connections: 291
server accepts handled requests
 16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
active connections -- 对后端发起的活动连接数

server accepts handled requests -- nginx 总共处理了 16630948 个连接, 成功创建 16630948 次握手 (证明中间没有失败的), 总共处理了 31070465 个请求 (平均每次握手处理了 1.8个数据请求)
reading -- nginx 读取到客户端的Header信息数
writing -- nginx 返回给客户端的Header信息数
waiting -- 开启 keep-alive 的情况下,这个值等于 active - (reading + writing),意思就是Nginx说已经处理完正在等候下一次请求指令的驻留连接

2. 页面缓存配置  http://wiki.codemongers.com/NginxChsMemcachedModule
server {
    location / {
        set  $memcached_key  $uri;
        memcached_pass   name:11211;
        default_type     text/html;
        error_page       404 = /fallback;
    }

    location = /fallback {
        proxy_pass       backend;
    }

3. 页面配置IP访问列表   http://wiki.codemongers.com/NginxHttpAccessModule
location / {
    deny    192.168.1.1;
    allow   192.168.1.0/24;
    allow   10.1.1.0/16;
    deny    all;
}

4. 配置页面访问控制

          location  /  {                         
              auth_basic            "Restricted";
}             auth_basic_user_file  conf/htpasswd;
htpasswd格式为 用户名:密码。你可以使用来自 Apache 的 htpasswd 工具来创建密码文件。

 

5. 限制每个IP的并发数  http://wiki.codemongers.com/NginxChsHttpLimit_zoneModule
limit_zone   one  $binary_remote_addr  10m;
server {
        location /download/ {
           limit_conn   one  1;
      }
}
定义一个叫“one”的记录区,总容量为 10M,以变量 $binary_remote_addr 作为会话的判断基准(即一个地址一个会话)。 限制 /download/ 目录下,一个会话只能进行一个连接。
简单点,就是限制 /download/ 目录下,一个IP只能发起一个连接,多过一个,一律503

6. 代理模块   http://wiki.codemongers.com/NginxChsHttpProxyModule

location / {
    proxy_pass        http://localhost:8000/hello;
    proxy_redirect    http:/localhost:8000/hello/   /;

    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_read_timeout 60
    proxy_connect_timeout 60
}

 

配置项介绍:

daemon on | off   缺省值: on 
可以在开发时开启,但必须在真实环境中设置为off

debug_points [stop | abort]  缺省值: none
可以在debugger上停止nginx应用

error_log file [ debug | info | notice | warn | error | crit ]      缺省值: ${prefix}/logs/error.log


include vhosts/*.conf;    缺省值: none
如果配置文件很长,你可以在任意地方使用include指令实现配置文件的包含。*.conf匹配所有以.conf结尾的文件

lock_file  /var/log/lock_file;
nginx采用以异步互斥进行访问控制

master_process on | off     缺省值: on
和dameon on 都是在开发时使用

pid /var/log/nginx.pid;
进程id存储文件。可以使用 kill -HUP cat /var/log/nginx.pid\ 对Nginx进行配置文件重新加载。

user user [group]
指定Nginx Worker进程运行用户,默认是nobody帐号。

worker_processes number 缺省值: 1
配置工作进程。max_clients = worker_processes * worker_connections


worker_priority [-]number
设置工作进程的优先级

worker_cpu_affinity 0001 0010 0100 1000;
绑定worker进行到四个CPU

worker_rlimit_core size
指定每个进程的文件限制


access_log path [format [buffer=size]] | off    默认值: access_log log/access.log combined
指令 access_log 指派路径、格式和缓存大小。参数 "off" 将清除当前级别的所有 access_log 指令。如果未指定格式,则使用预置的 "combined" 格式。缓存不能大于能写入磁盘的文件的最大大小。在 FreeBSD 3.0-6.0 ,缓存大小无此限制。


log_format name format [format ...]  默认值: log_format combined "..."
log_format  combined  '$remote_addr - $remote_user [$time_local] '
            '"$request" $status $apache_bytes_sent '
                      '"$http_referer" "$http_user_agent"';

 

expires [time|epoch|max|off] 默认值: expires off
使用本指令可以控制HTTP应答中的“Expires”和“Cache-Control”的头标,(起到控制页面缓存的作用)。
可以在time值中使用正数或负数。“Expires”头标的值将通过当前系统时间加上您设定的 time 值来获得。
epoch 指定“Expires”的值为 1 January, 1970, 00:00:01 GMT。
max 指定“Expires”的值为 31 December 2037 23:59:59 GMT,“Cache-Control”的值为10年。
“Cache-Control”头标的值由您指定的时间来决定:
负数:Cache-Control: no-cache
正数或零:Cache-Control: max-age = #, # 为您指定时间的秒数。
"off" 表示不修改“Expires”和“Cache-Control”的值

 
待补充。。。。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics