安裝平臺爲: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
注意: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
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
訪問一個不存在頁面
基於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
基於用戶的訪問控制
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
用戶認證成功,成功訪問bbs
Autoindex on:在未定義索引文件時顯示文件列表
定義狀態信息
5、設置顯示狀態信息
# vim /etc/nginx.conf
location /status {
stub_status on ;
}
# service nginx reload
訪問172.16.50.10/status,狀態查看
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
查看相關的文件是否存在
生成私鑰
# 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
[root@www ssl]# openssl ca -in nginx.csr-out nginx.crt -days 3665
# service nginx restart
查看端口是否啓動
# netstat -tnlp | grep 443
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
在本主機上解析一下主機名
# 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