nginx——安装和配置详解

参考文章:nginx安装和配置详解

第一章Nginx介绍

Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点开发的。
如果需要更加详细的了解nginx,可以通过nginx的官网去了解,下面是nginx的官网地址
一、中文官网地址
https://www.nginx.cn/doc/
二、英文官网地址
http://nginx.org/
1.1 nginx版本
Nginx的版本分为三种类型的版本:legacy versions、stable version、mainline version。
如果要将nginx在真实的环境中使用,那么需要使用nginx的stable版本,所有建议使用nginx-1.12.2。
1.2 nginx功能介绍
Nginx (engine x) 是一个高性能的HTTP和反向代理服务器。
1.3 nginx文档
中文文档:http://nginx.org/en/docs/
英文文档:http://nginx.org/en/docs/

第二章 Nginx反向代理

nginx作为web服务器一个重要的功能就是反向代理,当然你也可以使用nginx配置正向代理。
2.1 代理模块介绍
代理模块运行nginx将http请求转发到另外的服务器上(通常情况下是web服务器),各个模块间的交互过程如下图所示
nginx——安装和配置详解
2.2 代理配置
代理配置可以参考官网文档,通过代理可以实现前后端分离,后端服务器的负载均衡等功能。
具体的配置参考官网,官网地址如下:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html
2.3 鉴权模块介绍
鉴权模块可以实现对资源的鉴权访问,nginx在接收到需要权限验证的资源请求的时候,会创建一个子请求到后端鉴权服务器,根据服务器的返回结果决定是否允许该次请求访问成功(具体的鉴权方式是,nginx会将当前收到的请求剔除请求体之后转发到后端,后端从cookie中提取sessionId来判断用户权限,如果后端服务器无法提取sessionId或者是sessionId对于的session超时了,那么认定该次请求不合法,那么就拒绝该次请求,并返回重定向信息到登录页面)。
The ngx_http_auth_request_module module (1.5.4+) implements client authorization based on the result of a subrequest. If the subrequest returns a 2xx response code, the access is allowed. If it returns 401 or 403, the access is denied with the corresponding error code. Any other response code returned by the subrequest is considered an error.
For the 401 error, the client also receives the “WWW-Authenticate” header from the subrequest response.
This module is not built by default, it should be enabled with the –with-http_auth_request_module configuration parameter.
具体的验证流程如下图所示:
nginx——安装和配置详解
具体功能,以及详细的配置信息,建议参考官网配置,官网的地址如下:
http://nginx.org/en/docs/http/ngx_http_auth_request_module.html

第三章 源码编译安装

window环境下可以下载免安装包;linux环境如果可以连接网络通过配置repository可以直接安装(可能有些操作系统没有对应的版本);如果linux不能连接网络,或者是repository中没有对应的版本,那么需要通过源码安装。本章主要介绍nginx的源码安装。
源码编译安装在官网已经给出了比较详细的说明,建议参考官网的源码编译安装文档。但是由于官网安装配置选项过多,所以本章只介绍部分重要功能模块的源码安装,如果需要更多的扩展功能仍然需要仔细分析官网的功能模块介绍,以及相关功能的安装流程。
官网的安装配置介绍网址:http://nginx.org/en/docs/configure.html
3.1 SSL模块
Ssl模块主要是用于支持https请求,https请求由于建立连接过程中秘钥的协商,以及信息传输加密耗时过大,所以建议只在传输登录、表单等敏感数据的时候使用,其他的情况建议仍然使用http请求,https交互的过程如图所示:
nginx——安装和配置详解
针对nginx作为反向代理的请求的情况,有nginx来控制哪些敏感的数据传输接口需要使用https即可,后端服务器可以不用实现https功能,下面是编译安装的时候和ssl相关的配置的选项说明:
–with-http_ssl_module — enables building a module that adds the HTTPS protocol support to an HTTP server. This module is not built by default. The OpenSSL library is required to build and run this module.(配置是否允许添加ssl功能模块,默认是没有添加的,如果需要支持https,需要添加该选项配置)
3.2 Pcre模块
该模块主要包含了一套正则表达式相关函数,可以用在nginx中的location路径过滤中,如果需要location路径的正则表达式的过滤,那么必须要在编译安装nginx的时候关联该模块,下面是安装的时候的相关参数:
–without-http_rewrite_module — disables building a module that allows an HTTP server to redirect requests and change URI of requests. The PCRE library is required to build and run this module.(禁止url重写,可选)
–with-pcre=path — sets the path to the sources of the PCRE library. The library distribution (version 4.4 — 8.41) needs to be downloaded from the PCRE site and extracted. The rest is done by nginx’s ./configure and make. The library is required for regular expressions support in the location directive and for the ngx_http_rewrite_module module.(配置pcre模块的源码路径,nginx编译安装的时候会自动去该路径下编译和安装该模块,必选)
–with-pcre-jit — builds the PCRE library with “just-in-time compilation” support (1.1.12, the pcre_jit directive).(可选)
3.3 Zlib模块
该模块包含了数据压缩相关算法,可以支持nginx数据压缩传输,针对网络带宽差的情况下,可以明显提高客户端的相应速度,可选。
–without-http_gzip_module — disables building a module that compresses responses of an HTTP server. The zlib library is required to build and run this module.(禁止nginx对传输数据进行压缩,可选)
–with-zlib=path — sets the path to the sources of the zlib library. The library distribution (version 1.1.3 — 1.2.11) needs to be downloaded from the zlib site and extracted. The rest is done by nginx’s ./configure and make. The library is required for the ngx_http_gzip_module module.(如果nginx需要该模块,需要在编译的时候开启该选项,其中path是zlib的源码路径,可选)
3.4 安装配置详解
下面是nginx官方网站的安装说明,下面对相关说明进行解释,并对相关的配置选项进行解释。在几节的模块介绍中,已经部分配置选项进行了说明,本节主要是对nginx的配置做一个全面的介绍。在介绍安装的相关配置选项之前,我们需要对nginx的安装过程做一个简单的说明,具体的安装步骤将在下一节详细说明,编译安装过程简单介绍如下:
1、首先下载nginx和相关模块的源码包并解压。
2、进入到nginx的源码包中,会看到有一个configure执行文件,该文件主要是nginx进行一些编译安装前的配置,如是否需要配置ssl、pcre、zip等模块的信息,是否需要开启或者禁用某些功能等,通过configure执行文件会生成Makefile的配置文件信息,该文件是make和make install的参考。
3、最后make & make install即可完成安装
上面是nginx安装的简单的过程,从上面的过程我们可以看出nginx的大致安装步骤:下载源码->生成配置文件->编译安装。
The build is configured using the configure command. It defines various aspects of the system, including the methods nginx is allowed to use for connection processing. At the end it creates a Makefile. The configure command supports the following parameters:
nginx源码安装前,需要执行configure文件生成编译安装需要的配置文件Makefile,configure支持如下的一些配置选项:

–prefix=path — defines a directory that will keep server files. This same directory will also be used for all relative paths set by configure (except for paths to libraries sources) and in the nginx.conf configuration file. It is set to the /usr/local/nginx directory by default.
该配置主要配置编译之后的文件存放的地方,nginx.conf中的部分路径会参考该路径(不包含一些库的路径),默认使用的是/usr/local/nginx,推荐使用默认的,即不要设置该选项,推荐使用默认。

–sbin-path=path — sets the name of an nginx executable file. This name is used only during installation. By default the file is named prefix/sbin/nginx.
设置编译之后的nginx可执行文件的路径,默认使用prefix/sbin/nginx,推荐使用默认。

–conf-path=path — sets the name of an nginx.conf configuration file. If needs be, nginx can always be started with a different configuration file, by specifying it in the command-line parameter -c file. By default the file is named prefix/conf/nginx.conf.
设置编译之后的conf文件的路径,默认是用prefix/conf/nginx.conf,推荐使用默认。

–pid-path=path — sets the name of an nginx.pid file that will store the process ID of the main process. After installation, the file name can always be changed in the nginx.conf configuration file using the pid directive. By default the file is named prefix/logs/nginx.pid.
当nginx启动之后,会生成一个nginx.pid的文件,该选项配置nginx.pid文件存放的目录,默认是存放在prefix/logs/nginx.pid,不推荐使用默认路径,因为logs主要用于存放日志文件,这种情况下容易误删除,建议存放在prefix/nginx.pid目录下。

–error-log-path=path — sets the name of the primary error, warnings, and diagnostic file. After installation, the file name can always be changed in the nginx.conf configuration file using the error_log directive. By default the file is named prefix/logs/error.log.
配置错误日志文件的路径,默认路径是prefix/logs/error.log,在配置之后可以在nginx.conf中更改,所以在执行configure的时候可以忽略该选项。

–http-log-path=path — sets the name of the primary request log file of the HTTP server. After installation, the file name can always be changed in the nginx.conf configuration file using the access_log directive. By default the file is named prefix/logs/access.log.
配置服务器访问日志路径,通error日志配置,后期可以在nginx.conf中修改,在执行configure的时候可以忽略该选项。

–build=name — sets an optional nginx build name.
设置nginx的编译的名字,建议忽略该选项

–user=name — sets the name of an unprivileged user whose credentials will be used by worker processes. After installation, the name can always be changed in the nginx.conf configuration file using the user directive. The default user name is nobody.
设置worker线程的linux用户信息,可以控制worker线程的权限,推荐使用默认。

–group=name — sets the name of a group whose credentials will be used by worker processes. After installation, the name can always be changed in the nginx.conf configuration file using the user directive. By default, a group name is set to the name of an unprivileged user.
同上,设置worker线程的linux用户分组,可以控制worker线程的权限,推荐使用默认。

–with-select_module
–without-select_module — enables or disables building a module that allows the server to work with the select() method. This module is built automatically if the platform does not appear to support more appropriate methods such as kqueue, epoll, or /dev/poll.
配置服务器的是否使用select轮询接口(该接口低效且有限制),在编译过程中默认首先使用高效的epoll、poll等机制,所以推荐默认。

–with-poll_module
–without-poll_module — enables or disables building a module that allows the server to work with the poll() method. This module is built automatically if the platform does not appear to support more appropriate methods such as kqueue, epoll, or /dev/poll.
同上,poll接口比select接口更加高效,但是同样有一定的socket数量的限制,推荐使用默认。

–without-http_gzip_module — disables building a module that compresses responses of an HTTP server. The zlib library is required to build and run this module.
是否禁止允许服务器使用zip压缩传输数据,建议使用默认值。

–without-http_rewrite_module — disables building a module that allows an HTTP server to redirect requests and change URI of requests. The PCRE library is required to build and run this module.
是否需要禁止url重写,如果需要url重写,忽略该选项。

–without-http_proxy_module — disables building an HTTP server proxying module.
是否需要禁止代理模块,如果需要代理,忽略该选项。

–with-http_ssl_module — enables building a module that adds the HTTPS protocol support to an HTTP server. This module is not built by default. The OpenSSL library is required to build and run this module.
是否需要支持ssl,如果不需要,忽略该选项。

–with-pcre=path — sets the path to the sources of the PCRE library. The library distribution (version 4.4 — 8.41) needs to be downloaded from the PCRE site and extracted. The rest is done by nginx’s ./configure and make. The library is required for regular expressions support in the location directive and for the ngx_http_rewrite_module module.
配置pcre库的源文件的路径,如果需要url重写或者是location正则匹配,那么需要配置该选项。

–with-pcre-jit — builds the PCRE library with “just-in-time compilation” support (1.1.12, the pcre_jit directive).
建议不配置该选项,视具体需求而定。

–with-zlib=path — sets the path to the sources of the zlib library. The library distribution (version 1.1.3 — 1.2.11) needs to be downloaded from the zlib site and extracted. The rest is done by nginx’s ./configure and make. The library is required for the ngx_http_gzip_module module.
配置zlib压缩库源文件的路径,如果需要数据传输压缩,那么需要配置该选项。

–with-cc-opt=parameters — sets additional parameters that will be added to the CFLAGS variable. When using the system PCRE library under FreeBSD, –with-cc-opt=”-I /usr/local/include” should be specified. If the number of files supported by select() needs to be increased it can also be specified here such as this: –with-cc-opt=”-D FD_SETSIZE=2048”.
该配置是用于扩展gcc编译配置,根据操作系统不同需要进行配置。

–with-ld-opt=parameters — sets additional parameters that will be used during linking. When using the system PCRE library under FreeBSD, –with-ld-opt=”-L /usr/local/lib” should be specified.
如果在FreeBSD操作系统下面配置PCRE,那么需要配置该选项,参考英文描述。
3.5 安装步骤
上面介绍了nginx常用的几个模块,以及相应的的配置选项的含义,以及简单的安装步骤。本节介绍一个简单的nginx的安装的例子,在安装nginx之前需要准备编译环境,即操作系统需要预先安装gcc、openssl、openssl-devel库,环境准备好之后就可以进行nginx的安装了,下面将描述具体的安装步骤:
1、操作系统环境准备,需要安装gcc、openssl依赖,如果是centos系统,安装命令是yum -y install gcc openssl openssl-devel
2、下载nginx-1.12.2.tar.gz、pcre-8.00.tar.gz、zlib-1.2.11.tar.gz。
3、假定当前目录是/yy/test,把刚下载的nginx和pcre拷贝到当前目录,并使用命令解压:tar -xzvf nginx-1.12.2.tar.gz、tar -xzvf pcre-8.00.tar.gz、tar -xzvf zlib-1.2.11.tar.gz。
4、进入到nginx目录:cd nginx-1.12.2,执行configure配置脚本,在执行的时候根据需求携带相关的配置选项,本次安装的配置选项如下:
5、./configure –sbin-path=/usr/local/nginx/nginx –conf-path=/usr/local/nginx/nginx.conf –pid-path=/usr/local/nginx/nginx.pid –with-http_ssl_module –with-pcre=../pcre-8.00 –with-zlib=../zlib-1.2.11
执行完该命令之后,如果没有显示error信息,那么可以在当nginx-1.12.2目录下面看到Makefile文件。
6、上一步完成了nginx的配置之后,接着就可以执行make & make install命令来完成nginx的安装了。
7、上一步完成之后,如果没有提示错误信息,那么可以在/usr/local/nginx目录下面看到相应的编译之后的结果。
8、/usr/local/nginx/sbin/nginx -h获取帮助信息:
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
9、如果上述步骤正确完成,那么可以通过如下命令启动nginx了:
/usr/local/nginx/sbin/nginx
10、在浏览器打开http://ip或者是http://xxx.com,可以看到nginx的默认的欢迎页面了。
nginx——安装和配置详解