Nginx快速上手,


Nginx快速上手

根据实际的应用需要,学习要用到的Nginx的知识,以快速上手、理解并掌握

Nginx

一:Nginx简介

包括:Nginx是什么、能干什么、特点

二:Nginx安装和基本使用

包括:源码安装、安装配置选项、基本使用

三:Nginx基本配置

包括:结合示例的配置文件,熟悉Nginx的基本配置

四:学习核心模块、日志模块和事件模块的常用指令

五:学习Http模块的常用配置和指令

Nginx简介

Nginx是什么

Nginx是一款轻量级的Web服务器,也是一款轻量级的反向代理服务器。

Nginx能干什么

Nginx能干的事情很多,这里简要罗列一些:

1:直接支持Rails和PHP的程序

2:作为HTTP反向代理服务器

3:作为负载均衡服务器

4:作为邮件代理服务器

5:帮助实现前端动静分离

……

Nginx特点

高稳定、高性能、资源占用少、功能丰富、模块化结构、支持热部署

Nginx安装

源码安装

演示环境:CentOS6.5

1:需要gcc,系统自带了,没有的话,需要先安装

2:需要pcre,安装的命令示例如下: yum install pcre*

3:需要zlib,安装的命令示例如下:yum install zlib zlib-devel

4:如果需要支持ssl的话,安装OpenSSL,安装的命令示例如下:

yum install openssl openssl-devel

5:上http://nginx.org/去下载源码包,然后进行解压安装,示例如下:

(1)先解压源码包,然后进入到这个包里面

(2)安装命令示例如下:

第一步:./configure --prefix=/usr/common/nginx --withhttp_

stub_status_module --with-http_ssl_module

如果提示确少啥,就加上相应的选项,比如缺少pcre的话,就加上

--with-pcre=/usr/common/temp/pcre-8.34 (当然我们这里是不缺东西的)

第二步:配置后就依次make , make install

常见的Nginx安装配置选项-1

编译参数可能会根据版本的不同进行变化,./configure --help查看编译参数列表,常见的选项如下:

--prefix= - 安装路径,如果没有指定,默认为/usr/local/nginx。

--sbin-path= - nginx可执行命令的文件,如果没有指定,默认为/sbin/nginx。

--conf-path= - 在没有使用-c参数指定的情况下nginx.conf的默认位置,如果没有指定,默认

为/conf/nginx.conf。

--pid-path= - nginx.pid的路径,如果没有在nginx.conf中通过“pid”指令指定,默认为

/logs/nginx.pid。

--lock-path= - nginx.lock文件路径,如果没有指定,默认为/logs/nginx.lock。

--error-log-path= - 当没有在nginx.conf中使用“error_log”指令指定时的错误日志位置,

如果没有指定,默认为/logs/error.log。

--http-log-path= - 当没有在nginx.conf中使用“access_log”指令指定时的访问日志位置,

如果没有指定,默认为/logs/access.log。

--user= - 当没有在nginx.conf中使用“user”指令指定时nginx运行的用户,如果没有指定,

默认为“nobody”。

--group= - 当没有在nginx.conf中使用“user”指令指定时nginx运行的组,如果没有指定,

默认为“nobody”。

--builddir=DIR - 设置构建目录。

--with-rtsig_module - 启用rtsig模块。

常见的Nginx安装配置选项-2

--with-select_module –without-select_module - 如果在configure的时候没有发现kqueue, epoll,

rtsig或/dev/poll其中之一,select模块始终为启用状态。

--with-poll_module –without-poll_module - 如果在configure的时候没有发现kqueue, epoll,

rtsig或/dev/poll其中之一,poll模块始终为启用状态。

--with-http_ssl_module - 启用ngx_http_ssl_module,启用SSL支持并且能够处理HTTPS请求。需要

OpenSSL,在Debian系统中,对应的包为libssl-dev。

--with-http_realip_module - 启用ngx_http_realip_module

--with-http_addition_module - 启用ngx_http_addition_module

--with-http_sub_module - 启用ngx_http_sub_module

--with-http_dav_module - 启用ngx_http_dav_module

--with-http_flv_module - 启用ngx_http_flv_module

--with-http_stub_status_module - 启用”server status”(服务状态)页

--without-http_charset_module - 禁用ngx_http_charset_module

--without-http_gzip_module - 禁用ngx_http_gzip_module,如果启用,需要zlib包。

--without-http_ssi_module - 禁用ngx_http_ssi_module

--without-http_userid_module - 禁用ngx_http_userid_module

--without-http_access_module - 禁用ngx_http_access_module

--without-http_auth_basic_module - 禁用ngx_http_auth_basic_module

常见的Nginx安装配置选项-3

--without-http_autoindex_module - 禁用ngx_http_autoindex_module

--without-http_geo_module - 禁用ngx_http_geo_module

--without-http_map_module - 禁用ngx_http_map_module

--without-http_referer_module - 禁用ngx_http_referer_module

--without-http_rewrite_module - 禁用ngx_http_rewrite_module。如果启用,需要PCRE包。

--without-http_proxy_module - 禁用ngx_http_proxy_module

--without-http_fastcgi_module - 禁用ngx_http_fastcgi_module

--without-http_memcached_module - 禁用ngx_http_memcached_module

--without-http_limit_zone_module - 禁用ngx_http_limit_zone_module

--without-http_empty_gif_module - 禁用ngx_http_empty_gif_module

--without-http_browser_module - 禁用ngx_http_browser_module

--without-http_upstream_ip_hash_module - 禁用ngx_http_upstream_ip_hash_module

--with-http_perl_module - 启用ngx_http_perl_module

--with-perl_modules_path=PATH - 为perl模块设置路径

--with-perl=PATH - 为perl库设置路径

--http-client-body-temp-path=PATH - 为http连接的请求实体临时文件设置路径,如果没有指定,默认为/client_body_temp

常见的Nginx安装配置选项-4

--http-proxy-temp-path=PATH - 为http代理临时文件设置路径,如果没有指定,默认为/proxy_temp

--http-fastcgi-temp-path=PATH - 为http fastcgi临时文件设置路径,如果没有指定,默认为/fastcgi_temp

--without-http - 禁用HTTP服务

--with-mail - 启用IMAP4/POP3/SMTP代理模块

--with-mail_ssl_module - 启用ngx_mail_ssl_module

--with-cc=PATH - 设置C编译器路径

--with-cpp=PATH - 设置C预处理器路径

--with-cc-opt=OPTIONS - 变量CFLAGS中附加的参数,用于FreeBSD中的PCRE库,同样需要指定–withcc-

opt=”-I /usr/local/include”,如果我们使用select()函数则需要同时增加文件描述符数量,可

以通过–with-cc-opt=”-D FD_SETSIZE=2048”指定。

--with-ld-opt=OPTIONS - 通过连接器的附加参数,用于FreeBSD中的PCRE库,同样需要指定–withld-

opt=”-L /usr/local/lib”。

--with-cpu-opt=CPU - 指定编译的CPU,可用的值为: pentium, pentiumpro, pentium3, pentium4,

athlon, opteron, amd64, sparc32, sparc64, ppc64

--without-pcre - 禁用PCRE库文件,同时将禁用HTTP rewrite 模块,如果要在”location”指令中使

用正则表达式,同样需要PCRE库。

常见的Nginx安装配置选项-5

--with-pcre=DIR - 设置PCRE库源文件路径。

--with-pcre-opt=OPTIONS - 在编译时为PCRE设置附加参数。

--with-md5=DIR - 设置md5库源文件路径。

--with-md5-opt=OPTIONS - 在编译时为md5设置附加参数。

--with-md5-asm - 使用md5汇编源。

--with-sha1=DIR - 设置sha1库源文件路径。

--with-sha1-opt=OPTIONS - 在编译时为sha1设置附加参数。

--with-sha1-asm - 使用sha1汇编源。

--with-zlib=DIR - 设置zlib库源文件路径。

--with-zlib-opt=OPTIONS - 在编译时为zlib设置附加参数。

--with-zlib-asm=CPU - 为指定的CPU使用zlib汇编源进行优化,可用值为: pentium, pentiumpro。

--with-openssl=DIR - 设置openssl库源文件路径。

--with-openssl-opt=OPTIONS - 在编译时为openssl设置附加参数。

--with-debug - 启用debug记录。

--add-module=PATH - 增加一个在PATH中的第三方模块。

Nginx的基本运行

测试配置文件:

安装路径下的/nginx/sbin/nginx -t

启动:

安装路径下的/nginx/sbin/nginx

停止

安装路径下的/nginx/sbin/nginx -s stop

或者是: nginx -s quit

重启

安装路径下的/nginx/sbin/nginx -s reload

查看进程

ps -ef |grep nginx

安装过后,如果从外面访问不了,多半是被防火墙挡住了,可以关闭掉防火墙:

/sbin/service iptables stop

Nginx的基本配置-1

默认启动Nginx时,使用的配置文件是: 安装路径/conf/nginx.conf 文件

可以在启动nginx的时候,通过-c来指定要读取的配置文件

常见的配置文件有如下几个:

nginx.conf:应用程序的基本配置文件

mime.types:MIME类型关联的扩展文件

fastcgi.conf:与fastcgi相关的配置

proxy.conf:与proxy相关的配置

sites.conf:配置Nginx提供的网站,包括虚拟主机

Nginx的进程结构

启动Nginx的时候,会启动一个Master进程,这个进程不处理任何客户端的

请求,主要用来产生worker进程,一个worker进程用来处理一个request。

Nginx模块分为:核心模块、事件模块、标准Http模块、可选Http模块、邮件模

块、第三方模块和补丁等

Nginx的基本配置-2

Nginx基本模块:所谓基本模块,指的是Nginx默认的功能模块,它们提供的指

令,允许你使用定义Nginx基本功能的变量,在编译的时候不能被禁用,包括:

核心模块:基本功能和指令,如进程管理和安全

事件模块:在Nginx内配置网络使用的能力

配置模块:提供包含机制

常见的核心模块指令,大部分都是放置在配置文件的顶部

具体的指令,请参看nginx的官方文档,非常详细,参见:

http://nginx.org/en/docs/ngx_core_module.html

还有下面这个网站,也是非常不错的:

http://www.howtocn.org/nginx:directiveindex

常见的events模块指令,大部分都是放置在配置文件的顶部

具体的指令,在上面那个文档里面,命令的context为events的就是events

模块指令,只能在events里面使用

Nginx常用的核心模块指令

核心模块指令,重点看看:error_log、include、pid、user、worker_cpu_affinity、

worker_processes

error_log

日志有6个级别:debug|info|notice|warn|error|crit

Nginx支持将不同的虚拟主机的日志记录在不同的地方,如下示例:

http{

error_log logs/http_error.log error;

server{

server_name one;

access_log logs/one_access.log;

error_log logs/one_error.log error;

}

server{

server_name two;

access_log logs/two_access.log;

error_log logs/two_error.log error;

}

}

注意:error_log off不是禁用日志,而是创建一个名为off的日志,要禁用日志,可以这么写:error_log

/dev/null crit;

Nginx常用的日志模块、事件模块指令

日志模块指令,几个都看看

事件模块指令,重点看看:use和worker_connections

Nginx的HTTP基本配置

Nginx的HTTP配置主要包括三个区块,结构如下:

http { //这个是协议级别

include mime.types;

default_type application/octet-stream;

keepalive_timeout 65;

gzip on;

server { //这个是服务器级别

listen 80;

server_name localhost;

location / { //这个是请求级别

root html;

index index.html index.htm;

}

}

}

Nginx的HTTP核心模块,包括大量的指令和变量,大都很重要,具体参见:

http://nginx.org/en/docs/http/ngx_http_core_module.html

Location区段-1

Location区段,通过指定模式来与客户端请求的URI相匹配,基本语法如下:

location [=|~|~*|^~|@] pattern{……}

1:没有修饰符表示:必须以指定模式开始,如:

server {

server_name sishuok.com;

location /abc {

……

}

}

那么,如下是对的:

http://sishuok.com/abc

http://sishuok.com/abc?p1=11&p2=22

http://sishuok.com/abc/

http://sishuok.com/abcde

Location区段-2

2:= 表示:必须与指定的模式精确匹配,如:

server {

server_name sishuok.com;

location = /abc {

……

}

}

那么,如下是对的:

http://sishuok.com/abc

http://sishuok.com/abc?p1=11&p2=22

如下是错的:

http://sishuok.com/abc/

http://sishuok.com/abcde

Location区段-3

3:~ 表示:指定的正则表达式要区分大小写,如:

server {

server_name sishuok.com;

location ~ ^/abc$ {

……

}

}

那么,如下是对的:

http://sishuok.com/abc

http://sishuok.com/abc?p1=11&p2=22

如下是错的:

http://sishuok.com/ABC

http://sishuok.com/abc/

http://sishuok.com/abcde

Location区段-4

4:~* 表示:指定的正则表达式不区分大小写,如:

server {

server_name sishuok.com;

location ~* ^/abc$ {

……

}

}

那么,如下是对的:

http://sishuok.com/abc

http://sishuok.com/ABC

http://sishuok.com/abc?p1=11&p2=22

如下是错的:

http://sishuok.com/abc/

http://sishuok.com/abcde

Location区段-5

5:^~ 类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,

那么就停止搜索其他模式了。

6:@ :定义命名location区段,这些区段客户段不能访问,只可以由内部产生的请

求来访问,如try_files或error_page等

查找顺序和优先级

1:带有“=“的精确匹配优先

2:没有修饰符的精确匹配

3:正则表达式按照他们在配置文件中定义的顺序

4:带有“^~”修饰符的,开头匹配

5:带有“~” 或“~*” 修饰符的,如果正则表达式与URI匹配

6:没有修饰符的,如果指定字符串与URI开头匹配

Location区段匹配示例

location = / {

只匹配/ 的查询.

[ configuration A ]

}

location / {

匹配任何以/ 开始的查询,但是正则表达式与一些较长的字符串将被首先匹配。

[ configuration B ]

}

location ^~ /images/ {

匹配任何以/images/ 开始的查询并且停止搜索,不检查正则表达式。

[ configuration C ]

}

location ~* .(gif|jpg|jpeg)$ {

匹配任何以gif, jpg, or jpeg结尾的文件,但是所有/images/ 目录的请求将在Configuration C中处

理。

[ configuration D ]

}

各请求的处理如下例:

■/ → configuration A

■/documents/document.html → configuration B

■/images/1.gif → configuration C

■/documents/1.jpg → configuration D

相关内容