发布于 2014-11-10 09:20:44 | 1933 次阅读 | 评论: 0 | 来源: 网友投递
Nginx WEB服务器
Nginx 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。
本文为大家讲解的是centos 下nginx安装部署及配置方法 ,感兴趣的同学参考下。
系统:centos6.5 x64
web server:nginx
perl:perl-5.16.3.tar.gz
zlib:zlib-1.2.8.tar.xz
openssl:openssl-1.0.0o.tar.gz
pcre:pcre-8.35.tar.gz
nginx:nginx-1.6.2.tar.gz
相关包见附件
一、安装perl模块
二、安装nginx、配置文件详解
三、nginx扩展功能
1.1、解压perl,并安装;
[nginx@master ~]$pwd/opt/nginx[nginx@master ~]$ wget http://www.cpan.org/src/5.0/perl-5.16.3.tar.gz[nginx@master ~]$tarzxvf perl-5.16.3.tar.gz[nginx@master ~]$cdperl-5.16.3[nginx@master perl-5.16.3]$./Configure--help[nginx@master perl-5.16.3]$./Configure-des -Dprefix=$HOME/perl5.16 -Dusethreads -Duse64bitint -Duse64bitall -Uversiononly[nginx@master perl-5.16.3]$make[nginx@master perl-5.16.3]$makeinstall
参数解释:
-Dprefix=/opt/perl5choose your destination-Dusethreads use thread support-Duse64bitint use 64bit integers-Duse64bitall use 64bit integers and pointers
1.2、编辑用户环境变量
[nginx@master perl-5.16.3]$ vim ~/.bash_profilePERL5=$HOME/perl5.16/binPATH=$PERL5:$PATH
exportPATH
2.1、安装nginx
[nginx@master ~]$tarxf openssl-1.0.0o.tar.gz zlib-1.2.8.tar.xz pcre-8.35.tar.gz nginx-1.6.2.tar.gz[nginx@master ~]$cdnginx-1.6.2[nginx@master nginx-1.6.2]$ ./configure--prefix=$HOME/nginx--with-http_gunzip_module --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-openssl=../openssl-1.0.0o --with-pcre --with-pcre=../pcre-8.35 --with-zlib=../zlib-1.2.8[nginx@master nginx-1.6.2]$make[nginx@master nginx-1.6.2]$makeinstall
编译参数解释:
–prefix= 指向安装目录–sbin-path 指向(执行)程序文件(nginx)–conf-path= 指向配置文件(nginx.conf)–error-log-path= 指向错误日志目录–pid-path= 指向pid文件(nginx.pid)–lock-path= 指向lock文件(nginx.lock)(安装文件锁定,防止安装文件被别人利用,或自己误操作。)–user= 指定程序运行时的非特权用户–group= 指定程序运行时的非特权用户组–builddir= 指向编译目录–with-rtsig_module 启用rtsig模块支持(实时信号)–with-select_module 启用select模块支持(一种轮询模式,不推荐在高载环境下使用)禁用:–without-select_module–with-poll_module 启用poll模块支持(功能与select相同,与select特性相同,为一种轮询模式,不推荐在高载环境下使用)–with-file-aio 启用fileaio支持(一种APL文件传输格式)–with-ipv6 启用ipv6支持–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_xslt_module 启用ngx_http_xslt_module支持(过滤转换XML请求)–with-http_image_filter_module 启用ngx_http_image_filter_module支持(传输JPEG/GIF/PNG图片的一个过滤器)(默认为不启用。gd库要用到)–with-http_geoip_module 启用ngx_http_geoip_module支持(该模块创建基于与MaxMind GeoIP二进制文件相配的客户端IP地址的ngx_http_geoip_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_random_index_module 启用ngx_http_random_index_module支持(从目录中随机挑选一个目录索引)–with-http_secure_link_module 启用ngx_http_secure_link_module支持(计算和检查要求所需的安全链接网址)–with-http_degradation_module 启用ngx_http_degradation_module支持(允许在内存不足的情况下返回204或444码)–with-http_stub_status_module 启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)–without-http_charset_module 禁用ngx_http_charset_module支持(重新编码web页面,但只能是一个方向–服务器端到客户端,并且只有一个字节的编码可以被重新编码)–without-http_gzip_module 禁用ngx_http_gzip_module支持(该模块同-with-http_gzip_static_module功能一样)–without-http_ssi_module 禁用ngx_http_ssi_module支持(该模块提供了一个在输入端处理处理服务器包含文件(SSI)的过滤器,目前支持SSI命令的列表是不完整的)–without-http_userid_module 禁用ngx_http_userid_module支持(该模块用来处理用来确定客户端后续请求的cookies)–without-http_access_module 禁用ngx_http_access_module支持(该模块提供了一个简单的基于主机的访问控制。允许/拒绝基于ip地址)–without-http_auth_basic_module禁用ngx_http_auth_basic_module(该模块是可以使用用户名和密码基于http基本认证方法来保护你的站点或其部分内容)–without-http_autoindex_module 禁用disable ngx_http_autoindex_module支持(该模块用于自动生成目录列表,只在ngx_http_index_module模块未找到索引文件时发出请求。)–without-http_geo_module 禁用ngx_http_geo_module支持(创建一些变量,其值依赖于客户端的IP地址)–without-http_map_module 禁用ngx_http_map_module支持(使用任意的键/值对设置配置变量)–without-http_split_clients_module 禁用ngx_http_split_clients_module支持(该模块用来基于某些条件划分用户。条件如:ip地址、报头、cookies等等)–without-http_referer_module 禁用disable ngx_http_referer_module支持(该模块用来过滤请求,拒绝报头中Referer值不正确的请求)–without-http_rewrite_module禁用ngx_http_rewrite_module支持(该模块允许使用正则表达式改变URI,并且根据变量来转向以及选择配置。如果在server级别设置该选项,那么他们将在location之前生效。如果在location还有更进一步的重写规则,location部分的规则依然会被执行。如果这个URI重写是因为location部分的规则造成的,那么 location部分会再次被执行作为新的URI。这个循环会执行10次,然后Nginx会返回一个500错误。)–without-http_proxy_module 禁用ngx_http_proxy_module支持(有关代理服务器)–without-http_fastcgi_module 禁用ngx_http_fastcgi_module支持(该模块允许Nginx 与FastCGI 进程交互,并通过传递参数来控制FastCGI 进程工作。 )FastCGI一个常驻型的公共网关接口。–without-http_uwsgi_module 禁用ngx_http_uwsgi_module支持(该模块用来医用uwsgi协议,uWSGI服务器相关)–without-http_scgi_module 禁用ngx_http_scgi_module支持(该模块用来启用SCGI协议支持,SCGI协议是CGI协议的替代。它是一种应用程序与HTTP服务接口标准。它有些像FastCGI但他的设计 更容易实现。)–without-http_memcached_module 禁用ngx_http_memcached_module支持(该模块用来提供简单的缓存,以提高系统效率)-without-http_limit_zone_module 禁用ngx_http_limit_zone_module支持(该模块可以针对条件,进行会话的并发连接数控制)–without-http_limit_req_module 禁用ngx_http_limit_req_module支持(该模块允许你对于一个地址进行请求数量的限制用一个给定的session或一个特定的事件)–without-http_empty_gif_module 禁用ngx_http_empty_gif_module支持(该模块在内存中常驻了一个1*1的透明GIF图像,可以被非常快速的调用)–without-http_browser_module禁用ngx_http_browser_module支持(该模块用来创建依赖于请求报头的值。如果浏览器为modern,则$modern_browser等于modern_browser_value指令分配的值;如果浏览器为old,则$ancient_browser等于 ancient_browser_value指令分配的值;如果浏览器为MSIE中的任意版本,则 $msie等于1)–without-http_upstream_ip_hash_module 禁用ngx_http_upstream_ip_hash_module支持(该模块用于简单的负载均衡)–with-http_perl_module 启用ngx_http_perl_module支持(该模块使nginx可以直接使用perl或通过ssi调用perl)–with-perl_modules_path= 设定perl模块路径–with-perl= 设定perl库文件路径–http-log-path= 设定access log路径–http-client-body-temp-path= 设定http客户端请求临时文件路径–http-proxy-temp-path= 设定http代理临时文件路径–http-fastcgi-temp-path= 设定http fastcgi临时文件路径–http-uwsgi-temp-path= 设定http uwsgi临时文件路径–http-scgi-temp-path= 设定http scgi临时文件路径-without-http 禁用http server功能–without-http-cache 禁用http cache功能–with-mail 启用POP3/IMAP4/SMTP代理模块支持–with-mail_ssl_module 启用ngx_mail_ssl_module支持–without-mail_pop3_module禁用pop3协议(POP3即邮局协议的第3个版本,它是规定个人计算机如何连接到互联网上的邮件服务器进行收发邮件的协议。是因特网电子邮件的第一个离线协议标准,POP3协议允许用户从服务器上把邮件存储到本地主机上,同时根据客户端的操作删除或保存在邮件服务器上的邮件。POP3协议是TCP/IP协议族中的一员,主要用于 支持使用客户端远程管理在服务器上的电子邮件)–without-mail_imap_module禁用imap协议(一种邮件获取协议。它的主要作用是邮件客户端可以通过这种协议从邮件服务器上获取邮件的信息,下载邮件等。IMAP协议运行在TCP/IP协议之上,使用的端口是143。它与POP3协议的主要区别是用户可以不用把所有的邮件全部下载,可以通过客户端直接对服务器上的邮件进行操作。)–without-mail_smtp_module禁用smtp协议(SMTP即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。SMTP协议属于TCP/IP协议族,它帮助每台计算机在发送或中转信件时找到下一个目的地。)–with-google_perftools_module 启用ngx_google_perftools_module支持(调试用,剖析程序性能瓶颈)–with-cpp_test_module 启用ngx_cpp_test_module支持–add-module= 启用外部模块支持–with-cc= 指向C编译器路径–with-cpp= 指向C预处理路径–with-cc-opt=设置C编译器参数(PCRE库,需要指定–with-cc-opt=”-I/usr/local/include”,如果使用select()函数则需要同时增加文件描述符数量,可以通过–with-cc- opt=”-DFD_SETSIZE=2048”指定。)–with-ld-opt= 设置连接文件参数。(PCRE库,需要指定–with-ld-opt=”-L/usr/local/lib”。)–with-cpu-opt= 指定编译的CPU,可用的值为: pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64–without-pcre 禁用pcre库–with-pcre 启用pcre库–with-pcre= 指向pcre库文件目录–with-pcre-opt= 在编译时为pcre库设置附加参数–with-md5= 指向md5库文件目录(消息摘要算法第五版,用以提供消息的完整性保护)–with-md5-opt= 在编译时为md5库设置附加参数–with-md5-asm 使用md5汇编源–with-sha1= 指向sha1库目录(数字签名算法,主要用于数字签名)–with-sha1-opt= 在编译时为sha1库设置附加参数–with-sha1-asm 使用sha1汇编源–with-zlib= 指向zlib库目录–with-zlib-opt= 在编译时为zlib设置附加参数–with-zlib-asm= 为指定的CPU使用zlib汇编源进行优化,CPU类型为pentium, pentiumpro–with-libatomic 为原子内存的更新操作的实现提供一个架构–with-libatomic= 指向libatomic_ops安装目录–with-openssl= 指向openssl安装目录–with-openssl-opt 在编译时为openssl设置附加参数–with-debug 启用debug日志
2.2、编辑配置并启动进行测试
[nginx@master conf]$pwd/opt/nginx/nginx/confuser nginx;listen 1080;
[nginx@master conf]$cd../sbin/[nginx@master sbin]$ ./nginx
测试:http://192.168.10.128:1080/
配置文件解释:
#定义Nginx运行的用户和用户组user nginx;#nginx进程数,建议设置为等于CPU总核心数。worker_processes 48;#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]error_log log/error.log info;#进程文件pid logs/nginx.pid;#工作模式与连接数上限events{#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。use epoll;#单个进程最大连接数(最大连接数=连接数*进程数)worker_connections 65535;}#设定http服务器http{include mime.types;#文件扩展名与文件类型映射表default_type application/octet-stream;#默认文件类型#charset utf-8; #默认编码server_names_hash_bucket_size 128;#服务器名字的hash表大小client_header_buffer_size 32k;#上传文件大小限制large_client_header_buffers 4 64k;#设定请求缓client_max_body_size 8m;#设定请求缓sendfile on;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。autoindex on;#开启目录列表访问,合适下载服务器,默认关闭。tcp_nopush on;#防止网络阻塞tcp_nodelay on;#防止网络阻塞keepalive_timeout 120;#长连接超时时间,单位是秒#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 64k;fastcgi_buffers 4 64k;fastcgi_busy_buffers_size 128k;fastcgi_temp_file_write_size 128k;#gzip模块设置gzipon;#开启gzip压缩输出gzip_min_length 1k;#最小压缩文件大小gzip_buffers 4 16k;#压缩缓冲区gzip_http_version 1.0;#压缩版本(默认1.1,前端如果是squid2.5请使用1.0)gzip_comp_level 2;#压缩等级gzip_types text/plainapplication/x-javascripttext/cssapplication/xml;#压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。gzip_vary on;#limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用upstream aaa.com {#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。server 192.168.10.129:80 weight=3;server 192.168.10.130:80 weight=2;server 192.168.10.131:80 weight=3;}#虚拟主机的配置server{#监听端口listen 80;#域名可以有多个,用空格隔开server_name www.aaa.com aaa.com;index index.html index.htm index.php;root/opt/nginx/nginx/lansgg;location ~ .*.(php|php5)?${fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;include fastcgi.conf;}#图片缓存时间设置location ~ .*.(gif|jpg|jpeg|png|bmp|swf)${expires 10d;}#JS和CSS缓存时间设置location ~ .*.(js|css)?${expires 1h;}#日志格式设定log_format access'$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" $http_x_forwarded_for';#定义本虚拟主机的访问日志access_log logs/lansgg.access.log access;#对 "/" 启用反向代理location / {proxy_pass http://127.0.0.1:88;proxy_redirect off;proxy_set_header X-Real-IP $remote_addr;#后端的Web服务器可以通过X-Forwarded-For获取用户真实IPproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#以下是一些反向代理的配置,可选。proxy_set_header Host $host;client_max_body_size 10m;#允许客户端请求的最大单文件字节数client_body_buffer_size 128k;#缓冲区代理缓冲用户端请求的最大字节数,proxy_connect_timeout 90;#nginx跟后端服务器连接超时时间(代理连接超时)proxy_send_timeout 90;#后端服务器数据回传时间(代理发送超时)proxy_read_timeout 90;#连接成功后,后端服务器响应时间(代理接收超时)proxy_buffer_size 4k;#设置代理服务器(nginx)保存用户头信息的缓冲区大小proxy_buffers 4 32k;#proxy_buffers缓冲区,网页平均在32k以下的设置proxy_busy_buffers_size 64k;#高负荷下缓冲大小(proxy_buffers*2)proxy_temp_file_write_size 64k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传}#设定查看Nginx状态的地址location/NginxStatus{stub_status on;access_log on;auth_basic"NginxStatus";auth_basic_user_file confpasswd;#htpasswd文件的内容可以用apache提供的htpasswd工具来产生。}#本地动静分离反向代理配置#所有jsp的页面均交由tomcat或resin处理location ~ .(jsp|jspx|do)?$ {proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://127.0.0.1:8080;}#所有静态文件由nginx直接读取不经过tomcat或resinlocation ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)${ expires 15d; }location ~ .*.(js|css)?${ expires 1h; }}}
3.1、虚拟主机配置;
相同IP、端口、不同域名
[root@master conf]# pwd/opt/nginx/nginx/conf[root@master conf]# vim nginx.conf #新增server {listen 80 default_server;#默认域名server_name www.bbb.com bbb.com;#域名access_log logs/lansgg.access.log main;#日志路径,日志格式error_log logs/lansgg.error.log;#错误日志路径location / {index index.html;root/opt/nginx/nginx/html/lansgg;#此域名的文档目录}}server {listen 80;server_name www.aaa.com aaa.com;access_log logs/Aries.access.log main;error_log logs/Aries.error.log;location / {index index.html;root/opt/nginx/nginx/html/Aries;}}
测试,将Host进行修改;
127.0.0.1 localhost192.168.10.128 www.aaa.com192.168.10.128 www.bbb.com
分别访问 http://www.xxx.com/ http://www.bbb.com/
查看日志:
[nginx@master logs]$pwd/opt/nginx/nginx/logs[nginx@master logs]$catlansgg.access.log192.168.10.1 - - [06/Nov/2014:15:59:53 +0800]"GET / HTTP/1.1"200 7"-""Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0""-"[nginx@master logs]$catAries.access.log192.168.10.1 - - [06/Nov/2014:15:59:59 +0800]"GET / HTTP/1.1"200 6"-""Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0""-"192.168.10.1 - - [06/Nov/2014:15:59:59 +0800]"GET /favicon.ico HTTP/1.1"404 142"-""Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0""-"
分别访问错误;www.xxx.com/cc www.xxx.com/cc
查看日志:
[nginx@master logs]$catlansgg.error.log2014/11/0616:00:28 [error] 49576#0: *1 open() "/opt/nginx/nginx/html/lansgg/cc" failed (2: No such file or directory), client: 192.168.10.1, server: www.lansgg.com, request: "GET /cc HTTP/1.1", host: "[nginx@master logs]$catAries.error.log2014/11/0616:17:27 [error] 49576#0: *4 open() "/opt/nginx/nginx/html/Aries/cc" failed (2: No such file or directory), client: 192.168.10.1, server: www.aries.com, request: "GET /cc HTTP/1.1", host: "www.xxx.com"
3.2、自定义日志格式:
log_format main'$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 :用来记录从那个页面链接访问过来的;
$http_user_agent :记录客户端浏览器的相关信息;
自定义:在nginx.conf 的 http区域 (只做测试)
log_format access_test'$remote_addr - $remote_user [$time_local] "request"''$status'
将其应用到access_log;
server {listen 80 default_server;server_name www.lansgg.com lansgg.com;access_log logs/lansgg.access.log access_test;error_log logs/lansgg.error.log;location / {index index.html;root/opt/nginx/nginx/html/lansgg;}}
访问www.domain.com,查看日志
192.168.10.1 - - [06/Nov/2014:16:27:21 +0800]"request"200sendfileon
3.3、利用location去除部分日志:只要访问本域名下的图片文件就不记录日志;
未设置前:访问 http://www.domain.com/it.jpg 日志如下:
192.168.10.1 - - [06/Nov/2014:16:47:03 +0800]"GET /it.jpg HTTP/1.1"200 153579"-""Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0""-"
编辑nginx.conf
server {listen 80 default_server;server_name www.lansgg.com lansgg.com;access_log logs/lansgg.access.log main;error_log logs/lansgg.error.log;root/opt/nginx/nginx/html/lansgg;location / {index index.html;}location ~.*\.(gif|jpg|jpeg|png|bmp|swf)$ {expires 3d;access_log off;}
}
再次访问it.jpg;日志无输出;
上面新增内容:关闭了图片等访问日志,还设置了缓存,就是对这些gif|jpg|jpeg|png|bmp|swf等后缀的文件,设置一个缓存时间过期时间,图片等设置了3天,然后加上了access_log off;关闭他们的日志记录。