Nginx简介:Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
Nginx的安装:
1.依赖安装
# yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
2.下载源码包
www.nginx.org #官网地址,自己去找包
3.源码安装
./configure
--user=www \ #worker进程运行用户
--group=www \ #worker进程运行的组
--prefix=/usr/ \ #Nginx安装的根路径,所有其他的路径都要依赖于改选项
--conf-path=/etc/nginx/nginx.conf \ #如果在命令行没有指定配置文件,那么将会通过这里指定的路径,Nginx将会去那里查找它的配置文件
--sbin-path=/usr/sbin/nginx \ #指定Nginx二进制文件的路径。如果没有指定,那么这个路径会依赖于--prefix选项
--error-log-path=/var/log/nginx/nginx_error.log \ #指定错误文件的路径,Nginx将会往其中写入错误日志文件,除非有其他配置
--http-log-path=/var/log/nginx/nginx_access.log \ #http访问日志的默认路径
--pid-path=/usr/local/nginx/run/nginx.pid \ #指定的文件将会写入Nginx master进程的pid,通常在/var/run下
--lock-path=/usr/local/nginx/lock/nginx \ #共享存储器互斥锁文件的路径
--with-http_ssl_module \ #启用 ngx_http_ssl_module 支持(使支持 https 请求,需已安装openssl)
--with-http_realip_module \ #启用 ngx_http_realip_module 支持(这个模块允许从请求标头更改客户端的 IP 地址值,默认为关)
--with-http_addition_module \ #启用 ngx_http_addition_module 支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求)
--with-http_sub_module \ #启用 ngx_http_sub_module 支持(允许用一些其他文本替换nginx 响应中的一些文本)
--with-http_dav_module \ #启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY 和 MOVE 方法)默认情况下为关闭,需编译开启
--with-http_flv_module \ #启用 ngx_http_flv_module 支持(提供寻求内存使用基于时间的偏移量文件)
--with-http_gzip_static_module \ #启用 ngx_http_gzip_static_module 支持(在线实时压缩输出数据流)
--with-http_stub_status_module \ #启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
--with-http_perl_module \ #Nginx配置能够扩展使用Perl代码。这个选项启用这个模块(然而使用这个模块会降低性能)
--with-mail \ #该选项用于启用mail模块,该模块默认没有被激活
--with-mail_ssl_module \ #为了代理任何一种类型的使用SSL/TLS的mail,激活该模块
--with-pcre \
--http-client-body-temp-path=/var/tmp/nginx/client/ \ #从客户端收到请求后,该选项设置的目录用于作为请求体零食存放的目录,如果WebDAV模块启用,那么推荐设置该路径为同一文件系统上的目录作为最终的目的地
--http-proxy-temp-path=/var/tmp/nginx/proxy \ #在使用代理后,通过该选项设置存放临时文件路径
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \ #设置FastCGI临时文件的目录
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ #设置uWSGI临时文件的目录
--http-scgi-temp-path=/var/tmp/nginx/scgi #设置SCGI临时文件的目录
#以下是完整的复制
./configure --user=www --group=www --prefix=/usr/ --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/sbin/nginx --error-log-path=/var/log/nginx/nginx_error.log --http-log-path=/var/log/nginx/nginx_access.log --pid-path=/usr/local/nginx/run/nginx.pid --lock-path=/usr/local/nginx/lock/nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre --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
4.安装后配置
1.创建用户
useradd -r www -s /bin/false -M
2创建目录
mkdir -p /var/tmp/nginx/client/
3.修改nginx.conf
vi /etc/nginx/nginx.conf
#user nobody >> user www
3.创建启动脚本脚本
vi /etc/rc.d/init.d/nginx
#!/bin/bash
#
# nginx - this script starts and stops the nginx daemin
#
# 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
# pidfile: /usr/local/nginx/logs/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"
lockfile=/var/lock/subsys/nginx
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 $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
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
chmod +x /etc/rc.d/init.d/nginx
把nginx加入chkconfig,并设置开机启动
chkconfig --add nginx
chkconfig nginx on
启动、停止、重新加载的命令如下
service nginx start
service nginx stop
service nginx reload
Ngin -V 查看有关参数
脚本获得方法:
#启动脚本
wget http://www.centos.bz/wp-content/uploads/2011/07/nginx
改里面的相应文件位置即可
5.安装常见问题
1.源码编译可能会出现
./configure: error: perl module ExtUtils::Embed is required
解决方法:
yum -y install perl-ExtUtils-Embed
2.使用nginx启动脚本可能出现nginx: [emerg] getpwnam(“www”) failed
解决方法:
1)
nginx.conf中去掉user nobody注释即可
2)
错误原因是没有www这个用户,加上就好了
useradd www
3.-bash: /etc/init.d/nginx: /bin/sh^M: bad interpreter: 没有那个文件或目录
解决方法:
查看脚本格式 :set ff
会显示 fileformat=doc
改一下 :set ff=unix
保存执行
6.配置文件解读
user www; #运行用户
worker_processes 1; #启动进程,通常设置成和cpu的数量相等
error_log /var/log/nginx/error.log; #全局错误日志
pid /usr/local/nginx/run/nginx.pid; #pid文件
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#日志格式,以下具体介绍变量
$remote_addr 与$http_x_forwarded_for #用以记录客户端的 ip 地址;
$remote_user #用来记录客户端用户名称;
$time_local #用来记录访问时间与时区;
$request #用来记录请求的 url 与 http 协议;
$status #用来记录请求状态;成功是 200;
$body_bytes_s ent #记录发送给客户端文件主体内容大小;
$http_referer #用来记录从那个页面链接访问过来的;
#工作模式及连接数上限
events {
use epoll; #epoll 是多路复用 IO(I/O Multiplexing)中的一种方式,但是仅用于 linux2.6以上内核,可以大大提高 nginx 的性能
worker_connections 1024; #单个后台 worker process 进程的最大并发链接数
# multi_accept on;
}
#设定 http 服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定 mime 类型,类型由 mime.type 文件定义
include /etc/nginx/mime.types;
default_type application/octet-stream;
#设定日志格式
access_log /usr/local/nginx/logs/access.log;
#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,如果用来进行下载等应用磁盘 IO 重负载应用,可设置为 off,以平衡磁盘与网络 I/O 处理速度,降低系统的 uptime.
sendfile on;
#tcp_nopush on;
#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
#开启 gzip 压缩
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
#设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
#设定负载均衡的服务器列表
upstream mysvr {
#weigth 参数表示权值,权值越高被分配到的几率越大
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}
server {
#侦听 80 端口
listen 80;
#定义使用 www.xx.com 访问
server_name www.xxx.com;
#设定本虚拟主机的访问日志
access_log /var/log/nginx/www.xxx.com_access.log access;
#默认请求
location / {
root /var/www/html/; #定义服务器的默认网站根目录位置
index index.php index.html index.htm; #定义首页索引文件的名称
}
# 定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /root;
}
#静态文件,nginx 自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/htdocs;
#过期 30 天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
expires 30d;
}
#PHP 脚本请求全部转发到 FastCGI 处理. 使用 FastCGI 默认配置.
location ~ \.php$ {
root /root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME/home/www/www$fastcgi_script_name;
include fastcgi_params;
}
#设定查看 Nginx 状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
#禁止访问 .htxxx 文件
location ~ /\.ht {
allow host;
deny all;
}
}
}
Nginx简单实战
#基于域名的虚拟主机
server {
listen 80;
server_name www.Daniel.org Daniel.org;
access_log /var/log/nginx/Daniel/Daniel_access.log main;
error_log /var/log/nginx/Daniel/Daniel_error.log crit;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#多域名就多个server段
#基于端口的只需要将listen修改端口即可,server_name也要修改成IP
#别名:
#上面例子中,server_name中有两个域名,这个就是别名
#nginx状态信息配置
server{
listen 80;
server_name status.Daniel.org;
location / {
stub_status on;
access_log off;
}
}
#浏览器访问status.Daniel.org:
Active connections: 3 #正在处理的活动连接数
server accepts handled requests #server表示nginx启动到现在共处理了几个连接,accepts表示nginx启动到现在共成功创建了几次连接,请求丢失数据=(握手数-连接数),可以看出,本次状态显示没有丢失请求,handled requests表示总共处理了几次请求
9 9 192
Reading: 0 Writing: 1 Waiting: 2
#Reading:nginx读取客户端的Header信息数
#Writing:nginx返回给客户端的Header信息数
#Waiting:nginx已经处理完正在等候下一次请求指令的驻留连接,开启keep-alive的情况下,这个值等于active-(reading + writing)
#访问控制权限实战
server {
listen 80;
server_name www.etiantian.org etiantian.org;
access_log /var/log/nginx/etiantian/etiantian_access.log main;
error_log /var/log/nginx/etiantian/etiantian_error.log crit;
location / {
root html/www;
index index.html index.htm;
deny 172.16.50.173; #拒绝172.16.50.173
allow all; #允许全部,意思就是允许除了172.16.50.173的其他全部IP访问
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
server{
listen 80;
server_name status.Daniel.org;
location / {
stub_status on;
access_log off;
allow 172.16.50.173; #只允许172.16.50.173,其他全部拒绝
deny all;
}
}
#也可以用网段表示:172.16.50.0/24