|
一、源码包准备,用的是1.6.2版本,基于centos 6.6
二、编译
需要的开发包
yuminstall gcc openssl-devel pcre-devel zlib-devel
创建nginx用户与组,并指明默认shell、不创建家目录
[root@localhostnginx-1.6.2]# groupadd -r nginx
[root@localhostnginx-1.6.2]# useradd -r -g nginx -s /bin/false -M nginx
[root@localhost~]# tar xf nginx-1.6.2.tar.gz
[root@localhost~]# cd nginx-1.6.2
[root@localhostnginx-1.6.2]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx--conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid--lock-path=/var/lock/nginx.lock --user=nginx --group=nginx--with-http_ssl_module --with-http_flv_module --with-http_stub_status_module--with-http_gzip_static_module--http-client-body-temp-path=/var/tmp/nginx/client--http-proxy-temp-path=/var/tmp/nginx/proxy/--http-fastcgi-temp-path=/var/tmp/nginx/fcgi--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi--http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
[root@localhostnginx-1.6.2]# make
[root@localhostnginx-1.6.2]# make install
各选项的意义:
--prefix= nginx的安装路径,如果没有指定,则为/usr/local/nginx
--sbin-path= nginx的执行文件路径,只能用来安装,如果没有指定则为安装路径下的/sbin/nginx
--conf-path= 没有使用-c 参数指定时nginx的配置文件路径,默认为安装路径下的/conf/nginx/conf
--pid-path= 在nginx.conf中没有设置情形下,nginx.pid文件路径,默认为安装路径下的/logs/nginx.pid
--error-log-path= 在nginx.conf中没有设置的情形下,错误日志的存放路径,默认为安装路径下的/logs/error.log
--http-log-path= 在nginx中没有设置的情形下,访问日志的存放路径,默认为安装路径下的/logs/access.log
--user= 在nginx.conf中没有设置的情况下,设置以谁的身份运行nginx,默认为nobody
--group= 在nginx.conf中没有设置的情况下,设置以哪个用户组的身份运行nginx,默认为nobody
--with-http_ssl_module 启用ssl模块
--with-http_flv_module 启用glv模块
--http-client-body-temp-path=PATH 设置客户端请求临时文件存放目录,默认为安装路径下的/client_body_temp
--http-proxy-temp-path=PATH 设置http代理的临时文件目录,默认为安装路径下的/proxy_temp
--http-fastcgi-temp-path=PATH 设置fastcgi临时文件目录,默认为安装路径下的/fastcgi_temp
--lock-path= 设置nginx的锁文件路径,默认为安装路径下的/nginx/lock
三、为nginx添加服务脚本
编辑/etc/rc.d/init.d/nginx,并添加一下内容
#!/bin/sh
#
# nginx - thisscript starts and stops the nginx daemon
#
#chkconfig: - 85 15
#description: Nginx is an HTTP(S) server,HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname:nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Sourcefunction library.
./etc/rc.d/init.d/functions
# Sourcenetworking configuration.
./etc/sysconfig/network
# Check thatnetworking is up.
["$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename$nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f/etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep"configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'`]; then
value=`echo $opt | cut -d"=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating"$value
mkdir -p $value && chown-R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f$lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case"$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*) echo $"Usage: $0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
为脚本赋予执行权限:
[root@localhostnginx-1.6.2]# chmod +x /etc/rc.d/init.d/nginx 添加至服务管理列表,并能开机启动
[root@localhostnginx-1.6.2]# chkconfig --add nginx
[root@localhostnginx-1.6.2]# chkconfig nginx on
[root@localhostnginx-1.6.2]# chkconfig --list nginx
nginx 0:off1:off2:on3:on4:on5:on6:off
四、启动测试
已成功启动
五、nginx的工作模式
nginx启动时会生成一个master主进程,主进程会启动
多个worker线程,负责处理用户请求,master负责监控worker进程数,是否启动的够数目,是否工作正常等,以及管理其他负责缓存的进
程,master以系统用户运行,而worker线程是以普通进程运行,本身worker只能处理最简单的http请求,而其他的高级功能都是有模块处理
完成的,这些模块都有各自的作用,以流水线的方式处理用户的请求,worker负责分配哪些模块去处理用户的请求,master负责装载配置文件,如果配
置文件更改,master会重新载入,已经启动的worker会继续使用旧的配置文件,当worker请求处理完毕时,会被杀死,master会以新的配
置文件重新启动worker线程,此时新的配置文件便会生效,从而不会影响nginx运行
六、nginx配置文件
nginx的配置文件的自定义程度非常高
main配置段
http{
}
配置指令要以分号结尾,语法格式
directive value1[value2,...];
指令值(后可跟多个值);
支持使用变量
模块内置变量
自定义变量
set var_name value
主配置段的指令的类别
用于调试、定位问题
用于正常运行必备的配置
优化性能的配置
事件相关的配置
正常运行的必备配置:
1、user USERNAME [GROUPNAME]
指定运行worker进程的用户和组
2、pid/path/to/somewhere
指定pid文件路径
3、worker_rlimit_nofile #:
一个worker文件所能够打开的最大文件句柄数
4、worker_rlimit_sigpending:用于设定每个用户能够发往worker进程的信号的数量
优化性能相关的配置
1、worker_processes # ;启动的worker线程的个数,通常为物理cpu核心的个数或-1,如图当设置此值为2则有两个worker进程,而且从此图可以看出master是以root用户身份运行的,而worker进程是以nginx运行的。
2、worker_cpu_affinity cpumask...
绑定worker进程至指定的cpu上
cpumask
0001
0010
0100
1000
worker_cpu_affinity 00000001 00000010 00000100
3、worker_priority nice;
以指定nice至运行程序 -20到19
ps axo pid,command,nice命令可查看
事件相关的配置
1、access_mutex(互斥)[on|off],是否打开nginx的负载均衡锁
内部调用用户请求至各worker时用的负载均衡锁;打开时表示能够让多个worker轮流的序列化的响应新请求
2、lock_file (和脚本中的文件无关)
3、accept_mutex_delay #ms;
一个worker进程等待锁文件
4、use [epoll|rgsig|select|poll]
指明使用那个模型
5、worker_connections #
每个woreker进程所能够响应的最大并发请求数
用户用于调试、定位问题
1、daemon [off|on]
是否以守护进程方式启动nginx,正常环境中为on
2、master_process [on|off]
是否以master/worker模型来运行nginx
3、error_log /path/to/somewhere level;
错误日志文件及其级别;处于调试的目的,可以使用debug级别,但此级别只有在编译nginx时使用了--with-debug选项才有效
七、nginx的模块化配置
ginx_http_core_module
配置框架
http{
upstream{
....
}
server{
Listenip:port
# 虚拟主机
location/url{
if ...{
....
}
root"本地文件路径".....
}
}
server{
#虚拟主机
}
}
虚拟主机相关的配置
1,server{ }
定义一个虚拟主机
2、listen ip:port
监听的端口;
backlog=number
tcp协议backlog队列的大小。默认为-1,表示不启用
rcvbuf=size:设置监听的句柄的SO_RECVBUF参数
sendbuf=size设置监听的句柄的SO_SENTBUF参数
listen 172.16.100.8:8080
3、server_name
后可跟多个主机名,名称可以使用通配符和正则表达式(~)
1、先做精确匹配:www.stu30.com,只有访问www.stu30.com才会匹配此主机
2、左侧通配符匹配:*.magedu.com,没有被精确匹配匹配到,则会
3、右侧通配符匹配:www.*;
4、正则表达式匹配:~^.*\.magedu\.com$
4、location [=|~|~*|^~] /uri { ... }
loclation @name
功能:允许根据用户请求的URL来匹配定义的各location,匹配到时,此请求将被响应的location块中的配置所处理;
server {
location /images { }
location /bbs { }
location / { }
}
=:精确匹配检查
~:正则表达式模式匹配,区分字符大小写
~*:正则表达式匹配,不区分字符大小写
^~:URL前半部分匹配,不检查正则表达式
匹配优先级:(=)>(^~) >(~ ~*)>(不带任何符号)
5、root
设置web的资源路径映射,用来指明请求的URL所对应的文档的目录路径
location /image/{
root "/web/imgs";
}
访问.com/image 对应的文件路径为/web/imgs/image
6、alias path
用于location配置段。定义路径别名
location /image/{
alias "/web/imgs";
}
访问.com/image 对应的文件路径为/web/imgs
7、index file;
默认主页面
index index.html:
8、error_page code [...] [=code]URI
根据http的状态码重定向错误页面
error_page 404 /404.html
=code,以指定的响应码进行响应
=:以新资源的响应码为响应码
网络连接相关的配置
1、keepalive_timeouttime
保持连接的超时时长,默认为75秒
2、keepalive——requests #
在一次保持连接上允许承载的最大资源请求数
3、keepalive_disable[msie6|sarari|none]
为指定类型的浏览器禁用保持连接
4、tcp_nodelayon|off
对保持连接是否使用TCP_NODELAY选项
5、client_header_timetime ;
读取http请求报文首部的超时时长
6、client_body_timeouttime
读取http请求报文body部分的超时时长
7、send_timeouttime
发送响应报文的超时时长
对客户端请求进行限制
1、limit_except METHOD {...}
指定对范围之外的其他方法的访问控制;
limit_exceptGET {
allow 172.16.0.0/16;
deny all}
2、client_body_max_size> 限制请求报文中的body部分的上限,通过检测请求报文首部中的Content Length来判定
3、limit_rate speed
限制客户端每秒钟传输的字节数,默认为0表示不限制
实现对内存和磁盘资源进行分配
1、client_body_in_file_only on|clean|off
http的请求报文的body是否可暂时存储在磁盘
on 允许,即便请求结束也不会删除暂存内容
clean会删除
off不允许暂存
2、client_body_buffer-size> 暂存大小
3、client_body_temp_path DIR [level1[level2[level3]]]
4、client_header_buffer_size> 请求报文首部分配的内存空间大小
MIME类型相关的配置
1、types
如何判断某种扩展名类型
定义MIME types至文件的扩展名
types{
text/html .html;
image/jieg .jpg;
}
2、defaults_type MIME=TYPE
文件操作优化相关的配置
1、sendfile onoff
2、aio:是否启用异步IO
3、directio> 是否使用O_DIRRCT选项去请求读取文件,与sendfile功能互斥
4、open_file_cache max =N[inactive=time]| off
nginx可以缓存一下三种信息
1.文件句柄、文件大小和最近一次修改时间
2、打开目录的目录结构
3、没有找到的或者没有权限操作的文件的相关信息
max=N 表示可缓存的最大条目上限,一旦达到上限,则会使用LRU 从缓存中删除最近最少使用的条目
inactive=time 在inactive指定的时长内没有被访问过的缓存条目就会淘汰
5、open_fule_errors on|off
是否缓存在文件缓存打开文件时出现找不到路径,没有全乡等的错误信息
6、open_file_cache_min
基于IP的访问控制
同样可以使用172.16.0.0/16等网络地址,all表示拒绝所有,请求从上往下匹配,匹配到则执行拒绝或允许,没 匹配到则进入下一条开始匹配
此时重启服务便会被拒绝了
而允许的则可以访问
基于用户的访问控制
[root@localhost~]# htpasswd -c -m /etc/nginx/.htpasswd stu30
New password:
Re-type new password:
Adding password for user stu30
[root@localhost ~]# mkdir/nginx/htdocs/admin -pv
mkdir: created directory `/nginx'
mkdir: created directory `/nginx/htdocs'
mkdir: created directory`/nginx/htdocs/admin'
[root@localhost ~]# !ser
service nginx restart
nginx: the configuration file/etc/nginx/nginx.conf syntax is ok
nginx: configuration file/etc/nginx/nginx.conf test is successful
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
访问结果:需要输入密码,才能访问
基于gzip实现响应报文压缩
定制请求头部信息
测试:
URL重写
情景假设:论坛路径由bbs换为forum,如果不进行URL重写,会无法访问,通过URL重写将bbs换为forum即可正常访问
首先创建bbs目录
[root@localhost~]# mkdir /nginx/htdocs/bbs 创建主页文件
[root@localhost~]# vim /nginx/htdocs/bbs/index.html
BBS
访问测试:
将bbs目录修改为forum
[root@localhost~]# mv /nginx/htdocs/bbs/ /nginx/htdocs/forum 访问测试:
访问测试
而URL重写还有标识位
没有添加标识位的请求结果,服务器会自己将每次请求进行检查,最终返回客户端最终结果
添加标识位redirect
此时服务器会先返回一个302的临时重定向,客户端自己再次发送请求重定向之后的结果
|
|
|