目录:
一, 软件安装
二, 虚拟主机
三, 用户认证
四, HTTPS
五, 反向代理
六, 负载均衡
七, URL重写
*********************************************************************************************************************************************
在rhel5.x上实现以上功能
*********************************************************************************************************************************************
一,编译安装nginx
下载nginx-1.2.2.tar.gz到/usr/src目录 当然也可以下载最新版的,1.2.2的版本目前来说还是最新版的
#安装相关软件包
#yum -y groupinstall "Development Libraries" "Development Tools"
#yum -y install pcre-devel
#useradd -s /sbin/nologin nginx
#cd /usr/src
#tar xzvf nginx-1.2.2.tar.gz
#cd /usr/src/nginx-1.2.2
#./configure --prefix=/usr/local/nginx --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
#make && make install
给nginx提供SysV服务启动脚本
#vim /etc/init.d/nginx 内容如下
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/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 -TERM
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 a+x /etc/init.d/nginx
#chkconfig --add nginx
#chkconfig nginx on
#service nginx start
二,配置虚拟主机
#vim /usr/local/nginx/conf/nginx.conf
.....
server {
listen 80; #监听的端口
server_name www.andy.com; #访问此站点的域名
index index.html index.htm; #默认主页
root /www; #网站根目录
}
server {
........
.......
}
.......
如果说此服务器的虚拟主机很多,显然上面的配置方法不易于管理,我们可以专门指定一个目录来放虚拟主机的配置文件,每一个虚拟主机单独使用一个配置文件,这样的话管理起来也方便多了,配置如下
#vim /usr/local/nginx/conf/nginx.conf
.....
.....
include conf/vhost/*.conf;
#mkdir /usr/local/nginx/conf/vhost
#vim /usr/local/nginx/conf/vhost/www.conf
server {
listen 80; #监听的端口
server_name www.andy.com; #访问此站点的域名
index index.html index.htm; #默认主页
root /www; #网站根目录
}
三,用户认证
#这里使用htpasswd工具所生成的用户名密码作为用户认证的来源
#htpasswd -cd /usr/local/nginx/conf/.auth andy
#htpasswd -d /usr/local/nginx/conf/.auth andy_f
#vim /usr/local/nginx/conf/vhost/www.conf
server {
listen 80;
server_name www.andy.com;
index index.html index.htm;
root /www;
location / {
auth_basic "test";
auth_basic_user_file /usr/local/nginx/conf/.auth;
}
#service nginx restart
四, HTTPS 使用自颁发证书实现
#建立存放https证书的目录
#mkdir -pv /usr/local/nginx/conf/.sslkey
#生成网站私钥文件
#cd /usr/local/nginx/conf/.sslkey
#openssl genrsa -out https.key 1024
#生存网站证书文件,需要注意的是在生成的过程中需要输入一些信息根据自己的需要输入,但Common Name 项输入的必须是访问网站的FQDN
#openssl req -new -x509 -key https.key -out https.crt
#为了安全起见,将存放证书的目录权限设置为400
#chmod -R 400 /usr/local/nginx/conf/.sslkey
#vim /usr/local/nginx/conf/vhost/www.conf
server {
listen 443;
server_name www.andy.com;
index index.html index.htm;
root /www;
ssl on;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
ssl_certificate /usr/local/nginx/conf/.sslkey/https.crt;
ssl_certificate_key /usr/local/nginx/conf/.sslkey/https.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
}
#重新启动nginx服务
#service nginx restart
五, 反向代理 带缓存机制的反向代理
#vim /usr/local/nginx/conf/nginx/conf
.......
.......
http {
......
proxy_cache_path /var/www/cache levels=1:2 keys_zone=mycache:20m max_size=2048m inactive=60m;
proxy_temp_path /www/cache/tmp;
......
server {
listen 80;
server_name www.andy.com;
location / {
proxy_pass http://127.0.0.1/;
proxy_cache mycache;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
#重启nginx服务
#service nginx restart
六, 负载均衡
在nginx中实现负载均衡其实也是使用反向代理的机制,那么在nginx中默认支持的调度算法有三种,轮询,加权轮询,ip_hash, 负载均衡是啥不用解释了吧,下面来配置下nginx的负载均衡.
#vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 10;
error_log logs/error.log crit;
pid logs/nginx.pid;
events
{
use epoll;
worker_connections 51000;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 60;
tcp_nodelay on;
#指定负载均衡的方式 bbs.andy.com 是一个名字负载均衡集群的名字
upstream bbs.andy.com {
server 172.16.0.2:80; #后端的应用服务器
server 172.16.0.3:80;
server 172.16.0.4:80 weight=5; 权重
server 172.16.0.5:8080 backup; 备份节点,
ip_hash; #调度算法
}
server {
listen 80;
server_name bbs.andy.com;
index index.html index.htm index.php;
location / {
proxy_pass http://bbs.andy.com; #指定反代到哪个主机,或集群列表中
#如果后端服务器出现502 或504错误代码的话nginx就不会请求某台服务器了,当后端服务器又工作正常了,nginx继续请求,这样一来达到了后端服务器健康状况检测的功能,
proxy_next_upstream http_502 http_504 error timeout invalid_header;
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_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 8k;
proxy_temp_file_write_size 64k;
}
access_log logs/bbs.log;
}
}
七,URL重写
所谓url重写就是将某个路径重新定位到另一个路径,跟查找替换差不多,格式如下
语法格式 : rewrite regex replacement [flag];
在nginx中url重写的处理机制有以下4种,
last 被匹配到的url再进行匹配
break 被匹配到的url就不在进行匹配了
redirect 临时重定向
permanent 永久重定向
1,比如说访问某站点的路径为/forum/ 此时想使用/bbs来访问此站点需要做url重写如下
location / {
rewrite ^/forum/?$ /bbs/ permanent;
}
2,比如说某站点有个图片服务器(10.0.0.1/images/ ) 此时访问某站点上/images/的资源时希望访问到图片服务器上的资源
location / {
rewrite ^/images/(.*.jpg)$ /images2/$1 break;
}
3, 域名跳转
server
{
listen 80;
server_name andy.com;
rewrite ^/ http://www.andy.com/;
}
4,域名镜像
server
{
listen 80;
server_name andy.com;
rewrite ^/(.*)$ http://www.andy.com/$1 last;
}
*********************************************************************************************************************************************
在nginx的url重写中还支持if判断语句,
语法 if ( 条件 ) { ..... }
应用环境 server ,location
if 判断的条件如下
1、变量名; false values are: empty string ("", or any string starting with "0";)
2、对于变量进行的比较表达式,可使用=或!=进行测试;
3、正则表达式的模式匹配:
~ 区分大小的模式匹配
~* 不区分字母大小写的模式匹配
!~ 和 !~* 分别对上面的两种测试取反
4、测试文件是否存在-f或!-f
5、测试目录是否存在-d或!-d
6、测试目录、文件或链接文件的存在性-e或!-e
7、检查一个文件的执行权限-x或!-x
nginx 的一些内置变量,
http://wiki.nginx.org/HttpCoreModule#Variables 官方文档
$arg_PARAMETER
$args
$binary_remote_addr
$body_bytes_sent
$content_length
$content_type
$cookie_COOKIE
$document_root
$document_uri
$host
$hostname
$http_HEADER
$sent_http_HEADER
$is_args
$limit_rate
$nginx_version
$query_string
$remote_addr
$remote_port
$remote_user
$request_filename
$request_body
$request_body_file
$request_completion
$request_method
$request_uri
$scheme
$server_addr
$server_name
$server_port
$server_protocol
$uri
*********************************************************************************************************************************************
5,简单的防盗链
location ~* .(gif|jpg|png|swf|flv)$ {
valid_referers none blocked www.andy.com;
if ($invalid_referer) {
rewrite ^/ http://www.andy.com/403.html;
}
6,如果用户请求的页面不存在则自定义跳转
if (!-f $request_filename) {
rewrite ^(/.*)$ http://www.andy.com permanent;
}
7,判断用户浏览器的类型,作出相应的跳转,
if ($http_user_agent ~* MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
一, 软件安装
二, 虚拟主机
三, 用户认证
四, HTTPS
五, 反向代理
六, 负载均衡
七, URL重写
*********************************************************************************************************************************************
在rhel5.x上实现以上功能
*********************************************************************************************************************************************
一,编译安装nginx
下载nginx-1.2.2.tar.gz到/usr/src目录 当然也可以下载最新版的,1.2.2的版本目前来说还是最新版的
#安装相关软件包
#yum -y groupinstall "Development Libraries" "Development Tools"
#yum -y install pcre-devel
#useradd -s /sbin/nologin nginx
#cd /usr/src
#tar xzvf nginx-1.2.2.tar.gz
#cd /usr/src/nginx-1.2.2
#./configure --prefix=/usr/local/nginx --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
#make && make install
给nginx提供SysV服务启动脚本
#vim /etc/init.d/nginx 内容如下
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/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 -TERM
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 a+x /etc/init.d/nginx
#chkconfig --add nginx
#chkconfig nginx on
#service nginx start
二,配置虚拟主机
#vim /usr/local/nginx/conf/nginx.conf
.....
server {
listen 80; #监听的端口
server_name www.andy.com; #访问此站点的域名
index index.html index.htm; #默认主页
root /www; #网站根目录
}
server {
........
.......
}
.......
如果说此服务器的虚拟主机很多,显然上面的配置方法不易于管理,我们可以专门指定一个目录来放虚拟主机的配置文件,每一个虚拟主机单独使用一个配置文件,这样的话管理起来也方便多了,配置如下
#vim /usr/local/nginx/conf/nginx.conf
.....
.....
include conf/vhost/*.conf;
#mkdir /usr/local/nginx/conf/vhost
#vim /usr/local/nginx/conf/vhost/www.conf
server {
listen 80; #监听的端口
server_name www.andy.com; #访问此站点的域名
index index.html index.htm; #默认主页
root /www; #网站根目录
}
三,用户认证
#这里使用htpasswd工具所生成的用户名密码作为用户认证的来源
#htpasswd -cd /usr/local/nginx/conf/.auth andy
#htpasswd -d /usr/local/nginx/conf/.auth andy_f
#vim /usr/local/nginx/conf/vhost/www.conf
server {
listen 80;
server_name www.andy.com;
index index.html index.htm;
root /www;
location / {
auth_basic "test";
auth_basic_user_file /usr/local/nginx/conf/.auth;
}
#service nginx restart
四, HTTPS 使用自颁发证书实现
#建立存放https证书的目录
#mkdir -pv /usr/local/nginx/conf/.sslkey
#生成网站私钥文件
#cd /usr/local/nginx/conf/.sslkey
#openssl genrsa -out https.key 1024
#生存网站证书文件,需要注意的是在生成的过程中需要输入一些信息根据自己的需要输入,但Common Name 项输入的必须是访问网站的FQDN
#openssl req -new -x509 -key https.key -out https.crt
#为了安全起见,将存放证书的目录权限设置为400
#chmod -R 400 /usr/local/nginx/conf/.sslkey
#vim /usr/local/nginx/conf/vhost/www.conf
server {
listen 443;
server_name www.andy.com;
index index.html index.htm;
root /www;
ssl on;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
ssl_certificate /usr/local/nginx/conf/.sslkey/https.crt;
ssl_certificate_key /usr/local/nginx/conf/.sslkey/https.key;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
}
#重新启动nginx服务
#service nginx restart
五, 反向代理 带缓存机制的反向代理
#vim /usr/local/nginx/conf/nginx/conf
.......
.......
http {
......
proxy_cache_path /var/www/cache levels=1:2 keys_zone=mycache:20m max_size=2048m inactive=60m;
proxy_temp_path /www/cache/tmp;
......
server {
listen 80;
server_name www.andy.com;
location / {
proxy_pass http://127.0.0.1/;
proxy_cache mycache;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
#重启nginx服务
#service nginx restart
六, 负载均衡
在nginx中实现负载均衡其实也是使用反向代理的机制,那么在nginx中默认支持的调度算法有三种,轮询,加权轮询,ip_hash, 负载均衡是啥不用解释了吧,下面来配置下nginx的负载均衡.
#vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 10;
error_log logs/error.log crit;
pid logs/nginx.pid;
events
{
use epoll;
worker_connections 51000;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 60;
tcp_nodelay on;
#指定负载均衡的方式 bbs.andy.com 是一个名字负载均衡集群的名字
upstream bbs.andy.com {
server 172.16.0.2:80; #后端的应用服务器
server 172.16.0.3:80;
server 172.16.0.4:80 weight=5; 权重
server 172.16.0.5:8080 backup; 备份节点,
ip_hash; #调度算法
}
server {
listen 80;
server_name bbs.andy.com;
index index.html index.htm index.php;
location / {
proxy_pass http://bbs.andy.com; #指定反代到哪个主机,或集群列表中
#如果后端服务器出现502 或504错误代码的话nginx就不会请求某台服务器了,当后端服务器又工作正常了,nginx继续请求,这样一来达到了后端服务器健康状况检测的功能,
proxy_next_upstream http_502 http_504 error timeout invalid_header;
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_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 8k;
proxy_temp_file_write_size 64k;
}
access_log logs/bbs.log;
}
}
七,URL重写
所谓url重写就是将某个路径重新定位到另一个路径,跟查找替换差不多,格式如下
语法格式 : rewrite regex replacement [flag];
在nginx中url重写的处理机制有以下4种,
last 被匹配到的url再进行匹配
break 被匹配到的url就不在进行匹配了
redirect 临时重定向
permanent 永久重定向
1,比如说访问某站点的路径为/forum/ 此时想使用/bbs来访问此站点需要做url重写如下
location / {
rewrite ^/forum/?$ /bbs/ permanent;
}
2,比如说某站点有个图片服务器(10.0.0.1/images/ ) 此时访问某站点上/images/的资源时希望访问到图片服务器上的资源
location / {
rewrite ^/images/(.*.jpg)$ /images2/$1 break;
}
3, 域名跳转
server
{
listen 80;
server_name andy.com;
rewrite ^/ http://www.andy.com/;
}
4,域名镜像
server
{
listen 80;
server_name andy.com;
rewrite ^/(.*)$ http://www.andy.com/$1 last;
}
*********************************************************************************************************************************************
在nginx的url重写中还支持if判断语句,
语法 if ( 条件 ) { ..... }
应用环境 server ,location
if 判断的条件如下
1、变量名; false values are: empty string ("", or any string starting with "0";)
2、对于变量进行的比较表达式,可使用=或!=进行测试;
3、正则表达式的模式匹配:
~ 区分大小的模式匹配
~* 不区分字母大小写的模式匹配
!~ 和 !~* 分别对上面的两种测试取反
4、测试文件是否存在-f或!-f
5、测试目录是否存在-d或!-d
6、测试目录、文件或链接文件的存在性-e或!-e
7、检查一个文件的执行权限-x或!-x
nginx 的一些内置变量,
http://wiki.nginx.org/HttpCoreModule#Variables 官方文档
$arg_PARAMETER
$args
$binary_remote_addr
$body_bytes_sent
$content_length
$content_type
$cookie_COOKIE
$document_root
$document_uri
$host
$hostname
$http_HEADER
$sent_http_HEADER
$is_args
$limit_rate
$nginx_version
$query_string
$remote_addr
$remote_port
$remote_user
$request_filename
$request_body
$request_body_file
$request_completion
$request_method
$request_uri
$scheme
$server_addr
$server_name
$server_port
$server_protocol
$uri
*********************************************************************************************************************************************
5,简单的防盗链
location ~* .(gif|jpg|png|swf|flv)$ {
valid_referers none blocked www.andy.com;
if ($invalid_referer) {
rewrite ^/ http://www.andy.com/403.html;
}
6,如果用户请求的页面不存在则自定义跳转
if (!-f $request_filename) {
rewrite ^(/.*)$ http://www.andy.com permanent;
}
7,判断用户浏览器的类型,作出相应的跳转,
if ($http_user_agent ~* MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
OK ,完工了,暂时总结了这么多
http://www.codesky.net/article/201208/171296.html