Nginx简介:Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特色是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。javascript
Nginx的安装:php
1.依赖安装css
# yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-develhtml
2.下载源码包java
www.nginx.org #官网地址,本身去找包node
3.源码安装linux
./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.安装后配置nginx
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 查看有关参数
脚本得到方法:centos
#启动脚本 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