前言:
Nginx介绍:
Nginx(engine x)是由俄罗斯人logor sysoev研发的;官方网站:nginx.org;nginx是一个轻量级的高性能的web服务器和反向代理服务器;nginx本身一个处理静态资源的web服务器,但是通过加装fastcgi等模块,可是支持动态资源;可以为IMAP/POP3/SMTP等做代理,是工作在应用层的代理。
一、Nginx功能
1)Nginx的特性
模块化设计、较好的扩展性; 高可靠性:一个master启动异或多个worker,每个worker响应多个请求 低内存消耗:10000个keepalive连接在nginx中仅消耗2.5MB 支持热部署:不停机更新配置文件、更新日志文件、更新服务器程序版本
2)Nginx的基本功能
静态web资源服务器,能够缓存打开的文件描述符 支持http/imap/pop3/smtp的反向代理;支持缓存、负载均衡 支持Fastcgi(fpm) 模块化,非DSO机制,支持过滤器zip压缩,SSI以及图像大小调整 支持SSL
3)Nginx的扩展功能
基于名称和IP的虚拟主机 支持keepalive保持机制 支持平滑升级 定制访问日志,支持使用日志缓冲区提高日志存储性能 支持url rewrite 支持路径别名(root或alias指定) 支持基于IP以及用户的访问控制 支持传输速率限制,并发限制
4)Nginx的基本架构
一个master进程,生成一个或多个worker进程,每个worker相应多个请求 事件驱动:epoll,kqueue,poll,select,rt signals 支持sendfile,sendfile64 支持AIO 支持mmap
5)Nginx模块类型
Nginx core module: nginx的核心模块 Standard HTTP modules:nginx的标准模块 Optional HTTP modules:nginx的可选模块 Mail modules :nginx的邮件模块 3rd party modules:nginx的第三方模块
二、Nignx安装配置
1)安装环境
编译环境:安装Development Tools和Server Platform Development(centos6.6) nginx软件包:nginx-1.6.1.tar.gz pcre-devel:正则表达式依赖包
2)编译安装Nignx
# tar xf nginx-1.6.1.tar.gz -C /usr/src
# cd /usr/src/nginx-1.6.1/
# groupadd nginx
# useradd -r -g nginx nginx
# ./configure \
> --prefix=/usr/local/nginx \
> --conf-path=/etc/nginx/nginx.conf \
> --user=nginx --group=nginx \
> --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\
> --with-http_ssl_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
# make;make install
导出二进制文件:
# vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin/:$PATH
# soucre /etc/profile.d/nginx.sh
配置vim,使其编辑nginx配置文件时语法着色,默认没有
# mkdir .vim
# cp -ra /usr/src/nginx-1.6.1/contrib/vim/* .vim/
# ls .vim
ftdetect indent syntax
启动nginx:
#mkdir -pv /var/tmp/{client,proxy,fcgi}
#nginx
编写启动nginx脚本:
# vim /etc/rc.d/init.d/nginx
#!/bin/sh
#
# 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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
sysconfig="/etc/$prog"
lockfile="/var/lock/subsys/nginx"
pidfile="/var/run//nginx/${prog}.pid"
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f $sysconfig ] && . $sysconfig
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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 -p $pidfile $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest_q || return 6
stop
start
}
reload() {
configtest_q || return 6
echo -n $"Reloading $prog: "
killproc -p $pidfile $prog -HUP
echo
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
configtest_q() {
$nginx -t -q -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
# Upgrade the binary with no downtime.
upgrade() {
local oldbin_pidfile="${pidfile}.oldbin"
configtest_q || return 6
echo -n $"Upgrading $prog: "
killproc -p $pidfile $prog -USR2
retval=$?
sleep 1
if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then
killproc -p $oldbin_pidfile $prog -QUIT
success $"$prog online upgrade"
echo
return 0
else
failure $"$prog online upgrade"
echo
return 1
fi
}
# Tell nginx to reopen logs
reopen_logs() {
configtest_q || return 6
echo -n $"Reopening $prog logs: "
killproc -p $pidfile $prog -USR1
retval=$?
echo
return $retval
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest|reopen_logs)
$1
;;
force-reload|upgrade)
rh_status_q || exit 7
upgrade
;;
reload)
rh_status_q || exit 7
$1
;;
status|status_q)
rh_$1
;;
condrestart|try-restart)
rh_status_q || exit 7
restart
;;
*)
echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
exit 2
esac
#chkconfig --add /etc/rc.d/init.d/nginx
#service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
3)配置文件介绍
主要有两部分;分别是:
main:主体部分
http{}:虚拟主机配置部分
配置指令主要以分号结尾;配置语法: directive value1 [value2 ....] 支持使用变量: 模块内置的变量 自定义变量:set var_name value
主配置段的指令类别: 用于调试和定位问题: (1)daemon [on|off]: 是否以守护进程的方式启动nginx; (2)master_press [on|off]: 是否以master/worker模型来运行nginx; (3)error_log /path/to/error_log level: 指明错误日志文件级别,处于调试目的,可以使用debug级别,但次级别只有在编译nginx时使用了--with-debug选项才有效 ; 正常运行必备的配置: (1)user USERNAME [GROUPNAME]:指定运行worker的用户和用户组;例如 user nginx nginx (2)pid /path/to/nginx.pid : 指定pid文件 (3)worker_rlimit_nofile # : 指定一个worker进程能够打开的最大文件句柄数 (4)worker_rlimit_sigpending # : 指定每个用户能够发往worker信号的数量 优化性能相关的配置: (1)worker_processes # :worker进程的个数,通常是cpu核心数减1 (2)worker_cpu_affinity cpuumask :绑定worker进程至指定的CPU上 (3)timer-resolution t :时间解析度,在x86服务器上可以不用配置 (4)worker_priority NICE :调整nice值(-20,19);nice值越大,越优先分配cpu 事件相关的配置; (1)accept_mutex [on|off] :内部调动用户请求至各worker时的负载均衡锁;启用时表示能够让多个worker轮流的、序列化的响应请求 (2)lock_file /path/to/lock_file :指定锁文件 (3)accept_mutex_delay #ms: 取得负载均衡锁的时间 (4)use [epoll|poll|select|rgsig]:定义使用的事件模型,建议让nginx自动选择 (5)worker_connections #:每个worker进程所能够响应的最大并发请求数
三、nginx的一些基本功能实现
1)基于用户认证:
(1)修改配置文件
#vim /etc/nginx/nginx.conf
server {
listen 80;
server_name www.mylinux.com;
location / {
root /www/html;
index index.html index.htm;
auth_basic "www.magedu.com";
auth_basic_user_file /www/html/.passwd;
}
}
(2)创建文档根目录以及使用httpd-tools中的htpasswd工具创建用户
# mkdir -pv /www/html
# echo "Welcome to mylinx" > /www/html/index.html
# yum -y install httpd-tools
# htpasswd -c -m /www/html/.passwd centos
New password:
Re-type new password:
(3)重新载入配置文件
# nginx -t
# service nginx reload

2)基于ip认证:
#vim /etc/nginx/nginx.conf
server {
listen 80;
server_name www.mylinux.com;
location / {
root /www/html;
index index.html index.htm;
deny 172.16.2.0/24;
allow all;
}
}
#nginx -t
#service nginx reload
3)基于gzip压缩:
# vim /etc/nginx/nginx.conf
server {
listen 80;
server_name www.mylinux.com;
location / {
root /www/html;
gzip on;
gzip_http_version 1.0;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain applicatioin/xml text/css application/x-javascript text/xml
application/xml+rss text/javascript application/javascript application/json;
gzip_disable msie6 safari ;
}
}
#nginx -t
#cp /etc/rc.d/rc.sysinit /www/html/zip.html
#service nginx reload
# ll -h /www/html/zip.html
-rwxr-xr-x. 1 root root 20K Dec 17 07:46 /www/html/zip.html

4)定制响应头部
#vim /etc/nginx/nginx.conf
server {
listen 80;
server_name www.mylinux.com;
location / {
root /www/html;
expires 24h;
add_header magedu mylinux ;
}
}
#nginx -t
#service nginx reload

5)URL重定向
语法格式:
rewrite regex replacement [flages]
flages
last:一旦被当前规则匹配并重写后立即停止检查其他后续的rewrite的规则,而后通过重写后的规则重写发起请求;
break:一旦被当前规则匹配并重写后立即停止后续的其他rewrite的规则,而后由nginx进行后续操作 ;
redirect:返回302临时重定向;
permanent:返回301永久重定向;
例:
# vim /etc/nginx/nginx.conf
server {
listen 80;
server_name www.mylinux.com;
location / {
root /www/html;
}
location /admin {
root /www/html;
rewrite ^/admin/(.*)$ /web/$1;
}
}
# mkdir -pv /www/html/{admin,web}
# echo "www.magedu.com" > /www/html/web/index.html
#nginx -t
#service nginx reload

原创文章,作者:马行空,如若转载,请注明出处:http://www.178linux.com/5417

