设为首页 收藏本站
查看: 750|回复: 0

[经验分享] Nginx的介绍和部署

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-5-11 12:56:25 | 显示全部楼层 |阅读模式
1.nginx的介绍1.1 nginx的优势
1) 作为Web服务器,Nginx处理静态文件、索引文件,自动索引的效率非常高。
2) 作为代理服务器,Nginx可以实现无缓存的反向代理加速,提高网站运行速度。
3)作为负载均衡服务器,Nginx既可以在内部直接支持Rails和PHP,也可以支持HTTP代理服务器对外进行服务,同时还支持简单的容错和利用算法进行负载均衡。
4) 在性能方面,Nginx是专门为性能优化而开发的,在实现上非常注重效率。它采用内核Poll模型,可以支持更多的并发连接,最大可以支持对50 000个并发连接数的响应,而且只占用很低的内存资源。
5) 在稳定性方面,Nginx采取了分阶段资源分配技术,使得CPU与内存的占用率非常低。Nginx官方表示,Nginx保持10000个没有活动的连接,而这些连接只占用2.5MB内存,因此,类似DOS这样的攻击对Nginx来说基本上是没有任何作用的。
6) 在高可用性方面,Nginx支持热部署,启动速度特别迅速,因此可以在不间断服务的情况下,对软件版本或者配置进行升级,即使运行数月也无需重新启动,几乎可以做到7×24小时不间断地运行。
1.2 nginx的模块和工作原理
1) Nginx由内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是Nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
2) Nginx的模块从结构上分为核心模块、基础模块和第三方模块, HTTP模块、EVENT模块和MAIL模块等属于核心模块,HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块属于基础模块,而HTTP Upstream Request Hash模块、Notice模块和HTTP Access Key模块属于第三方模块,用户根据自己的需要开发的模块都属于第三方模块。正是有了这么多模块的支撑,Nginx的功能才会如此强大。

3) Nginx的模块从功能上分为如下三类。
Handlers(处理器模块)。此类模块直接处理请求,并进行输出内容和修改headers信息等操作。Handlers处理器模块一般只能有一个。
Filters(过滤器模块)。此类模块主要对其他处理器模块输出的内容进行修改操作,最后由Nginx输出。
Proxies(代理类模块)。此类模块是Nginx的HTTPUpstream之类的模块,这些模块主要与后端一些服务比如FastCGI等进行交互,实现服务代理和负载均衡等功能。

3) 在工作方式上,Nginx分为单工作进程和多工作进程两种模式。在单工作进程模式下,除主进程外,还有一个工作进程,工作进程是单线程的;在多工作进程模式下,每个工作进程包含多个线程。Nginx默认为单工作进程模式。
4) Nginx的模块直接被编译进Nginx,因此属于静态编译方式。启动Nginx后,Nginx的模块被自动加载,不像Apache,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。在解析配置文件时,Nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。
2 nginx安装
1) Nginx的的运行需要有pcre-devel和zlib-devel的支持,所以安装之前需要先把这两个包装上
1
yuminstall pcre-devel zlib-devel -y




2) 创建Nginx用户,便于权限管理
1
useradd-M -s /sbin/nologin -u 48 nginx




3) 解包,编译,安装
1
2
3
4
5
6
cd/usr/src
wgethttp://nginx.org/download/nginx-1.6.3.tar.gz
tarzxf nginx-1.6.3.tar.gz
cdnginx-1.6.3
./configure--prefix=/usr/local/nginx-1.6.3 --user=nginx --group=nginx --with-http_stub_status_module
make&& make install




4) 作链接,然后启动Nginx   
1
2
3
4
5
6
7
8
cd/usr/local
ln-s nginx-1.6.3 nginx
/usr/local/nginx/sbin/nginx
[iyunv@LNMP local]# netstat -anput | grepnginx
tcp       0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3470/nginx
[iyunv@LNMP local]# ps -ef | grep nginx  | grep -v grep
root      3470      1  0 20:50 ?        00:00:00 nginx: master process/usr/local/nginx/sbin/nginx
nginx     3471   3470  0 20:50 ?        00:00:00 nginx: worker process




3 nginx的启动脚本编写启动脚本,便于对Nginx进行管理。
脚本内容:
====================================================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
#chkconfig:- 85 15
#description:this is a script to control nginx
  
#Sourcefunction library.
./etc/rc.d/init.d/functions
#Source networking configuration.
./etc/sysconfig/network
#Check that networking is up.
["$NETWORKING" = "no" ] && exit 0
nginx=/usr/local/nginx/sbin/nginx
pid=/usr/local/nginx/logs/nginx.pid
start(){
if  [ -e$pid ]
   then
    action "Nginx already running..."/bin/false && exit 1
  else
   $nginx
  [ $? -eq 0 ] && {
        action "Nginx start is  OK..." /bin/true
} || action "Nginx start iserror..." /bin/false
fi
}
stop(){
  if [ ! -e $pid ]
   then
   action "Nginx is not  running..." /bin/false && exit 1
  else
   kill -s QUIT $(cat $pid)
  [ $? -eq 0 ] && action "Nginxstop is  OK..." /bin/true || action"Nginx stop is error..." /bin/false
  fi
}
restart(){
    $0 stop
    $0 start
}
reload(){
  if [ ! -e $pid ]
   then
   action "Nginx is not  running..." /bin/false && exit 1
  else
   kill -s HUP $(cat $pid)
  [ $? -eq 0 ] && action "Nginxreload is  OK..." /bin/true ||action "Nginx reload is error..." /bin/false
  fi
}
usage(){
echo"Usage: $0 {start|stop|status|restart|reload"
}
status(){
if [ -e $pid ]
   then
   echo "Nginx is running..."
else
   echo "Nginx is stop..."
fi
}
case$1 in
start)
  start
;;
stop)
   stop
;;
reload)
   reload
;;
restart)
   restart
;;
status)
   status
;;
*)
usage
;;
esac




====================================================================
把上面的脚本复制到/etc/init.d/nginx脚本中,并赋予执行权限,添加开机自启动
1
2
3
chmod a+x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on





4 nginx简单管理4.1 Nginx的启动、关闭和平滑重启
1)检查文件正确性
Nginx提供的配置文件调试功能非常有用,可以快速定位配置文件存在的问题。执行如下命令可检测配置文件的正确性
1
2
/usr/local/nginx/sbin/nginx–t
或 /usr/local/nginx/sbin/nginx-t -c /usr/local/nginx/conf/nginx.conf




当出现下面的提示时,表示配置文件没有问题
nginx: the configuration file /usr/local/nginx/conf/nginx.confsyntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf testis successful
2) 显示Nginx的版本以及相关编译信息
显示Nginx版本信息
1
2
[iyunv@Nginx~]# /usr/local/nginx/sbin/nginx -v
nginxversion: nginx/1.5.0




显示Nginx的编译信息
1
2
3
4
[iyunv@Nginx~]# /usr/local/nginx/sbin/nginx -V
nginxversion: nginx/1.5.0
builtby gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)
configurearguments: --prefix=/usr/local/nginx-1.5.0 --user=nginx --group=nginx--with-http_stub_status_module




3)Nginx的启动,关闭与重启
Nginx对进程的控制能力非常强大,可以通过信号指令控制进程。常用的信号有:
QUIT,表示处理完当前请求后,关闭进程。
HUP,表示重新加载配置,也就是关闭原有的进程,并开启新的工作进程。此操作不会中断用户的访问请求,因此可以通过此信号平滑地重启Nginx。
USR1,用于Nginx的日志切换,也就是重新打开一个日志文件,例如每天要生成一个新的日志文件时,可以使用这个信号来控制。
USR2,用于平滑升级可执行程序。
WINCH ,从容关闭工作进程。

4)Nginx的启动
直接执行/usr/local/nginx/sbin/nginx即可启动
5)Nginx的关闭
killall nginx
或者:
kill -9 pid号
其中,XXX就是信号名,pid是Nginx的进程号,、
6)Nginx的平滑重启
kill-HUP `cat /usr/local/nginx/logs/nginx.pid`
另外可以做个nginx启动脚本,方便管理,上面已经说明。现在不再赘述
4.2 Nginx常见的编译选项 使用./configure --help可获取
--prefix=PATH                     定义安装路径
--sbin-path=PATH                  设置可执行文件的路径
--conf-path=PATH                  设置配置文件路径
--error-log-path=PATH             设置错误日志文件路径
--pid-path=PATH                   设置pid文件路径
--lock-path=PATH                  设置 nginx.lock 文件路径
--user=USER                       设置Nginx工作进程的用户
--group=GROUP                     设置Nginx工作进程的组
--with-rtsig_module               enable rtsig module
--with-select_module              enable select module
--without-select_module           disable select module
--with-poll_module                enable poll module
--without-poll_module             disable poll module

--with-file-aio                   enable file AIO support
--with-ipv6                       enable IPv6 support

--with-http_ssl_module            enable ngx_http_ssl_module
--with-http_spdy_module           enable ngx_http_spdy_module
--with-http_realip_module         enable ngx_http_realip_module
--with-http_addition_module       enable ngx_http_addition_module
--with-http_xslt_module           enable ngx_http_xslt_module
--with-http_image_filter_module   enable ngx_http_image_filter_module
--with-http_geoip_module          enable ngx_http_geoip_module
--with-http_sub_module            enable ngx_http_sub_module
--with-http_dav_module            enable ngx_http_dav_module
--with-http_flv_module            enable ngx_http_flv_module
--with-http_mp4_module            enable ngx_http_mp4_module
--with-http_gunzip_module         enable ngx_http_gunzip_module
--with-http_gzip_static_module    enable ngx_http_gzip_static_module
--with-http_auth_request_module   enable ngx_http_auth_request_module
--with-http_random_index_module   enable ngx_http_random_index_module
--with-http_secure_link_module     enable ngx_http_secure_link_module
--with-http_degradation_module    enable ngx_http_degradation_module
--with-http_stub_status_module    enable ngx_http_stub_status_module

--without-http_charset_module     disable ngx_http_charset_module
  --without-http_gzip_module         disable ngx_http_gzip_module
--without-http_ssi_module         disable ngx_http_ssi_module
--without-http_userid_module      disable ngx_http_userid_module
--without-http_access_module      disable ngx_http_access_module
--without-http_auth_basic_module  disable ngx_http_auth_basic_module
--without-http_autoindex_module   disable ngx_http_autoindex_module
--without-http_geo_module         disable ngx_http_geo_module
--without-http_map_module         disable ngx_http_map_module
--without-http_split_clients_module disablengx_http_split_clients_module
--without-http_referer_module     disable ngx_http_referer_module
--without-http_rewrite_module     disable ngx_http_rewrite_module
--without-http_proxy_module       disable ngx_http_proxy_module
--without-http_fastcgi_module     disable ngx_http_fastcgi_module
--without-http_uwsgi_module       disable ngx_http_uwsgi_module
--without-http_scgi_module        disable ngx_http_scgi_module
--without-http_memcached_module   disable ngx_http_memcached_module
--without-http_limit_conn_module  disable ngx_http_limit_conn_module
--without-http_limit_req_module   disable ngx_http_limit_req_module
--without-http_empty_gif_module   disable ngx_http_empty_gif_module
--without-http_browser_module     disable ngx_http_browser_module
--without-http_upstream_ip_hash_module
                                     disablengx_http_upstream_ip_hash_module
--without-http_upstream_least_conn_module
                                     disablengx_http_upstream_least_conn_module
--without-http_upstream_keepalive_module
                                     disablengx_http_upstream_keepalive_module
--with-http_perl_module           enable ngx_http_perl_module
--with-perl_modules_path=PATH     set Perl modules path
--with-perl=PATH                   set perl binary pathname
--http-log-path=PATH              set http access log pathname
--http-client-body-temp-path=PATH set path to store
                                     httpclient request body temporary files
--http-proxy-temp-path=PATH       set path to store
                                     http proxytemporary files
--http-fastcgi-temp-path=PATH     set path to store
                                     httpfastcgi temporary files
--http-uwsgi-temp-path=PATH       set path to store
                                     http uwsgi temporary files
--http-scgi-temp-path=PATH        set path to store
                                     http scgi temporary files
--without-http                    disable HTTP server
--without-http-cache               disable HTTP cache
--with-mail                       enable POP3/IMAP4/SMTP proxy module
--with-mail_ssl_module            enable ngx_mail_ssl_module
--without-mail_pop3_module        disable ngx_mail_pop3_module
--without-mail_imap_module        disable ngx_mail_imap_module
--without-mail_smtp_module        disable ngx_mail_smtp_module

--with-google_perftools_module    enable ngx_google_perftools_module
--with-cpp_test_module            enable ngx_cpp_test_module

--add-module=PATH                 enable an external module

--with-cc=PATH                    set C compiler pathname
--with-cpp=PATH                   set C preprocessor pathname
--with-cc-opt=OPTIONS             set additional C compiler options
--with-ld-opt=OPTIONS             set additional linker options
--with-cpu-opt=CPU                build for the specified CPU, valid values:
                                     pentium,pentiumpro, pentium3, pentium4,
                                     athlon,opteron, sparc32, sparc64, ppc64

--without-pcre                    disable PCRE library usage
  --with-pcre                        force PCRE libraryusage
--with-pcre=DIR                   set path to PCRE library sources
--with-pcre-opt=OPTIONS           set additional build options for PCRE
--with-pcre-jit                   build PCRE with JIT compilation support

--with-md5=DIR                    set path to md5 library sources
--with-md5-opt=OPTIONS            set additional build options for md5
--with-md5-asm                    use md5 assembler sources

--with-sha1=DIR                    set path to sha1 library sources
--with-sha1-opt=OPTIONS           set additional build options for sha1
--with-sha1-asm                   use sha1 assembler sources

--with-zlib=DIR                   set path to zlib library sources
--with-zlib-opt=OPTIONS           set additional build options for zlib
--with-zlib-asm=CPU               use zlib assembler sources optimized
                                     for thespecified CPU, valid values:
                                     pentium,pentiumpro

--with-libatomic                  force libatomic_ops library usage
--with-libatomic=DIR              set path to libatomic_ops library sources

--with-openssl=DIR                set path to OpenSSL library sources
--with-openssl-opt=OPTIONS        set additional build options for OpenSSL

--with-debug                      enable debug logging


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-65858-1-1.html 上篇帖子: Nginx配置文件(nginx.conf)配置详解 下篇帖子: nginx+lua环境搭建笔记
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表