Nginx快速上手

17次阅读

共计 8198 个字符,预计需要花费 21 分钟才能阅读完成。

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=<path> – 安装路径,如果没有指定,默认为 /usr/local/nginx。
–sbin-path=<path> – nginx 可执行命令的文件,如果没有指定,默认为 <prefix>/sbin/nginx。
–conf-path=<path> – 在没有使用 - c 参数指定的情况下 nginx.conf 的默认位置,如果没有指定,默认
为 <prefix>/conf/nginx.conf。
–pid-path=<path> – nginx.pid 的路径,如果没有在 nginx.conf 中通过“pid”指令指定,默认为
<prefix>/logs/nginx.pid。
–lock-path=<path> – nginx.lock 文件路径,如果没有指定,默认为 <prefix>/logs/nginx.lock。
–error-log-path=<path> – 当没有在 nginx.conf 中使用“error_log”指令指定时的错误日志位置,
如果没有指定,默认为 <prefix>/logs/error.log。
–http-log-path=<path> – 当没有在 nginx.conf 中使用“access_log”指令指定时的访问日志位置,
如果没有指定,默认为 <prefix>/logs/access.log。
–user=<user> – 当没有在 nginx.conf 中使用“user”指令指定时 nginx 运行的用户,如果没有指定,
默认为“nobody”。
–group=<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 连接的请求实体临时文件设置路径,如果没有指定,默认为 <prefix>/client_body_temp
常见的 Nginx 安装配置选项 -4
–http-proxy-temp-path=PATH – 为 http 代理临时文件设置路径,如果没有指定,默认为 <prefix>/proxy_temp
–http-fastcgi-temp-path=PATH – 为 http fastcgi 临时文件设置路径,如果没有指定,默认为 <prefix>/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_…
还有下面这个网站,也是非常不错的:
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…
Location 区段 -1
Location 区段,通过指定模式来与客户端请求的 URI 相匹配,基本语法如下:
location [=|~|~*|^~|@] pattern{……}
1:没有修饰符表示:必须以指定模式开始,如:
server {
server_name sishuok.com;
location /abc {
……
}
}
那么,如下是对的:
http://sishuok.com/abc
http://sishuok.com/abc?p1=11&…
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&…
如下是错的:
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&…
如下是错的:
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&…
如下是错的:
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

正文完
 0