|
一、Nginx
简介:
nginx [engine x]是Igor Sysoev编写的一个高性能的HTTP和反向代理服务器,另外它也可以作为邮件代理服务器。
在大多数情况下都是用来做静态web服务器和反向代理服务器,在作为反向代理服务器的时候,Nginx可以对后端的real server做负载均衡,基于应用层的负载均衡,但是他仅支持一些常见的协议,如:http、mysql、ftp、smtp。
特性:
Nginx是一款面向性能设计的HTTP服务器,相较于Apache、lighttpd具有占有内存少,稳定性高等优势。与旧版本( //http请求包体的最大值;常用于限定客户所能够请求的最大包体;根据请求首部中的Content-Length来检测,以避免无用的传输;上下文:http, server, location
# limit_rate speed;
//限制客户端每秒钟传输的字节数;默认为0,表示没有限制;上下文:http, server, location,
# limit_rate_after time;
//nginx向客户发送响应报文时,如果时长超出了此处指定的时长,则后续的发送过程开始限速;如:下载站上下文:http, server, location,
其他参数设置:对客户端请求的特殊处理
# ignore_invalid_headers on|off //是否忽略不合法的http首部;默认为on; off意味着请求首部中出现不合规的首部将拒绝响应;只能用于server和http;
# log_not_found on|off;
//是否将文件找不到的信息也记录进错误日志中;默认为on;上下文:http, server, location
# resolver address;
//指定nginx使用的dns服务器地址;上下文: http, server, location
# resover_timeout time;
//指定DNS解析超时时长,默认为30s,建议5秒; 上下文:http, server, location
# server_tokens on|off;
//是否在错误页面中显示nginx的版本号;默认on,上下文:http, server, location
其他参数设置:文件操作的优化
# sendfile on|off //是否启用sendfile功能;由内核直接响应用户请求,默认off上下文:http, server, location, if in location
# aio on|off
//是否启用aio功能;完全异步
# open_file_cache max=N [inactive=time]|off
//是否打开文件缓存功能;上下文: http, server, location
max: 缓存条目的最大值;当满了以后将根据LRU(最近最少使用)算法进行置换;
inactive: 某缓存条目在指定时长时没有被访问过时,将自动被删除;默认为60s;
缓存的信息包括: 文件句柄、文件大小和上次修改时间;已经打开的目录结构;没有找到或没有访问权限的信息;
# open_file_cache_errors on|off
//是否缓存文件找不到或没有权限访问等相关信息;默认off;上下文:http, server, location
# open_file_cache_valid time;
//多长时间检查一次缓存中的条目是否超出非活动时长,默认为60s; 上下文:http, server, location
# open_file_cache_min_use #;
//在inactive指定的时长内被访问超此处指定的次数地,才不会被删除;默认1分钟;上下文:http, server, location
http核心模块的内置变量
# $uri: #当前请求的uri,不带参数;参数 ?=12等之类的
# $request_uri: #请求的uri,带完整参数;
# $host: #http请求报文中host首部;如果请求中没有host首部,则以处理此请求的虚拟主机的主机名代替;
# $hostname: #nginx服务运行在的主机的主机名;
# $remote_addr: #客户端IP
# $remote_port: #客户端Port
# $remote_user: #使用用户认证时客户端用户输入的用户名;
# $request_filename: #用户请求中的URI经过本地root或alias转换后映射的本地的文件路径;用的很多
# $request_method: #请求方法
# $server_addr: #服务器地址
# $server_name: #服务器名称
# $server_port: #服务器端口
# $server_protocol: #服务器向客户端发送响应时的协议,如http/1.1, http/1.0
# $scheme: #在请求中使用scheme, 如https://www.magedu.com/中的https;
# $http_HEADER: #匹配请求报文中指定的HEADER,$http_host匹配请求报文中的host首部
# $sent_http_HEADER: #匹配响应报文中指定的HEADER,例如$http_content_type匹配响应报文中的content-type首部;
# $document_root: #当前请求映射到的root配置;
五、Nginx编译安装
1)部署开发环境
# yum -y install "Development tools" "Server Platform Development"
2)解决依赖 pcre-devel openssl-devel
# yum -y install pcre-devel openssl-devel
3) 设置用户
# groupadd -r nginx
# useradd -r -g nginx nginx
4)编译安装nginx-1.4.7
# tar xf nginx-1.4.7.tar.gz
# cd nginx-1.4.7
# ./configure --prefix=/usr --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
# make && make install
5)检测配置文件语法
# /usr/sbin/nginx -t
6) 提供启动脚本
# vim /etc/rc.d/init.d/nginx
内容如下
# nginx - this script 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
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking 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
7)添加到系统服务并开机启动
# chkconfig --add nginx
# chkconfig nginx on
# chkconfig --list nigx
8) 设置nginx配置文件的语法高亮
# mkdir ./vim/syntax -pv
# cd .vim/syntax
# wget http://www.vim.org/scripts/download_script.php?src_id=19394
# cd .vim
# vim filetype.vim 内容如下
au BufRead,BufNewFile /etc/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif
9)启动服务
# service nginx start
# pa aux | grep nginx
六、配置使用Nginx
1.配置虚拟主机
server { listen 80;
server_name www.a.com;
root /web/a
}
2.设置访问控制access模块,只有两个选项:allow和deny
server { listen 80;
server_name www.a.com;
root /web/a;
allow 192.168.0.0/16;
deny all;
}
3.用户认证
location /admin/ { auth_basic "admin";
auth_basic_user_file /etc/nginx/.htpasswd;
root /web/a/;
}
# 使用htpasswd -c -m /path/to/somefile username 创建密码文件
4.下载点设置
#建立下载站点autoindex,只需autoindex为on就行了,在/web/a/down目录下的不可识别的文件都会被以列表的形式列出来 location /down/ {
autoindex on;
root /web/b/;
}
5.SSL示例
server { listen 443;
server_name localhost;
ssl on; #开启ssl引擎
ssl_certificate cert.pem; #证书
ssl_certificate_key cert.key; #私钥文件
ssl_session_timeout 5m; #ssl的超时时间,ssl会话的建立还释放比保持连接更消耗时间,所以如果内存够大并且你网站的粘性比较大,建议时间调长一点
ssl_protocols SSLv2 SSLv3 TLSv1; #支持的协议
ssl_ciphers HIGH:!aNULL:!MD5; #加密算法
ssl_prefer_server_ciphers on; #服务端选择倾向的算法
}
6.stub_status状态页
location /server-status { stub_status on;
}
7.gzip压缩
http { gzip on; #开启压缩功能,可以放在http,server
gzip_http_version 1.0; #压缩后使用那种http协议构建响应报文
gzip_comp_level 2; #压缩级别
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/json; #仅对这些格式的内容进行压缩
gzip_disable msie6; #对IE6不使用压缩机制
}
8.URL rewrite, 地址重写
# rewrite regex replacement [flag]; //上下文 server,location,if
flag参数:
last:停止执行当前这一轮的ngx_http_rewrite_module指令集,然后查找匹配改变后URI的新location;
break:停止执行当前这一轮的ngx_http_rewrite_module指令集;
redirect:在replacement字符串未以“http://”或“https://”开头时,使用返回状态码为302的临时重定向;
permanent:返回状态码为301的永久重定向。
# rewrite_log on|off
//开启或者关闭将ngx_http_rewrite_module模块指令的处理日志以notice级别记录到错误日志中。默认为off;
# return code
//用于结束rewrite规则,并且为客户返回状态码;可以使用的状态码有204, 400, 402-406, 500-504等;
location /down/ { #可以没有down1文件夹
root /web/b;
rewrite ^/down1/(.*\.(jpg|gif|png))$ /image/$1 last;
#访问网址是www.a.com/down1,实际目录是www.a.com/image
}
9.防盗链
#ngx_http_referer_module //此模块允许拦截“Referer”请求头中含有非法值的请求,阻止它们访问站点。 需要注意的是伪造一个有效的“Referer”请求头是相当容易的, 因此这个模块的预期目的不在于彻底地阻止这些非法请求,而是为了阻止由正常浏览器发出的大规模此类请求。 还有一点需要注意,即使正常浏览器发送的合法请求,也可能没有“Referer”请求头。
定义:
# location /photos/ {
# valid_referers none blocked server_names
# *.example.com example.* www.example.org/galleries/
# ~\.google\.;
# if ($invalid_referer) {
//如果是以上指定范围外的,则返回 403错误
# return 403;
# }
# }
该指令的参数:
none # 缺少“Referer”请求头;
blocked # “Referer” 请求头存在,但是它的值被防火墙或者代理服务器删除; 这些值都不以“http://” 或者 “https://”字符串作为开头;
server_names # “Referer” 请求头包含某个虚拟主机名;
|
|