依赖模块安装
GCC(GNU Compiler Collection)可用来编译C语言程序,Nginx不会直接提供二进制可执行程序,GCC是必须的编译工具
如果遇到C++编写的Nginx模块,则必须安装gcc-c++编译器的支持
yum -y install gcc
PCRE(Perl Compatible Regular Expressions) Perl兼容正则表达式,该库是Nginx正则表达式锁支持的,如果在http映射url
上引用了正则表达式则必须引入这个包
yum install -y pcre pcre-devel
zlib库用于对HTTP包的内容做gzip格式的压缩,如果我们在nginx.conf里配置了gzip on,并制定对于某些content-type 的http
响应使用gzip来进行压缩以减少网络传输量,那么在编译时就必须引入zlib包,编译进Nginx
yum install -y zlib zlib-devel
OpenSSL是我们需要在https,SSL协议上传输http的时候需要的库,另外,如果我们需要使用MD5,SHA1,等散列函数,那么也需要
安装它。
yum install -y openssl openssl-devel
下载Nginx源码包,安装
wget http://nginx.org/download/nginx-1.2.9.tar.gz
tar -zxvf nginx-1.2.9.tar.gz
查看帮助信息
./configure --help
--help print this message
--prefix=PATH set installation prefix
--sbin-path=PATH set nginx binary pathname
--conf-path=PATH set nginx.conf pathname
--error-log-path=PATH set error log pathname
--pid-path=PATH set nginx.pid pathname
--lock-path=PATH set nginx.lock pathname
--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
--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_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_gzip_static_module enable ngx_http_gzip_static_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 disable ngx_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
disable 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
--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
http client request body temporary files
--http-proxy-temp-path=PATH set path to store
http proxy temporary files
--http-fastcgi-temp-path=PATH set path to store
http fastcgi 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 library usage
--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 the specified 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
编译,将SSL模块编译进去
./configure --with-http_ssl_module
配置日志信息如下:
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
如此我们便可以知道Nginx默认安装完成以后的一些目录情况
-部署目录
-日志存放目录
make && make install
objs目录
安装目录configure在编译之后生成目标文件中带有objs目录
-src目录用于存放编译时产生的目标文件
-Makefile文件用于编译Nginx工程以及在加入install参数后安装Nginx
-autoconf.err保存configure执行过程中产生的结果
-ngx_auto_headers.h和ngx_auto_config.h保存了一些宏,这两个头文件会被src/core/ngx_config.h及
src/os/unix/ngx_linux_confg.h文件引用
ngx_modules.c文件
#include <ngx_config.h>
#include <ngx_core.h>
extern ngx_module_t ngx_core_module;
extern ngx_module_t ngx_errlog_module;
extern ngx_module_t ngx_conf_module;
extern ngx_module_t ngx_events_module;
extern ngx_module_t ngx_event_core_module;
extern ngx_module_t ngx_epoll_module;
extern ngx_module_t ngx_openssl_module;
extern ngx_module_t ngx_regex_module;
extern ngx_module_t ngx_http_module;
extern ngx_module_t ngx_http_core_module;
extern ngx_module_t ngx_http_log_module;
extern ngx_module_t ngx_http_upstream_module;
extern ngx_module_t ngx_http_static_module;
extern ngx_module_t ngx_http_autoindex_module;
extern ngx_module_t ngx_http_index_module;
extern ngx_module_t ngx_http_auth_basic_module;
extern ngx_module_t ngx_http_access_module;
extern ngx_module_t ngx_http_limit_conn_module;
extern ngx_module_t ngx_http_limit_req_module;
extern ngx_module_t ngx_http_geo_module;
extern ngx_module_t ngx_http_map_module;
extern ngx_module_t ngx_http_split_clients_module;
extern ngx_module_t ngx_http_referer_module;
extern ngx_module_t ngx_http_rewrite_module;
extern ngx_module_t ngx_http_ssl_module;
extern ngx_module_t ngx_http_proxy_module;
extern ngx_module_t ngx_http_fastcgi_module;
extern ngx_module_t ngx_http_uwsgi_module;
extern ngx_module_t ngx_http_scgi_module;
extern ngx_module_t ngx_http_memcached_module;
extern ngx_module_t ngx_http_empty_gif_module;
extern ngx_module_t ngx_http_browser_module;
extern ngx_module_t ngx_http_upstream_ip_hash_module;
extern ngx_module_t ngx_http_upstream_least_conn_module;
extern ngx_module_t ngx_http_upstream_keepalive_module;
extern ngx_module_t ngx_http_write_filter_module;
extern ngx_module_t ngx_http_header_filter_module;
extern ngx_module_t ngx_http_chunked_filter_module;
extern ngx_module_t ngx_http_range_header_filter_module;
extern ngx_module_t ngx_http_gzip_filter_module;
extern ngx_module_t ngx_http_postpone_filter_module;
extern ngx_module_t ngx_http_ssi_filter_module;
extern ngx_module_t ngx_http_charset_filter_module;
extern ngx_module_t ngx_http_userid_filter_module;
extern ngx_module_t ngx_http_headers_filter_module;
extern ngx_module_t ngx_http_copy_filter_module;
extern ngx_module_t ngx_http_range_body_filter_module;
extern ngx_module_t ngx_http_not_modified_filter_module;
此文件控制了nginx module的处理顺序。按照数组中位置先手顺序处理,
假设一个请求满足多个模块的过滤规则,那么则按照此文件数组的从上往下的顺序依次执行
但http模块则不同,http模块顺序是相反的,因为http模块会压入到一个链表中
每次添加到链表的头部。所以顺序是相反的。
Nginx的命令行控制
-默认启动文件:
/usr/local/nginx/sbin/nginx
-默认配置文件(默认配置文件可以通过configure的--conf-path来指定默认配置文件路径)
/usr/local/nginx/conf/nginx.conf
-启动时指定配置文件位置
/usr/local/nginx/sbin/nginx -c [path]
-指定Nginx的安装目录(-p 参数是指,如果没有按照默认路径进行安装Nginx)
/usr/local/nginx/sbin/nginx -p [path]/usr/local/nginx
-在Nginx中存放Nginx进程的pid号码的文件默认存放在:
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
在启动时可以通过-g 参数来指定目录和文件名
/usr/local/nginx/sbin/nginx -g [path] "/usr/local/nginx/logs/nginx.pid"
在以-g命令启动服务时,在执行其他命令的时候也必须要带上-g命令。必须停止Nginx服务
所以不推荐使用
停止Nginx服务
/usr/local/nginx/sbin/nginx -s stop
如果希望Nginx服务可以正常的处理完当前所有请求再停止服务,那么可以使用-s quit来停止服务
/usr/local/nginx/sbin/nginx -s stop
或:
kill -s SIGQUIT <nginx master pid>
如果只是希望停止某个worker进程那么可以执行以下命令:
kill -s SIGWINCH <nginx worker pid>
-----重新加载NGINX配置文件
/usr/local/nginx/sbin/nginx -s reload
或
kill -s SIGHUP <nginx master pid>
------日志文件回滚
该操作使得当前日志文件重命名或者复制到其他地方,重新开启一个日志文件。
使得当前的日志文件不至于过大
/usr/local/nginx/sbin/nginx -s reopen
或
kill -s SIGUSER1 <nginx master id>
平滑升级Nginx
1-通知正在运行的Nginx准备升级,通过向Master进行发送以下指令
kill -s SIGUSER2 <nginx master pid>
此刻老的Nginx Pid将会重命名
然后启动新版本Nginx
关闭老版本Nginx即可
一键安装,脚本地址
https://pan.baidu.com/s/1tPoIOKlS7qI9XDorQjsB5g