中文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. 【Untiy3D 游戏开发之一】Unity3D For Window/Mac最新4.2.0版本破解教程

    转载请标明:转载自[小枫栏目],博文链接:http://blog.csdn.net/rexuefengye/article/details/11646885 一.Unity3D For Mac 1.首 ...

  2. 如何使用 Barracuda 防火墙设置/保护 Azure 应用程序

     如果某企业在 Windows Azure 上托管某个应用程序,该应用程序会在某个特定时间暴露到 Internet,以用于商业用途.公共 Internet 带来 客户的同时也带来了攻击者. Tim ...

  3. 基于Visual C++2013拆解世界五百强面试题--题4-double转换成字符串

    请用C语言实现将double类型数据转换成字符串,再转换成double类型的数据.int类型的数据 想要完成题目中的功能,首先我们的先对系统存储double的格式有所了解. 浮点数编码转换使用的是IE ...

  4. JAVA GUI学习 - 总结

    一:项目 二:重要组件补充 三:组件高级操作

  5. hdoj 3478 Catch(二分图判定+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 思路分析:该问题需要求是否存在某一个时刻,thief可能存在图中没一个点:将该问题转换为图论问题 ...

  6. jQuery ajax在GBK编码下表单提交终极解决方案(非二次编码方法)(转)

    版权声明]:版权归作者所有,转载时请以超链接形式标明文章原始出处和作者信息及本声明:http://www.open-lib.com/Forum/Read_69_1.action 前言: 当jquery ...

  7. Objective-c @property和@Synthesize

    在Objective-c中,使用@property来标识属性(一般是实例变量).在实现文件中使用@synthesize标识所声明的变量,让系统自动生成设置方法和获取方法. 也就是说@property和 ...

  8. FreeCodeCamp:Title Case a Sentence

    要求: 确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. 结果: titleCase("I'm a little tea pot") 应该 ...

  9. 模仿jquery的一些实现 第二版

    具体如下: //w作为window的形参,就表示window (function(w) { // 定义一个全局的window.wyl变量,就类似于jquery里的$,Jquery对象 w.wyl; / ...

  10. Android Bug 记录

    1.Unable to resolve target 'android-5' 无法解析目标 ' 安卓系统-5'      Unable to resolve target 'Google Inc.:G ...