中文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”的值

http://www.blogjava.net/agapple/archive/2008/11/07/239327.html

nginx使用小记的更多相关文章

  1. nginx小记

    上一次折腾nginx还是两年前的事情了.好多配置都忘记了. 捣腾了下阿里云,部署了一下,遇到几个小问题,温故并记录一下吧 :) 重新设置 nginx遇到问题:nginx: [error] invali ...

  2. Archlinux 上 Nginx + PHP + Mariadb + DiscuzX2.5 安装小记

    因为不好找下载集成服务器工具,而且他们自己又打包了一份 php 之类的程序,本身系统就已经有 php 还有数据库什么的了再搞一份受不了,最后选择了手动配置…… 这是一个在 Archlinux 上手动从 ...

  3. [cacti]nginx+php+cacti+mysql+php-fpm 安装小记

    网上教程很多,但是nginx不太多,下面安装时候主要参考的篇文章: http://54im.com/linux/linux-cacti-cn-install.html http://www.tecmi ...

  4. nginx file not found 错误处理小记

    安装完php php-fpm nginx 后访问php出现file not found错误,html就没问题配置文件server 段如下 server { listen 80; server_name ...

  5. Linux-centos安装node、nginx小记

    一.安装node 1.进入/usr目录,新建toos目录 cd /usr && mkdir tools && cd tools 2.wget命令下载对应版本的node包 ...

  6. 菜鸟运维笔记:小记编译安装Nginx所遇到的坑

    转载请注明出处:http://blog.csdn.net/guodongxiaren/article/details/40950249 谢谢合作 前言 无论是CentOS,或是Debian/Ubunt ...

  7. Nginx --Windows下和Linux下搭建集群小记

    nginx: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器 特点: 反向代理 负载均衡 动静分离... 反向代理 : 先来了解正向代理:需要我们用户 ...

  8. nginx配置之后接口状态200,但是无返回数据问题小记

    nginx配置可能有问题.导致nginx不能解析PHP文件,检测nginx里对于php的配置信息. location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; f ...

  9. ConCurrent in Practice小记 (3)

    ConCurrent in Practice小记 (3) 高级同步技巧 Semaphore Semaphore信号量,据说是Dijkstra大神发明的.内部维护一个许可集(Permits Set),用 ...

随机推荐

  1. QT窗口拖拽功能简单应用(处理dragEnterEvent和dropEvent事件,不同的事件有不同的信息,比如mimeData)

    void dragEnterEvent(QDragEnterEvent *event); void dropEvent(QDropEvent *event); ui->lineEdit-> ...

  2. [Windows编程] #pragma once 和#ifndef ... #define ... #endif 比较

    C++中防止头文件被多次include 的常见方式有: 1) 用#ifndef ...  #define ... #endif  宏 #ifndef __MYHEADER_H__#define __M ...

  3. HDOJ 1423 Greatest Common Increasing Subsequence(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423 思路分析:[问题定义]给定两个序列A[0, 1,..., m]和B[0, 1, ..., n], ...

  4. http://qt-project.org/wiki/Category:Developing_with_Qt::QtWebKit#ff7c0fcd6a31e735a61c001f75426961

    404: Not Found | Qt Project QtWebKit documentation http://dwz.cn/hr2013

  5. android点滴之PendingIntent的使用

    一概念 PendingIntent就是一个能够在满足一定条件下运行的Intent,它相比于Intent的优势在于自己携带有Context对象.这样他就不必依赖于某个activity才干够存在. 它和I ...

  6. ActionScript3游戏中的图像编程(连载二十四)

    总文件夹:http://blog.csdn.net/iloveas2014/article/details/38304477 2.1.1 投影样式的制作 点击左側列表的"投影"系列 ...

  7. js推断指定函数、变量是否存在的方法

    //推断是否存在指定函数 function isExitsFunction(funcName) { try { if (typeof (eval(funcName)) == "functio ...

  8. Sql server 事务的两种用法

    事务(Transaction)是并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位. 通过事务,SQL Server能将逻辑相关的一组操作绑定在一起,以便 ...

  9. JQuery的stop()属性

    $(function(){ $('#input1').hover( function(){ $('.div1').stop() .animate({left:) .animate({top:); } ...

  10. git和GItHub的区别

    git是一个版本控制工具.github是一个用git做版本控制的项目托管平台. 这有点类似于Wordpress和Wordpress.com的关系,前者是一个任何人都可以用的免费博客系统,后者是一个平台 ...