`
agapple
  • 浏览: 1584305 次
  • 性别: 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”的值

 
待补充。。。。

分享到:
评论

相关推荐

    jks证书格式转换nginx使用格式

    由于生成的证书是jks格式,nginx不能直接用,需要要转成PEM格式,这要用到jks2pfx工具进行转换。 jks2pfx的命令格式:JKS2PFX.bat keystore password alias exportname keystore:KeyStore文件绝对路径 password:...

    Nginx使用方法

    nginx -t -c F:/nginx-1.12.2/conf/nginx.conf 测试nginx配置文件是否正确 start nginx nginx -s stop nginx -s quit nginx -s reload nginx -t 修改后的hosts文件放入c:\windows\system32\drivers\etc替换以前的...

    nginx安装与使用.zip

    nginx安装与使用 http://blog.163.com/njut_wangjian/blog/static/1657964252013327103716818/ Nginx开发从入门到精通 http://tengine.taobao.org/book/index.html nginx官网上下载相应的安装包,--- 直接解压就...

    nginx 使用及配置文件

    关于nginx+tomcat 的配置文件,及详细介绍

    nginx使用文档

    包含lvs+nginx负载均衡 nginx安装手册 nginx使用文档 keepalived高可用高可用方法 SwitchHosts软件包 还有相关安装包

    Nginx使用手册.docx

    Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。

    nginx使用指南.pdf

    nginx使用指南.pdf

    nginx使用手册.chm

    nginx使用手册.chm 配置服务器nginx的手册,参考工具,欢迎下载

    nginx离线安装依赖项-linux系统/麒麟v10系统

    PCRE(Perl Compatible Regular Expressions):Nginx使用PCRE来支持正则表达式,可以使用它更灵活地匹配和处理请求。 zlib:它是一个广泛使用的压缩库,Nginx使用zlib来处理gzip压缩。 OpenSSL:它是一个开放源...

    Linux自动化脚本安装Nginx使用说明

    Linux自动化脚本安装Nginx使用说明,里面带了所需要的文件与文档说明

    nginx安装使用教程

    nginx安装使用教程nginx安装使用教程

    使用nginx部署前端项目(超详细教程).pdf

    使用nginx部署前端项目是一篇非常详细的教程,旨在帮助初学者使用Nginx来部署前端项目。本文首先介绍了Nginx的基本概念和作用,解释了为什么Nginx是一个强大的Web服务器和反向代理。然后,文章详细讲解了如何在Linux...

    Nginx之proxy_redirect使用详解

    今天在做nginx反向代理apache的时候出了一点点问题,原来后端apache用的端口是8080通过反向代理后,使用wireshark抓包发现location头域数值为http://192.168.1.154:8080/wuman/  如果把这个返回给客户端肯定是不...

    nginx 离线安装包nginx 离线安装包

    nginx 离线安装包nginx 离线安装包

    Nginx1.22.0版本Linux已编译可直接使用

    Linux环境Nginx1.22.0版本,解压即用。 解压后使用./nginx -V可查看版本和编译信息。

    nginx镜像资源nginx镜像资源nginx镜像资源nginx镜像资源nginx镜像资源nginx镜像资源

    nginx镜像资源nginx镜像资源nginx镜像资源nginx镜像资源nginx镜像资源nginx镜像资源

    nginx使用方法

    nginx使用方法 ,基本使用和配置 配置文件的修改和参数的添加

    开机自起nginx

    [root@web1 ~]# cat /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target  [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/...

    Nginx 1.22.0 Linux 版本,解压安装。

    Nginx 的源代码使用 2-clause BSD-like license。 Nginx 是一个很强大的高性能Web和反向代理服务,它具有很多非常优越的特性: 在连接高并发的情况下,Nginx是Apache服务不错的替代品:Nginx在美国是做虚拟主机生意...

Global site tag (gtag.js) - Google Analytics