一: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;
#打開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${
#設置客戶端真實IP
proxy_set_header X-real-ip $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:請求超過多長時間,認爲訪問失敗
}