NGINX总结

简介: 目录一、安装        2A)一般安装        2B)高级安装        2二、基本命令        3三、配置文件        3A)配置文件解释        3B)带监控模块的配置        4C)虚拟主机的配置        5五、事件模型介绍--I/O...
目录

一、安装        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
一、安装
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
二、基本命令
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 /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
三、配置文件
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;
        }
   }
}


五、事件模型介绍--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. 为了防止出现内核崩溃的问题, 有必要安装安全补丁。



六、关于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/


七、软件的平滑升级
     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给旧的主进程。


八、相关的处理信号
A)主进程可以处理的信号
   TERM ,INT            快速关闭
    QUIT                        从容关闭
        HUP                                重载配置,用新的配置开始新的进程,从容关闭旧的工作进程
USR1                        重新打开日志文件
USR2                         平滑升级可执行程序
WINCH                        从容关闭工作进程
B)工作进程可以处理的信号
TERM ,INT                         快速关闭
QUIT                                从容关闭
USR1                                重新打开日志文件


参考网站:
http://wiki.codemongers.com/Main
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
缓存 负载均衡 应用服务中间件
nginx简单学习总结
nginx简单学习总结
79 1
nginx简单学习总结
|
缓存 应用服务中间件 nginx
|
网络协议 应用服务中间件 Linux
|
负载均衡 Java 应用服务中间件
Nginx学习笔记总结:初次认识 Nginx
Nginx是一个高性能的HTTP,一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
Nginx学习笔记总结:初次认识 Nginx
|
存储 缓存 负载均衡
nginx从理论到实践超详细笔记总结(下)
nginx从理论到实践超详细笔记总结(下)
228 0
|
存储 负载均衡 前端开发
nginx从理论到实践超详细笔记总结
nginx从理论到实践超详细笔记总结
167 0
|
缓存 负载均衡 JavaScript
Vue+VueRouter+Nginx+CDN 项目优化实际总结
Vue+VueRouter+Nginx+CDN 项目优化实际总结
432 0
Vue+VueRouter+Nginx+CDN 项目优化实际总结
|
缓存 负载均衡 网络协议
Haproxy LVS Nginx的优缺点总结
1、haproxy优点 2、Nginx优点 3、Nginx缺点 4、LVS优点 5、LVS缺点