-------------------------------------------------
一、前言
二、环境
三、配置
1.安装libevent
2.安装pcre-devel和openssl-devel
3.创建nginx组和账号
4.安装nginx
四、测试
1.简单测试
2.基于地址的虚拟主机测试
3.基于端口的虚拟主机
4.基于主机头的访问
5.物理目录
6.虚拟目录
-------------------------------------------------
一、前言
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:新浪、网易、 腾讯等。
Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性:在高连接并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型。
二、环境
系统:CentOS6.4 32位
源码包:
nginx-1.4.4.tar.gz
libevent-2.0.21-stable.tar.gz
三、配置
1.安装libevent
# rpm -qa |grep libevent libevent-1.4.13-4.el6.i686 # yum remove libevent # tar -zxvf libevent-2.0.21-stable.tar.gz -C /usr/local/src/ # cd /usr/local/src/libevent-2.0.21-stable/ # ./configure --prefix=/usr/local/libevent # make && make install # cd /usr/local/libevent/ # cd lib/ # ll # vim /etc/ld.so.conf.d/libevent.conf # ldconfig # ldconfig -pv |grep libevent libevent_pthreads-2.0.so.5 (libc6) => /usr/local/libevent/lib/libevent_pthreads-2.0.so.5 libevent_extra-2.0.so.5 (libc6) => /usr/local/libevent/lib/libevent_extra-2.0.so.5 libevent_core-2.0.so.5 (libc6) => /usr/local/libevent/lib/libevent_core-2.0.so.5 libevent-2.0.so.5 (libc6) => /usr/local/libevent/lib/libevent-2.0.so.5
2.安装pcre-devel和openssl-devel
# rpm -qa |grep pcre pcre-7.8-6.el6.i686 # yum install pcre-devel -y # rpm -qa |grep openssl openssl-1.0.0-27.el6.i686 # yum install openssl-devel -y
3.创建组和账号
# groupadd -r nginx # useradd -r -g nginx -s /sbin/nologin -M nginx
4.安装nginx
# yum remove httpd # setenforce 0 //尽量关闭防火墙和SELinux # tar -zxvf nginx-1.4.4.tar.gz -C /usr/local/src/nginx-1.4.4/ # ./configure \ --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/ \ --with-pcre //路径详单 nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx configuration prefix: "/etc/nginx" nginx configuration file: "/etc/nginx/nginx.conf" nginx pid file: "/var/run/nginx/nginx.pid" nginx error log file: "/var/log/nginx/error.log" nginx http access log file: "/var/log/nginx/access.log" nginx http client request body temporary files: "/var/tmp/nginx/client/" nginx http proxy temporary files: "/var/tmp/nginx/proxy/" nginx http fastcgi temporary files: "/var/tmp/nginx/fcgi/" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" # make && make install # cd /etc/nginx/sbin/ # ./nginx -h //显示帮助 nginx version: nginx/1.4.4 Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives] Options: -?,-h : this help -v : show version and exit -V : show version and configure options then exit -t : test configuration and exit -q : suppress non-error messages during configuration testing -s signal : send signal to a master process: stop, quit, reopen, reload -p prefix : set prefix path (default: /usr/local/nginx/) -c filename : set configuration file (default: /etc/nginx/nginx.conf) -g directives : set global directives out of configuration file # ./nginx -v //显示版本 nginx version: nginx/1.4.4 # ./nginx -t //测试语法,有错误 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory) nginx: configuration file /etc/nginx/nginx.conf test failed # mkdir -pv /var/tmp/nginx/client //按照提示更正错误 mkdir: created directory `/var/tmp/nginx' mkdir: created directory `/var/tmp/nginx/client' # ./nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful # ./nginx //启动nginx # netstat -tupln |grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12682/nginx # vim /etc/init.d/nginx //编辑nginx启动脚本 #!/bin/bash . /etc/init.d/functions prog=/usr/local/nginx/sbin/nginx lockfile=/var/lock/nginx.lock start () { if [ -e $lockfile ];then echo "nginx is started" && exit else echo -n "nginx is starting....." sleep 1 $prog &>/dev/null && touch $lockfile && echo "OK" || echo "failer" fi } configtest (){ $prog -t } stop () { if [ ! -e $lockfile ];then echo "nginx is stoped" && exit else echo -n "httpd is stoping......" sleep 1 $prog -s stop &>/dev/null && rm -rf $lockfile && echo "OK" || echo "failer" fi } restart () { if [ -e $lockfile ];then killproc nginx && rm -rf $lockfile else $prog && echo "OK" && touch $lockfile fi } case "$1" in start) start ;; stop) stop ;; restart) restart ;; configtest) configtest ;; *) echo "Usage:start|stop|configtest|restart" esac # chmod a+x /etc/init.d/nginx # vim /etc/nginx/nginx.conf //nginx主配置文档 12 events { 13 use epoll; //使用epoll机制 14 worker_connections 1024; 15 } # service nginx restart
四、测试
1.简单测试
客户机访问http://192.168.2.10
2.基于地址的虚拟主机测试
# cd /usr/local/nginx/ # echo 'Welcome to www.nuo.com' >html/index.html # mkdir tec # echo 'Welcome to tec.nuo.com!' >tec/index.html # ifconfig eth0:1 192.168.2.20 //添加临时地址 # vim /etc/nginx/nginx.conf 36 server { 37 listen 192.168.2.10:80; 38 server_name www.nuo.com; 81 server { //复制36-80行内容 82 listen 192.168.2.20:80; 83 server_name tec.nuo.com; 84 85 #charset koi8-r; 86 87 #access_log logs/host.access.log main; 88 89 location / { 90 root tec; 91 index index.html index.htm; 92 } 93 access_log /var/log/nginx/tec_log; 94 error_log /var/log/nginx/tec_err_log; 95 #error_page 404 /404.html; 96 97 # redirect server error pages to the static page /50x.html 98 # 99 error_page 500 502 503 504 /50x.html; 100 location = /50x.html { 101 root html; 102 } # service nginx configtest nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful # service nginx restart # netstat -tupln |grep nginx tcp 0 0 192.168.2.20:80 0.0.0.0:* LISTEN 15284/nginx tcp 0 0 192.168.2.10:80 0.0.0.0:* LISTEN 15284/nginx //客户机hosts文件设置 192.168.2.10 www.nuo.com 192.168.2.20 tec.nuo.com
3.基于端口的虚拟主机
# vim /etc/nginx/nginx.conf 82 listen 192.168.2.10:800; 83 server_name www.nuo.com; # service nginx restart # netstat -tupln |grep nginx tcp 0 0 192.168.2.10:80 0.0.0.0:* LISTEN 15468/nginx tcp 0 0 192.168.2.10:800 0.0.0.0:* LISTEN 15468/nginx //客户机hosts文件设置 192.168.2.10 www.nuo.com
4.基于主机头的访问
# vim /etc/nginx/nginx.conf 82 listen 192.168.2.10:80; 83 server_name tec.nuo.com; # service nginx restart //客户机hosts文件设置 192.168.2.10 www.nuo.com 192.168.2.10 tec.nuo.com
5.物理目录
# cd /usr/local/nginx/html # mkdir abc # echo 'This is real dir' >abc/index.html
6.虚拟目录
# vim /etc/nginx/nginx.conf //添加48-51行内容 48 location /public { 49 alias /abc; 50 index index.html index.htm; 51 } # service nginx restart