安装平台为:rhel 6.4 64bit

一、安装Nginx

1、解决依赖关系

# yum groupinstall "DevelopmentTools" "Server Platform Deveopment"

# yum install openssl-devel pcre-devel

2、安装

首先添加用户nginx,实现与之运行nginx服务进程:

# groupadd -r -g 108 nginx

# useradd -r -g 108 -u 108 nginx

# date 时间同步

# ntpdate 172.16.0.1

# tar xf nginx-1.4.1.tar.gz

接着开始编译和安装:

# cd nginx-1.4.1/

# ./configure \

--prefix=/usr \

--sbin-path=/usr/sbin/nginx \

--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/ \

--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \

--http-scgi-temp-path=/var/tmp/nginx/scgi \

--with-pcre \

--with-file-aio

# make && make install

3、为nginx提供SysV init脚本:

新建文件/etc/rc.d/init.d/nginx,内容如下:

# vim /etc/rc.d/init.d/nginx

#!/bin/sh

#

# nginx - this script starts and stops thenginx 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/sbin/nginx"

prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f/etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {

  #make required directories

 user=`nginx -V 2>&1 | grep "configure arguments:" | sed's/[^*]*--user=\([^ ]*\).*/\1/g' -`

 options=`$nginx -V 2>&1 | grep 'configure arguments:'`

 for opt in $options; do

     if [ `echo $opt | grep '.*-temp-path'` ]; then

         value=`echo $opt | cut -d "=" -f 2`

         if [ ! -d "$value" ]; then

              # echo "creating"$value

              mkdir -p $value && chown-R $user $value

         fi

     fi

 done

}

start() {

   [-x $nginx ] || exit 5

   [-f $NGINX_CONF_FILE ] || exit 6

  make_dirs

  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

  sleep 1

  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

添加至服务管理列表,并让其开机自动启动:

# chkconfig --add nginx

# chkconfig nginx on

而后就可以启动服务并测试了:

# service nginx start

访问测试页  172.16.50.201

082248637.png

注意:nginx的主页面在/usr/html目录下

# cd /etc/nginx

# cp nginx.conf nginx.conf.bak

配置文件定义介绍

1、修改主页面文件路径

# vim nginx.conf

worker_processes 2;启动worker线程数,与cpu个数有关

location /bbs {

          root   /web;

          index  index.html index.htm;

      }

# mkdir /web/bbs -pv 创建所需目录

# vim /web/bbs/index.html

<h1> test page </h1>

# service nginx restart

访问页面       172.16.51.21/bbs

082331736.png

2、提供错误页面

直接在配置文件中启用

# vim nginx.conf

error_page 404    /404.html;

location / {

          root   /web/htdocs;

          index  index.html index.htm;

      }

# mkdir /web/htdocs -pv

# vim /web/htdocs/404.html

<h1>404 page </h1>

# service nginx reload

访问一个不存在页面

082350766.png

基于IP的访问控制

3、定义访问控制法则

也是在配置文件中的server段中

如bbs拒绝172.16.50.1访问(拒绝本主机访问bbs

location /bbs {

  root /web

  index index.html index.htm

  deny 172.16.50.1

}

# service nginx reload

082413943.png

基于用户的访问控制

4、需要先安装http,借助于apache的htpasswd建立用户

安装httpd

# yum -y installhttpd

不让其开机自启动

# chkconfig httpdoff

需要借用apache中的htppasswd命令定义的用户与密码

# htpasswd -c -m/etc/nginx/.users ji

第二次添加用户时不使用-c选项

# htpasswd -m /etc/nginx/.users tom

# vim /etc/nginx.conf

location /bbs {

 root /web ;

  indexindex.html index.htm ;

 auth_basic "Users:ji/tom Password:redhat";

 auth_basic_user_file /etc/nginx/.users;

}

# service nginx reload

082641346.png

用户认证成功,成功访问bbs

082703141.png

Autoindex on:在未定义索引文件时显示文件列表

定义状态信息

5、设置显示状态信息

# vim /etc/nginx.conf

location /status {

   stub_status on ;

}

# service nginx reload

访问172.16.50.10/status,状态查看

082721320.png

6、定义ssl模块的虚拟主机

# vim /etc/nginx /nginx.conf  在配置文件的最后有ssl的定义,这里直接启用即可使用

.,$s/^\([[:space:]]*\)#/\1/g     使用此命令可以将#号替换了

server {

listen 443;

server_name www.test.comt;

ssl    on;

ssl_certificate   /etc/nginx/ssl/nginx.crt;  证书

ssl_certificate_key /etc/nginx/ssl/ngix.key;  私钥

ssl_session_timeout  5m;   会话超时时间

ssl_protocols SSLv2 SSLv3 TLSv1;  使用的协议

ssl_ciphersHIGH:!aNULL:!MD5;   加密算法

ssl_prefer_server_ciphers  on;   是否允许服务端选择其倾向的加密算法

location / {

root /web/ssl;

index index.html index.htm;

   }

 }

}

# mkdir /web/ssl

# vim /web/ssl/index.html

<h1>https</h1>

查看路径是否正确(/etc/pki/CA

# vim/etc/pki/tls/openssl.conf

082742497.png

查看相关的文件是否存在

082804752.png

生成私钥

# cd /etc/pki/CA/

[root@www CA]# (umask 077; openssl genrsa2048 > private/cakey.pem)

[root@www CA]# openssl req -new -x509 -keyprivate/cakey.pem -out cacert.pem

[root@slave CA]# touch serial

[root@www CA]# echo 01 > serial

[root@slave CA]# cat serial

01          序列号为01

[root@slave CA]# touch index.txt

[root@slave CA]#mkdir /etc/nginx/ssl

[root@slave CA]#cd /etc/nginx/ssl

[root@www ssl]#(umask 077; openssl genrsa 2048 > nginx.key)

[root@www ssl]# openssl req -new -keynginx.key -out nginx.csr

082818264.png

[root@www ssl]# openssl ca -in nginx.csr-out nginx.crt -days 3665

# service nginx restart

查看端口是否启动

# netstat -tnlp | grep 443

082833741.png

访问https://172.16.50.201/

082849209.png

7、如何使用nginx的基于主机名的虚拟主机

# vim/etc/nginx.conf

server {

listen 80;

 server_name    www.magedu.com;(原来是localhost,现在改为www.magedu.com

}

server {  注意:要定义在server之外

 listen 80;

 server_name www.a.org;

 location / {

      root /web/a.org;

      index  index.html;

}

}

# mkdir /web/a.rog

# vim /web/a.rog/index.html

<h1>a.org</h1>

# nginx -t 测试语法是否有错

# service nginx restart

082905338.png

在本主机上解析一下主机名

# vim /etc/hosts

172.16.50.201  www.magedu.com

172.16.50.201  www.a.org

# curl -I http://www.magedu.com/index.html

# curl -I http://www.a.org/index.html

082918436.png