免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 10477 | 回复: 11
打印 上一主题 下一主题

[Web] NGINX 介绍 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-31 12:39 |只看该作者 |倒序浏览
NGINX总结

目录

一、安装        2
A)一般安装        2
B)高级安装        2
二、基本命令        3
三、配置文件        3
A)配置文件解释        3
B)带监控模块的配置        4
C)虚拟主机的配置        5
五、事件模型介绍--I/O复用方法        7
A)标准事件模型        7
B)高效事件模型        7
六、关于LEMP的结构        8
A)Linux+Nginx+Mysql+Php        8
B)其他结构        8
七、软件的平滑升级        10
A)简单修改配置文件        10
B)平滑升级二进制代码        11
八、相关的处理信号        12
A)主进程可以处理的信号        12
B)工作进程可以处理的信号        12

Nginx (engine X)是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。Nginx是俄罗斯人开发的。Nginx的英文站点:http://nginx.net。Nginx的wiki站点:http://wiki.codemongers.com/Main

论坛徽章:
0
2 [报告]
发表于 2008-01-31 12:39 |只看该作者
一、安装
A)一般安装
./configure –prefix=/usr/local/nginx_2
make
make install
B)高级安装
如果添加对网站状态监控的功能,所需要做的工作如下
1)        安装pcre包
tar zxvf pcre-7.2.tar.gz
cd pcre-7.2
./configure
make
make install
2)        安装nginx
./configure –prefix=/usr/local/nginx_2 --with-http_stub_status_module
make
make install
状态页面

更多安装选项可参考网站http://wiki.codemongers.com/NginxInstallOptions

论坛徽章:
0
3 [报告]
发表于 2008-01-31 12:40 |只看该作者
二、基本命令
nginx –v 显示nginx的版本
[root@localhost sbin]# ./nginx -v
nginx version: nginx/0.5.34
nginx –V 显示nginx的版本,编译器和配置参数
[root@localhost sbin]# ./nginx -V
nginx version: nginx/0.5.34
built by gcc 3.4.6 20060404 (Red Hat 3.4.6-3)
configure arguments: --prefix=/usr/local/nginx_2 --with-http_stub_status_module
nginx –t 不运行,仅仅测试配置文件
nginx –c </path/to/config>为nginx指定配置文件
[root@localhost sbin]# ./nginx -t -c /usr/local/nginx_2/conf/nginx_status.conf
2008/01/26 18:34:40 [info] 4242#0: the configuration file /usr/local/nginx_2/conf/nginx_status.conf syntax is ok
2008/01/26 18:34:40 [info] 4242#0: the configuration file /usr/local/nginx_2/conf/nginx_status.conf was tested successfully

启动nginx
/usr/local/nginx_2/sbin/nginx –c /usr/local/nginx_2/conf/nginx_status.conf
注:如果不指定配置文件,系统会按照Nginx安装目录下conf/nginx.conf的配置启动。
退出nginx
   kill –QUIT nginx_pid

论坛徽章:
0
4 [报告]
发表于 2008-01-31 12:41 |只看该作者
三、配置文件
Nginx涉及到的配置文件
koi-utf  koi-win  mime.types  mime.types.default  nginx.conf  nginx.conf.default  win-utf
其中主配置文件是nginx.conf,主要的配置工作,都在这里进行
mime.types文件规定文件的类型
A)配置文件解释
nginx.conf文件
user          www        users;  规定用户、用户组
worker_processes 3; 规定工作进程数 默认是1
error_log logs/error.log;记录日志文件
pid        logs/nginx.pid; 主进程的进程号
events {
    use epoll;     规定使用的I/O复用模式
    worker_connections 1024; 规定进程的连接数 默认是1024
}
        最大客户端数由worker_processes、worker_connections决定
        Max_client=worker_processes*worker_connections
                在反向代理情况下
                Max_client=worker_processes*worker_connections/4

http {
  include       conf/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] $request '
                      '"$status" $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
                       #规定了日志的文件格式
   access_log  logs/access.log  main;
   keepalive_timeout  65;
    server {
        listen        80;
        server_name localhost;
        location / {
            root   html;
            index index.html index.htm;
        }
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                              root   html;
                                                        }
    }
}
B)带监控模块的配置
################状态监控######################
user          www        users;
worker_processes 3;
error_log logs/error.log;
pid        logs/nginx.pid;
events {
    use epoll;
    worker_connections 1024;
}
http {
  include       conf/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] $request '
                      '"$status" $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    keepalive_timeout  65;
    server {
        listen        80;
        server_name localhost;
        location / {
            root   html;
            index index.html index.htm;
        }
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
                                                        }
    }
#######################状态监控部分############################
server{
  listen 80;
  server_name status.ceshi.com;
  location / {
               stub_status on;
                   access_log off;
             }
       }
}
C)虚拟主机的配置
###############虚拟主机配置文档#########################
user          www        users;
worker_processes  3;
error_log  logs/error.log;
pid        logs/nginx.pid;
events {
   use epoll;
    worker_connections  1024;
}
http {
  include       conf/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] $request '
                      '"$status" $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    keepalive_timeout  65;
    server {
        listen       172.20.20.244;
        listen       172.20.20.244:80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
######################虚拟主机部分####################
  server {
        listen      172.20.20.244;
        listen      172.20.20.244:80;
        listen      8000;
        server_name  www.love.com;
        location / {
            root   www/love;
            index  index.html index.htm;
        }
    }
server {
       listen      172.20.20.244;
        listen      172.20.20.244:80;
        listen      9000;
        server_name  www.duzhenhua.com;
        location / {
            root   www/duzhenhua;
            index  index.html index.htm;
        }
}
server {
       listen      172.20.20.244;
        listen      172.20.20.244:80;
        listen      9900;
        server_name  www.du.com;
        location / {
            root   www/du;
            index  index.html index.htm;
        }
   }
}

论坛徽章:
0
5 [报告]
发表于 2008-01-31 12:41 |只看该作者
五、事件模型介绍--I/O复用方法
     与apache相类,nginx针对不同的操作系统,有不同的事件模型
      A)标准事件模型
       Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll
      B)高效事件模型   
     Kqueue:使用于 FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X. 使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
Epoll: 使用于Linux内核2.6版本及以后的系统。
/dev/poll:使用于 Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
        Eventport:使用于 Solaris 10. 为了防止出现内核崩溃的问题, 有必要安装安全补丁。

论坛徽章:
0
6 [报告]
发表于 2008-01-31 12:44 |只看该作者
六、关于LEMP的结构
     A)Linux+Nginx+Mysql+Php
    对于LEMP结构,网络上有人搭建了,整个搭建过程可以参考:
http://blog.s135.com/read.php/314.htm
    如果考虑将此结构应用到实际项目中,还需要具体测试。
B)其他结构
     我们大部分项目都是采用Linux+Apache+Mysql+Php,可以考虑在前端添加一个Nginx服务器,由它来处理大量静态页面,其它页面由原来系统处理。

结构图


流程图
有人实现过此结构,参考网站:http://blog.kovyrin.net/2006/05/18/nginx-as-reverse-proxy/

论坛徽章:
0
7 [报告]
发表于 2008-01-31 12:45 |只看该作者
七、软件的平滑升级
     A)简单修改配置文件
     当系统管理员需要修改配置文件时,不需要停机可以参考以下过程。
     kill  -HUP  Nginx_root_PID
   例如:
首先按照需要修改Nginx的配置文件,然后使用-t参数测试配置文件的正确性,一切验证无误,最后执行新的配置文件。
[root@localhost sbin]# ps -ef|grep nginx
root      4258     1  0 16:12 ?        00:00:00 nginx: master process ./nginx
www       4259  4258  0 16:12 ?        00:00:00 nginx: worker process
www       4260  4258  0 16:12 ?        00:00:00 nginx: worker process
www       4261  4258  0 16:12 ?        00:00:00 nginx: worker process
root      4264  4220  0 16:12 pts/0    00:00:00 grep nginx
[root@localhost sbin]# kill -HUP 4258
[root@localhost sbin]# ps -ef|grep nginx
root      4258     1  0 16:12 ?        00:00:00 nginx: master process ./nginx
www       4265  4258  0 16:14 ?        00:00:00 nginx: worker process
www       4266  4258  0 16:14 ?        00:00:00 nginx: worker process
www       4267  4258  0 16:14 ?        00:00:00 nginx: worker process
root      4269  4220  0 16:14 pts/0    00:00:00 grep nginx
     B)平滑升级二进制代码
过程分析
       首先,使用新的可执行程序替换旧的,然后,发送USR2(kill –USR2 ROOT_PID)信号给主进程,主进程将重新命名它的.pid文件为nginx.pid..oldbin,然后执行新的可执行程序,依次启动新的主进程和工作进程。
          在这时,两个nginx实例会同时运行,一起处理输入的请求。需要逐步停止旧的实例,此时需要发送WINCH信号给就的主进程,然后它的工作进程开始关闭。
                  一段时间后,旧的工作进程处理了所有已连接的请求后退出,就仅由新的工作进程来处理输入的请求。
          这时,因为旧的服务器还尚未关闭它监听的套接字,所以通过下面几步仍然可以恢复旧的服务器。
a)        发送HUP信号给旧的主进程,它将在不重载配置文件的情况下启动它的工作进程。
b)        发送QUIT信号给新的主进程,要求其从容关闭其工作进程
c)        发送TERM信号给新的主进程,迫使其退出
d)        如果新的工作进程不退,可以发送kill命令
e)        新的主进程退出后,会恢复nginx.pid.oldbin为nginx.pid。一切照旧。
           如果升级成功,发送QUIT给旧的主进程。

论坛徽章:
0
8 [报告]
发表于 2008-01-31 12:46 |只看该作者
八、相关的处理信号
A)主进程可以处理的信号
   TERM ,INT            快速关闭
    QUIT                        从容关闭
        HUP                                重载配置,用新的配置开始新的进程,从容关闭旧的工作进程
USR1                        重新打开日志文件
USR2                         平滑升级可执行程序
WINCH                        从容关闭工作进程
B)工作进程可以处理的信号
TERM ,INT                         快速关闭
QUIT                                从容关闭
USR1                                重新打开日志文件


参考网站:
http://wiki.codemongers.com/Main

论坛徽章:
0
9 [报告]
发表于 2008-01-31 12:47 |只看该作者
自己整理的资料,感兴趣的朋友可以参考一下^_^

111.jpg (12.34 KB, 下载次数: 71)

111.jpg

论坛徽章:
0
10 [报告]
发表于 2008-01-31 15:29 |只看该作者
不错, 不错, 楼主辛苦了!
头像也比较有个性, 顺便问下那是不是 nginx 的作者?

[ 本帖最后由 newzy 于 2008-1-31 15:32 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP