当前位置: 首页 > Linux

【nginx运维基础(一)】Nginx的编译安装与使用

时间:2023-04-06 21:12:15 Linux

nginx官方手册:http://nginx.org/en/docs/编译安装(本文以centos为例)下载地址:http://nginx.org/en/download....#为了支持rewrite功能,我们需要安装pcreyuminstallpcre*#安装openssl,如果不需要ssl支持请跳过这一步yuminstallopenssl*#编译./configure--prefix=/usr/local/nginx-1.5.1\--with-http_ssl_module\--with-http_stub_status_module--with-pcre#编译参数说明--with-http_stub_status_module#支持nginx状态查询--with-http_ssl_module#支持https--with-pcre#为了支持rewrite功能,必须制定pcrecd/usr/local/nginx。看下面4个目录conf配置文件html网页文件logs日志文件sbin主二进制程序configure参数说明--prefix=指向安装目录--sbin-path指向(执行)程序文件(nginx)--conf-path=指向配置文件(nginx.conf)--error-log-path=指向错误日志目录--pid-path=指向pid文件(nginx.pid)--lock-path=指向到锁定文件(nginx.lock)(安装文件被锁定,以防止安装文件被他人使用或自己误操作。)--user=指定程序运行时的非特权用户--group=指定程序运行时的非特权用户组--builddir=指向编译目录--with-rtsig_module开启rtsig模块支持(real-timesignal)--with-select_moduleEnableselectmodulesupport(一种轮询模式,高负载环境下不推荐使用)Disable:--without-select_module--with-poll_moduleEnablepollmodulesupport(功能为同select,同selectfeatures,foronepollingmode,高负载环境不推荐)--with-file-aioenablefileaiosupport(一种APL文件传输格式)--with-ipv6enableipv6support--with-http_ssl_moduleenablengx_http_ssl_modulesupport(支持https请求,需要安装openssl)--with-http_realip_moduleenablengx_http_realip_modulesupport(该模块允许从请求头改变客户端的IP地址值,默认关闭)--with-http_addition_moduleenablengx_http_addition_modulesupport(作为输出过滤设备,支持不完全缓冲,分块响应请求)--with-http_xslt_moduleenablengx_http_xslt_modulesupport(过滤和转换XML请求)--with-http_image_filter_moduleenablengx_http_image_filter_modulesupport(一个过滤器用于传输JPEG/GIF/PNG图像)(默认为未启用。要使用的gd库)--with-http_geoip_module启用ngx_http_geoip_module支持(此模块根据与MaxMindGeoIP二进制文件匹配的客户端IP地址创建ngx_http_geoip_module变量)--with-http_sub_module启用ngx_http_sub_module支持(允许用其他文本替换nginx一些文本在响应中)--with-http_dav_moduleenablengx_http_dav_modulesupport(addPUT,DELETE,MKCOL:createacollection,COPYandMOVEmethods)默认关闭,需要编译打开--with-http_flv_moduleenablengx_http_flv_modulesupport(提供seekingMemoryusagetime-basedoffsetfile)--with-http_gzip_static_moduleenablengx_http_gzip_static_modulesupport(onlinereal-timecompressionoutputdatastream)--with-http_random_index_moduleenablengx_http_random_index_modulesupport(从目录中随机选择一个目录索引)--with-http_secure_link_module启用ngx_http_secure_link_module支持(计算和检查要求所需的安全链接url)--with-http_degradation_module启用ngx_http_degradation_module支持(允许在内存不足的情况下返回204或444代码)--with-http_stub_status_module启用ngx_http_stub_status_module支持(自上次工作状态以来获取nginx自启动以来)--without-http_charset_module禁用ngx_http_charset_module支持(重新编码网页,但只能在一个方向——服务器到客户端,并且只能重新编码单字节编码)--without-http_gzip_module禁用ngx_http_gzip_module支持(此模块与-with-http_gzip_static_module具有相同的功能)--without-http_ssi_module禁用ngx_http_ssi_module支持(此模块提供了一个过滤器,用于处理输入中的服务器包含文件(SSI),支持的SSI命令列表目前不完整)--without-http_userid_module禁用ngx_http_userid_module支持(此模块用于处理用于确定来自客户端的后续请求)--without-http_access_module禁用ngx_http_access_module支持(该模块提供了一个简单的基于主机的访问控制允许/拒绝基于ip地址)--without-http_auth_basic_module禁用ngx_http_auth_basic_module(该模块是你可以使用用户名和passwordtoprotectyoursiteorpartofcontentbasedonhttpbasicauthenticationmethod)--without-http_autoindex_moduledisable禁用ngx_http_autoindex_module支持(该模块用于自动生成目录列表,仅在ngx_http_index_module模块没有找到索引文件请求时发出.)--without-http_geo_module禁用ngx_http_geo_module支持(创建一些变量,其值取决于客户端的IP地址)(该模块用于根据一定条件划分用户。条件如:ip地址、header、cookies等)--without-http_referer_moduledisabledisablengx_http_referer_modulesupport(该模块用于过滤请求,拒绝header中Referer值不正确的请求)--without-http_rewrite_moduleDisablengx_http_rewrite_module支持(该模块允许使用正则表达式更改URI,并根据变量重定向和选择配置。如果在服务器级别设置此选项,则它们将在定位之前生效。如果位置中有进一步的重写规则,位置部分中的规则仍然会被执行。如果URI由于location部分中的规则而被重写,则location部分将作为新的URI再次执行。这个循环会执行10次,然后Nginx会返回500错误。)--without-http_proxy_moduledisablesngx_http_proxy_modulesupport(forproxyservers)--without-http_fastcgi_moduledisablesngx_http_fastcgi_modulesupport(该模块允许Nginx与FastCGI进程交互,通过传递参数控制FastCGI进程的工作。)FastCGI是常驻的公共网关接口。--without-http_uwsgi_moduledisablengx_http_uwsgi_modulesupport(该模块用于医疗uwsgi协议,uWSGI服务器相关)--without-http_scgi_moduledisablengx_http_scgi_modulesupport(该模块用于启用SCGI协议支持,SCGI协议是CGI协议的替代品。它是一个应用程序和HTTP服务接口标准,有点像FastCGI,但他的设计更容易实现。)--without-http_memcached_moduleDisablengx_http_memcached_modulesupport(该模块用于提供简单的缓存以提高系统效率)-without-http_limit_zone_moduleDisablengx_http_limit_zone_modulesupport(该模块可以根据条件控制session的并发连接数)--without-http_limit_req_module禁用ngx_http_limit_req_module支持(此模块允许您限制对给定会话或特定事件的地址的请求数)--without-http_empty_gif_module禁用ngx_http_empty_gif_module支持(此模块驻留在内存中,带有1*1透明GIF图像,可以很快调用)--without-http_browser_module禁用ngx_http_browser_module支持(该模块用于根据请求头创建值。如果浏览器是现代的,则$modern_browser等于modern_browser_value指令分配的值;如果浏览器是旧的,则$ancient_browser等于ancient_browser_value指令分配的值;如果浏览器是MSIE中的任意版本,则$msie等于1)--without-http_upstream_ip_hash_moduledisablengx_http_upstream_ip_hash_modulesupport(该模块用于简单的负载均衡)--with-http_perl_moduleenablengx_http_perl_modulesupport(该模块使nginx可以使用直接perl或者通过ssi调用perl)--with-perl_modules_path=设置perl模块路径--with-perl=设置perl库文件路径--http-log-path=设置访问日志路径--http-client-body-temp-path=设置http客户端请求临时文件路径--http-proxy-temp-path=设置http代理临时文件路径--http-fastcgi-temp-path=设置httpfastcgi临时文件路径--http-uwsgi-temp-path=设置httpuwsgi临时文件路径--http-scgi-temp-path=设置httpscgi临时文件路径-without-http禁用http服务器功能--without-http-cache禁用http缓存功能--with-mail启用POP3/IMAP4/SMTPproxymodulesupport--with-mail_ssl_module启用ngx_mail_ssl_modulesupport--without-mail_pop3_module禁用pop3协议(POP3是PostOfficeProtocol的第三个版本,它是一个协议,规定了个人电脑如何连接邮件服务器在Internet上发送和接收邮件。它是互联网电子邮件的第一个离线协议标准,POP3协议允许用户将邮件从服务器存储到本地主机,同时根据客户端的操作删除或保存邮件服务器上的邮件。POP3协议是TCP/IP协议族的一员,主要用于支持使用客户端远程管理服务器上的邮件)--without-mail_imap_module禁用imap协议(一种邮件获取协议。其主要功能是mail客户端可以通过该协议从邮件服务器获取邮件信息,下载邮件等。IMAP协议运行在TCP/IP协议之上,使用的端口为143。它与POP3协议的主要区别在于:用户不需要发送所有下载所有邮件,可以直接通过客户端操作服务器上的邮件。)--without-mail_smtp_module禁用smtp协议(SMTP是SimpleMailTransferProtocol,是一组协议用于将邮件从源地址传送到目的地址的规则,控制信件的传送。SMTP协议属于TCP/IP协议族,帮助每台计算机在发送或传送信件时找到下一个目的地。)--with-google_perftools_moduleenablengx_google_perftools_modulesupport(fordebugging,Analyzeprogramperformancebottlenecks)--with-cpp_test_moduleenablengx_cpp_test_modulesupport--add-module=enableexternalmodulesupport--with-cc=指向C编译器路径--with-cpp=指向C预处理路径--with-cc-opt=设置C编译器参数(PCRE库,需要指定–with-cc-opt=”-I/usr/local/include”,如果使用select()函数,需要同时增加文件描述符个数,可以通过-with-cc-opt="-DFD_SETSIZE=2048"指定。)--with-ld-opt=设置链接文件参数。(PCRE库,需要指定--with-ld-opt="-L/usr/local/lib"。)--with-cpu-opt=指定编译后的CPU,可用值有:pentium,pentiumpro,pentium3,pentium4,athlon,opteron,amd64,sparc32,sparc64,ppc64--without-pcredisablepcrelibrary--with-pcre启用pcre库--with-pcre=指向pcre库文件目录--with-pcre-opt=在编译时为pcre库设置附加参数--with-md5=指向md5库文件目录(消息摘要算法第五版,提供消息完整性保护)--with-md5-opt=编译时为md5库设置附加参数--with-md5-asm使用md5汇编源--with-sha1=指向sha1库目录(数字签名算法,主要用于数字签名)--with-sha1-opt=编译时为sha1库设置附加参数--with-sha1-asm使用sha1汇编源--with-zlib=指向zlib库目录--with-zlib-opt=在编译时为zlib设置附加参数--with-zlib-asm=使用指定CPU的zlib汇编源进行优化,CPU类型为奔腾,pentiumpro--with-libatomic是原子内存的更新操作实现提供了一个架构--with-libatomic=指向libatomic_ops安装目录--with-openssl=指向openssl安装目录--with-openssl-optsetsopenssl编译时附加参数--with-debug启用调试日志常用操作#start/usr/nginx/sbin/nginxnginxstartservicenginxstart#belowcentos7#restartkill-HUP主进程号或进程号文件路径/usr/nginx/sbin/nginx-sreload#关闭/usr/nginx/sbin/nginx-sstop#停止运行ps-ef|grepnginx#查看主进程号kill-QUIT主进程号#冷静停止kill-TERM主进程号#快速停止nginxpkill-9nginx#强行停止停止Nginx#另外,如果nginx.conf中配置了pid文件存放路径,该文件存放的是Nginx主进程号。如果不指定,则放在nginxlogs目录下。有了pid文件,我们就不需要先查询Nginx了。主进程号,并直接向Nginx发送信号,命令如下:kill-signaltype'/usr/nginx/logs/nginx.pid'signalcontrolsyntaxcommandsignaloptionnginx主进程号Kill-HUP4873Kill-USR1cat/xxx/path/log/nginx.pidsignaloptioncommandsignaloptionTERM,INT快速关闭QUIT并优雅关闭进程,即请求完成后,关闭HUP更改配置文件,顺利重新读取配置文件(无需重启)USR1重新读取日志,在日志按月/日划分时,使用USR2顺利升级WINCH优雅关闭旧进程(配合USR2升级)