zoukankan      html  css  js  c++  java
  • nginx 1.安装

    nginx 1.安装

    nginx的众多优点这里就不多说了,直接开始吧。


    基本依赖

    yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel  openssl openssl-devel
    

    内核优化,

    一份典型配置(生产环境,尽可能对内核进行调优,可以减少硬件的浪费和对系统的掌控)

    fs.file-max = 999999
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.tcp_keepalive_time = 600
    net.ipv4.tcp_fin_timeout = 30
    net.ipv4.tcp_max_tw_buckets = 5000
    net.ipv4.ip_local_port_range = 1024    61000
    net.ipv4.tcp_rmem = 4096 32768 262142
    net.ipv4.tcp_wmem = 4096 32768 262142
    net.core.netdev_max_backlog = 8096
    net.core.rmem_default = 262144
    net.core.wmem_default = 262144
    net.core.rmem_max = 2097152
    net.core.wmem_max = 2097152
    net.ipv4.tcp_syncookies = 1
    net.ipv4.tcp_max_syn_backlog = 1024
    

    然后执行sysctl-p命令,使上述修改生效。

    上面的参数意义解释如下:

    ·file-max:这个参数表示进程(比如一个worker进程)可以同时打开的最大句柄数,这个参数直接限制最大并发连接数,需根据实际情况配置。
    ·tcp_tw_reuse:这个参数设置为1,表示允许将TIME-WAIT状态的socket重新用于新的TCP连接,这对于服务器来说很有意义,因为服务器上总会有大量TIME-WAIT状态的连接。
    ·tcp_keepalive_time:这个参数表示当keepalive启用时,TCP发送keepalive消息的频度。默认是2小时,若将其设置得小一些,可以更快地清理无效的连接。
    ·tcp_fin_timeout:这个参数表示当服务器主动关闭连接时,socket保持在FIN-WAIT-2状态的最大时间。
    ·tcp_max_tw_buckets:这个参数表示操作系统允许TIME_WAIT套接字数量的最大值,如果超过这个数字,TIME_WAIT套接字将立刻被清除并打印警告信息。该参数默认为180000,过多的TIME_WAIT套接字会使Web服务器变慢。
    ·tcp_max_syn_backlog:这个参数表示TCP三次握手建立阶段接收SYN请求队列的最大长度,默认为1024,将其设置得大一些可以使出现Nginx繁忙来不及accept新连接的情况时,Linux不至于丢失客户端发起的连接请求。   ·ip_local_port_range:这个参数定义了在UDP和TCP连接中本地(不包括连接的远端)端口的取值范围。
    ·net.ipv4.tcp_rmem:这个参数定义了TCP接收缓存(用于TCP接收滑动窗口)的最小值、默认值、最大值。
    ·net.ipv4.tcp_wmem:这个参数定义了TCP发送缓存(用于TCP发送滑动窗口)的最小值、默认值、最大值。   ·netdev_max_backlog:当网卡接收数据包的速度大于内核处理的速度时,会有一个队列保存这些数据包。这个参数表示该队列的最大值。
    ·rmem_default:这个参数表示内核套接字接收缓存区默认的大小。
    ·wmem_default:这个参数表示内核套接字发送缓存区默认的大小。
    ·rmem_max:这个参数表示内核套接字接收缓存区的最大大小。
    ·wmem_max:这个参数表示内核套接字发送缓存区的最大大小。
    ·tcp_syncookies:该参数与性能无关,用于解决TCP的SYN攻击。
    
    注意 滑动窗口的大小与套接字缓存区会在一定程度上影响并发连接的数目。每个TCP连接都会为维护TCP滑动窗口而消耗内存,这个窗口会根据服务器的处理速度收缩或扩张。参数wmem_max的设置,需要平衡物理内存的总大小、Nginx并发处理的最大连接数量(由nginx.conf中的worker_processes和worker_con-nections参数决定)而确定。当然,如果仅仅为了提高并发量使服务器不出现Out Of Memory问题而去降低滑动窗口大小,那么并不合适,因为滑动窗口过小会影响大数据量的传输速度。rmem_default、wmem_de-fault、rmem_max、wmem_max这4个参数的设置需要根据我们的业务特性以及实际的硬件成本来综合考虑。
    

    编译安装

    根据系统选择你需要的版本,这里我们一般选择源码编译安装,这样可定制度更高

    解压后就如目录可通过./configure --help 查看编译选项,如下 看起来很多,这可能会吓到你,其实没必要担心,一般情况下你只需关注你需要的就行,这个可以作为一个字典,方便你需要的时候查看

    [nick@lvs-master nginx-1.6.3]$ ./configure --help
    
      --help                             print this message    打印出这些信息
    
      --prefix=PATH                      set installation prefix    设置安装路径(一般按这个设置就行了),默认/usr/local/nginx
      --sbin-path=PATH                   set nginx binary pathname  设置sbin目录的安装路径
      --conf-path=PATH                   set nginx.conf pathname     设置conf目录的安装路径
      --error-log-path=PATH              set error log pathname       设置错误log目录的安装路径
      --pid-path=PATH                    set nginx.pid pathname         设置pid文件的路径
      --lock-path=PATH                   set nginx.lock pathname       设置lock文件的路径
    
      --user=USER                        set non-privileged user for       设置程序运行的用户环境
                                         worker processes
      --group=GROUP                      set non-privileged group for 设置程序运行的组环境
                                         worker processes
    
      --builddir=DIR                     set build directory                        设定程序编译目录
    
      --with-rtsig_module                enable rtsig module                  允许rtsig模块
    rtsig模块是一种实时信号,在Linux 2.2.19 默认情况下,实时信号连接数不超过1024,但是对于高负载是肯定不够的。因此通过调整内核参数/proc/sys/kernel/rtsig-max达到效果。但是Linux 2.6.6-mm2开始,这个参数不再可用,并为每个进程有一个独立的信号队列,数字是由RLIMIT_SIGPENDING确定。当队列变得满载时,nginx开始抛弃连接并使用poll方法,直到负载恢复正常。
      --with-select_module               enable select module         允许select模块(一种轮询模式,不推荐用在高载环境)
      --without-select_module            disable select module      使用select模块
    标准连接模式。默认情况下自动编译方式。您可以启用或禁用通过使用-select_module和不带- select_module配置参数这个模块。
      --with-poll_module                 enable poll module      允许poll模块(一种轮询模式,不推荐用在高载环境)
    标准连接模式。默认情况下自动编译方式。
      --without-poll_module              disable poll module   不使用poll模块
    
      --with-file-aio                    enable file AIO support
      --with-ipv6                        enable IPv6 support
    
      --with-http_ssl_module             enable ngx_http_ssl_module   允许ngx_http_ssl_module模块(Apache对应:mod_ssl)
      --with-http_spdy_module            enable ngx_http_spdy_module
      --with-http_realip_module          enable ngx_http_realip_module   允许ngx_http_realip_module模块(mod_rpaf)
    此模块支持显示真实来源IP地址,主要用于NGINX做前端负载均衡服务器使用。
      --with-http_addition_module        enable ngx_http_addition_module    允许ngx_http_addition_module模(mod_layout)
    游戏服务器不必安装,门户网站可以安装,有利于被搜索引擎收录页面信息。
      --with-http_xslt_module            enable ngx_http_xslt_module    允许ngx_http_xslt_module模块
    这个模块是一个过滤器,它可以通过XSLT模板转换XML应答。0.7.8后面版本才可以使用。
      --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    允许ngx_http_sub_module模块
    这个模块可以能够在nginx的应答中搜索并替换文本。
      --with-http_dav_module             enable ngx_http_dav_module    允许ngx_http_dav_module模块(mod_dav)
    为文件和目录指定权限,限制不同类型的用户对于页面有不同的操作权限
      --with-http_flv_module             enable ngx_http_flv_module    --with-http_flv_module 允许ngx_http_flv_module模块(mod_flvx)
    这个模块支持对FLV(flash)文件的拖动播放。
      --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    允许ngx_http_gzip_static_module模块(mod_dflate)。
    这个模块在一个预压缩文件传送到开启Gzip压缩的客户端之前检查是否已经存在以“.gz”结尾的压缩文件,这样可以防止文件被重复压缩。
      --with-http_auth_request_module    enable ngx_http_auth_request_module
      --with-http_random_index_module    enable ngx_http_random_index_module    允ngx_http_random_index_module模块(mod_autoindex),从目录中选择一个随机主页
      --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    允许ngx_http_stub_status_module模块(mod_status)
    这个模块可以取得一些nginx的运行状态,如果是工业状况,可以直接取消。
      --without-http_charset_module      disable ngx_http_charset_module    不使用ngx_http_charset_module模块
    这个模块将在应答头中为"Content-Type"字段添加字符编码
      --without-http_gzip_module         disable ngx_http_gzip_module    --without-http_gzip_module 不使用ngx_http_gzip_module模块,文件压缩模式。
      --without-http_ssi_module          disable ngx_http_ssi_module     --without-http_ssi_module 不使用ngx_http_ssi_module模块,此模块处理服务器端包含文件(ssi)的处理.
      --without-http_userid_module       disable ngx_http_userid_module    不使用ngx_http_userid_module模块
      --without-http_access_module       disable ngx_http_access_module   不使用ngx_http_access_module模块
      --without-http_auth_basic_module   disable ngx_http_auth_basic_module    不使用ngx_http_auth_basic_module模块
      --without-http_autoindex_module    disable ngx_http_autoindex_module    不使用ngx_http_autoindex_module模块
      --without-http_geo_module          disable ngx_http_geo_module     不使用ngx_http_geo_module模块
    这个模块基于客户端的IP地址创建一些ngx_http_geoip_module变量,并与MaxMindGeoIP文件进行匹配,该模块仅用于0.7.63和0.8.6版本之后。但效果不太理想,对于城市的IP记录并不是特别准确,不过对于网站的来源访问区域的分析大致有一定参考性。
      --without-http_map_module          disable ngx_http_map_module    不使用ngx_http_map_module模块
    这个模块允许你分类或者同时映射多个值到多个不同值并储存到一个变量中,可用于页面跳转其他域名使用。
      --without-http_split_clients_module disable ngx_http_split_clients_module
      --without-http_referer_module      disable ngx_http_referer_module    不使用ngx_http_referer_module模块
    当一个请求头的Referer字段中包含一些非正确的字段,这个模块可以禁止这个请求访问站点。可以禁止盗链的情况发生。
      --without-http_rewrite_module      disable ngx_http_rewrite_module    不使用ngx_http_rewrite_module模块,跳转模块
      --without-http_proxy_module        disable ngx_http_proxy_module    不使用ngx_http_proxy_module模块,代理模块
      --without-http_fastcgi_module      disable ngx_http_fastcgi_module    不使用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    不使ngx_http_memcached_module模块
      --without-http_limit_conn_module   disable ngx_http_limit_conn_module   不使用ngx_http_limit_zone_module模块
    此模块可以限制并发连接,达到减少攻击的效果
      --without-http_limit_req_module    disable ngx_http_limit_req_module
      --without-http_empty_gif_module    disable ngx_http_empty_gif_module    不使用ngx_http_empty_gif_module模块
    这个模块在内存中保存一个能够很快传递的1×1透明GIF
      --without-http_browser_module      disable ngx_http_browser_module    不使用ngx_http_browser_module模块
    识别客户端浏览器版本来制定计划。可以实现更加来源浏览器的版本访问不同页面的效果。
      --without-http_upstream_ip_hash_module
                                         disable ngx_http_upstream_ip_hash_module    不使用ngx_http_upstream_ip_hash_module模块
      --without-http_upstream_least_conn_module
                                         disable ngx_http_upstream_least_conn_module
      --without-http_upstream_keepalive_module
                                         disable ngx_http_upstream_keepalive_module
    
      --with-http_perl_module            enable ngx_http_perl_module    这个模块允许nginx使用SSI调用perl或直接执行perl
      --with-perl_modules_path=PATH      set Perl modules path    设置perl模块路径
      --with-perl=PATH                   set perl binary pathname     设置perl库文件路径
    
      --http-log-path=PATH               set http access log pathname     设置access log文件路径
      --http-client-body-temp-path=PATH  set path to store
                                         http client request body temporary files     设置客户端请求临时文件路径
      --http-proxy-temp-path=PATH        set path to store
                                         http proxy temporary files     设置http proxy临时文件路径
      --http-fastcgi-temp-path=PATH      set path to store
                                         http fastcgi temporary files     设置http fastcgi临时文件路径
      --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     不使用HTTP server功能,如果只是做代理服务器,可以不提供http服务
      --without-http-cache               disable HTTP cache
    
      --with-mail                        enable POP3/IMAP4/SMTP proxy module     允许POP3/IMAP4/SMTP代理模块
      --with-mail_ssl_module             enable ngx_mail_ssl_module     允许ngx_mail_ssl_module模块
    这个模块使得POP3/IMAP/SMTP可以使用SSL/TLS.配置已经定义了HTTP SSL模块,但是不支持客户端证书检测
      --without-mail_pop3_module         disable ngx_mail_pop3_module     不允许ngx_mail_pop3_module模块
      --without-mail_imap_module         disable ngx_mail_imap_module     不允许ngx_mail_imap_module模块
      --without-mail_smtp_module         disable ngx_mail_smtp_module     不允许ngx_mail_smtp_module模块
    
      --with-google_perftools_module     enable ngx_google_perftools_module     允许ngx_google_perftools_module模块(调试用)
    这个模块可以启用google性能分析工具套件,模块在版本0.6.29增加。
      --with-cpp_test_module             enable ngx_cpp_test_module     允许ngx_cpp_test_module模块
    
      --add-module=PATH                  enable an external module     允许使用外部模块,以及路径
    
      --with-cc=PATH                     set C compiler pathname     设置C编译器路径
      --with-cpp=PATH                    set C preprocessor pathname     设置C预处理路径
      --with-cc-opt=OPTIONS              set additional C compiler options     设置C编译器参数
      --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      为指定CPU优化,可选参数有:
                                       pentium, pentiumpro, pentium3, pentium4,
                                       athlon, opteron, sparc32, sparc64, ppc64
    
      --without-pcre                     disable PCRE library usage     不使用pcre库文件,pcre是包括 perl 兼容的正规表达式库
      --with-pcre                        force PCRE library usage
      --with-pcre=DIR                    set path to PCRE library sources     设定PCRE库路径
      --with-pcre-opt=OPTIONS            set additional build options for PCRE     设置PCRE运行参数
      --with-pcre-jit                    build PCRE with JIT compilation support
    
      --with-md5=DIR                     set path to md5 library sources     设定md5库文件路径
      --with-md5-opt=OPTIONS             set additional build options for md5     设置md5运行参数
      --with-md5-asm                     use md5 assembler sources      使用md5源文件编译
    
      --with-sha1=DIR                    set path to sha1 library sources     设定sha1库文件路径
    sha1:安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准(Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signature Algorithm DSA)。对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。当接收到消息的时候,这个消息摘要可以用来验证数据的完整性。在传输的过程中,数据很可能会发生变化,那么这时候就会产生不同的消息摘要。
      --with-sha1-opt=OPTIONS            set additional build options for sha1     设置sha1运行参数
      --with-sha1-asm                    use sha1 assembler sources     使用sha1源文件编译
    
      --with-zlib=DIR                    set path to zlib library sources     设定zlib库文件路径
    很多程序中的压缩或者解压缩函数都会用到这个库
      --with-zlib-opt=OPTIONS            set additional build options for zlib     设置zlib运行参数
      --with-zlib-asm=CPU                use zlib assembler sources optimized
                                         for the specified CPU, valid values:
                                         pentium, pentiumpro     使zlib对特定的CPU进行优化,可选参数:
                                       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     设定OpenSSL库文件路径
    Openssl作为一个基于密码学的安全开发包,OpenSSL提供的功能相当强大和全面,囊括了主要的密码算法、常用的密钥和证书封装管理功能以及SSL协议,并提供了丰富的应用程序供测试或其它目的使用。
      --with-openssl-opt=OPTIONS         set additional build options for OpenSSL     设置OpenSSL运行参数
      --with-debug                       enable debug logging     允许调试日志
    

    一个典型的安装

    [root@www nginx-1.8.1]# ./configure 
    --prefix=/home/nick/nginx 
    --with-http_stub_status_module 
    --with-http_ssl_module 
    --with-http_gzip_static_module 
    --with-pcre 
    --with-http_realip_module 
    --with-http_sub_module
    
    make && make install
    

    启动 关闭 重启

    1、启动:nginx
    2、停止:nginx -s stop
    3、退出:nginx -s quit
    4、重启:nginx -s reopen
    5、重新加载:nginx -s reload
    6、平滑启动:kill -HUP pid(kill -HUP `cat /var/run/nginx.pid`)
    
    也可以指定配置文件启动
    
    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    
    停止nginx
    
    kill - 信号类型或者 QUIT pid/主进程号
    
    快速停止nginx
    
    kill - TERM/INT 主进程号/pid
    
    信号说明
    
    TERM INT  快速关闭
    QUIT 从容关闭
    HUP 平滑重启
    USER1 重新打开日志文件,切割日志的时候用途较大
    USER2 平缓升级可执行程序
    WINCH 从容关闭工作进程
    
    生产环境中建议使用supervisor管理nginx
    

    升级

    众所周知,nginx可以在不中断生产的情况下平滑升级,下面是过程

    1.备份可执行文件,然后使用新的可执行文件替换旧的可执行文件(编译安装)
    2.kill -USER2 旧nginx主进程号
    3.旧版本nginx的主进程重命名它的pid为 .oldbin(例如:/usr/local/webserver/nginx/logs/nginx.pid.oldbin),然后执行新版本的nginx可执行程序,一次启动新的主进程和工作进程。
    4.此时,新旧版本的nginx实例会同事进行,共同处理输入请求。要逐步停止旧版本的nginx实例,你必须发送WINCH信号给旧的主进程,然后它的工作进程就开始从容关闭:kill -WINCH 旧版本的nginx的主进程号
    5.一段时间后,旧的工作进程处理了所有已连接的请求后退出,仅由新的工作进程处理输入的请求了
    6.这时候,我们可以决定使用新版本还是恢复到旧的版本:
    kill -HUP 旧的主进程号:nginx将在不重载配置文件的情况下启动它的工作进程
    kill -QUIT 新的主进程号:从容关闭其工作进程
    kill -TERM 新的主进程号:强制退出
    kill 新的主进程号或者旧的主进程号:因为某些原因新的工作进程不能退出,则向其发送kill信号
    
    新的主进程号退出后,旧的主进程号会移除.oldbin 后缀,恢复它的.pid文件,这样一切就恢复到升级之前了,如果升级成功,你也希望保留新的服务器时,可发送 QUIT 信号给旧的主进程,使其退出而只保留新的服务器运行。
  • 相关阅读:
    ECSHOP 2.5.1 二次开发文档【文件结构说明和数据库表分析】
    ECSHOP后台左侧添加菜单栏
    PHPstudy 2018 集成环境项目配置虚拟域名访问
    微擎左侧模块业务菜单修改
    ThinkPHP5使用阿里云OSS图片上传
    ThinkPHP5使用PHPExcel实现数据导出功能
    ThinkPHP5生成二维码图片与另一张背景图片进行合成
    原生PHP连接MySQL数据库
    数组的冒泡排序
    【原创诗歌】读仓央嘉措(下)
  • 原文地址:https://www.cnblogs.com/colder219/p/5602277.html
Copyright © 2011-2022 走看看