一:Nginx环境搭建
1:Nginx下载
wget http://nginx.org/download/nginx-1.6.2.tar.gz
2:解压安装
tar -xvf nginx-1.6.2.tar.gz
3:下载所需的依赖包
yum -y install pcre
yum -y install pcre-devel
yum -y install zlib
yum -y install zlib-devel
yum -y install gcc-c++
4:进行configure配置
cd nginx-1.6.2 && ./configure --prefix=/opt/module/nginx
5:编译安装
make && make install
6:启动Nginx
cd /opt/module/nginx/sbin
./nginx //启动
./nginx -s stop //关闭
./nginx -s reload //重启
7:检验
使用ps -ef | grep nginx查看进程,使用浏览器访问http://xx.xx.xx.xx:yy。
8:设置开机自启动
8.1:编写shell脚本
vim /etc/rc.d/init.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
8.2:设置文件的访问权限
chmod a+x /etc/init.d/nginx
8.3:设置开机启动
chkconfig nginx on
二:Nginx虚拟主机配置
server {
listen 8008;
#监听端口
server_name localhost;
#服务器主机名
location / {
#根目录
root html;
#默认访问的页面
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
#错误页面
location = /50x.html {
#根目录
root html;
}
}
三:Nginx日志文件讲解
1:文件概念
1.1:access.log:成功日志
1.2:error.log:错误日志
1.3:nginx.pid:运行进程PID码日志
2:配置日志路径
access_log logs/access.log main
3:查看日志内容命令
tail -n 100 -f nginx/logs/access.log
四:日志文件切分
1:编写shell脚本log.sh
1-1:脚本内容
#!/bin/sh
BASE_DIR=/usr/local/nginx
BASE_FILE_NAME=bhz.com.access.log
CURRENT_PATH=$BASE_DIR/logs
BAK_PATH=$BASE_DIR/datalogs
CURRENT_FILE=$CURRENT_PATH/$BASE_FILE_NAME
BAK_TIME=`/bin/date -d yesterday +%Y%m%d%H%M`
BAK_FILE=$BAK_PATH/$BAK_TIME-$BASE_FILE_NAME
echo $BAK_FILE
$BASE_DIR/sbin/nginx -s stop
mv $CURRENT_FILE $BAK_FILE
$BASE_DIR/sbin/nginx
1.2:给log.sh赋权
chmod 777 log.sh
2:定时任务对脚本进行调度
crontab -e
*/1*****sh /opt/module/nginx/sbin/log.sh
五:nginx_location配置讲解
1:location语法:表示uri方式定位
1.1:基础语法有三种
location=pattern {} 精准匹配,当精确匹配和一般匹配同时存在时,执行精确匹配的内容
location pattern {} 一般匹配,当存在多个一般匹配时,匹配度最高的执行
location ~ pattern {} 正则匹配,正则匹配存在多个时,匹配度最高的执行,正则与普通匹配同时存在时,执行正则
1.2:其他语法
if (条件为: =~~*)、return 、break、rewrite
-f 是否为文件、-d 是否为目录、-e 是否为存在
1.3:nginx配置文件说明
#user nobody;
#开启进程数 <=CPU数
worker_processes 1;
#错误日志保存位置
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#进程号保存文件
#pid logs/nginx.pid;
#每个进程最大连接数(最大连接=连接数x进程数)每个worker允许同时产生多少个链接,默认1024
events {
worker_connections 1024;
}
http {
#文件扩展名与文件类型映射表
include mime.types;
#默认文件类型
default_type application/octet-stream;
#日志文件输出格式 这个位置相于全局设置
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#请求日志保存位置
#access_log logs/access.log main;
#打开发送文件
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#连接超时时间
keepalive_timeout 65;
#请求头允许下划线
underscores_in_headers on;
#打开gzip压缩
#gzip on;
#设定请求缓冲
#client_header_buffer_size 1k;
#large_client_header_buffers 4 4k;
#设定负载均衡的服务器列表
#upstream myproject {
#weigth参数表示权值,权值越高被分配到的几率越大
#max_fails 当有#max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查
#fail_timeout 在以后的#fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器
#}
#webapp
#upstream myapp {
# server 192.168.1.171:8080 weight=1 max_fails=2 fail_timeout=30s;
# server 192.168.1.172:8080 weight=1 max_fails=2 fail_timeout=30s;
#}
#配置虚拟主机,基于域名、ip和端口
server {
#监听端口
listen 80;
#监听域名
server_name localhost;
#charset koi8-r;
#nginx访问日志放在logs/host.access.log下,并且使用main格式(还可以自定义格式)
#access_log logs/host.access.log main;
#返回的相应文件地址
location / {
#设置客户端真实ip地址
#proxy_set_header X-real-ip $remote_addr;
#负载均衡反向代理
#proxy_pass http://myapp;
#返回根路径地址(相对路径:相对于/usr/local/nginx/)
root html;
#默认访问文件
index index.html index.htm;
}
#配置反向代理tomcat服务器:拦截.jsp结尾的请求转向到tomcat
#location ~ \.jsp$ {
# proxy_pass http://192.168.1.171:8080;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#错误页面及其返回地址
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
#虚拟主机配置:
server {
listen 1234;
server_name bhz.com;
location / {
#正则表达式匹配uri方式:在/usr/local/nginx/bhz.com下 建立一个test123.html 然后使用正则匹配
#location ~ test {
## 重写语法:if return (条件 = ~ ~*)
#if ($remote_addr = 192.168.1.200) {
# return 401;
#}
#if ($http_user_agent ~* firefox) {
# rewrite ^.*$ /firefox.html;
# break;
#}
root bhz.com;
index index.html;
}
#location /goods {
# rewrite "goods-(\d{1,5})\.html" /goods-ctrl.html;
# root bhz.com;
# index index.html;
#}
#配置访问日志
access_log logs/bhz.com.access.log main;
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
六:Nginx反向代理配置和负载均衡配置
1:反向代理
在location中使用proxy_pass
#配置反向代理tomcat服务器,拦截.jsp结尾的请求转向tomcat
location ~ \.jsp${
# proxy_set_header作用:设置发送到后端服务器(上面proxy_pass)的请求头值
# 当Host设置为 $http_host 时,则不改变请求头的值;
# 当Host设置为 $proxy_host 时,则会重新设置请求头中的Host信息;
# 当为$host变量时,它的值在请求包含Host请求头时为Host字段的值,在请求未携带Host请求头时为虚拟主机的主域名;
# 当为$host:$proxy_port时,即携带端口发送 ex: $host:8080
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; # 配置用户的真实ip
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 配置前一个代理服务IP
proxy_set_header REMOTE-HOST $remote_addr;
#转向http://192.168.1.114:8080;
proxy_pass http://192.168.1.114:8080;
}
2:负载均衡
与server平级使用upstream
#当主机名为myapp时,对请求分流给不同的主机
upstream myapp{
server 192.168.1.171:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.172:8080 weight=1 max_fails=2 fail_timeout=30s;
#weight:权重,请求分流给该主机的概率
#max_fails:失败多少次,认为主机宕机
#fail_timeout:请求超过多长时间,认为访问失败
}