nginx的信號控制
ITEM,INT
Quick shutdown
QUIT
Graceful shutdown 優雅的關閉進程,即等請求結束後再關閉
HUP
Configuration reload ,Start the new worker processes with
a new configuration Gracefully shutdown the old worker processes
改變配置文件,平滑的重讀配置文件
USR1
Reopen the log files 重讀日誌,在日誌按月/日分割時有用
USR2
Upgrade Executable on the fly 平滑的升級
WINCH
Gracefully shutdown the worker processes 優雅關閉舊的進程(配合USR2來進行升級)
nginx配置段
//全局區
work_processes 1; //有一個工作的子進程,一般設置爲CPU數*核數
Event{ //配置nginx連接特性
work_connections 1024; //一個子進程最大允許1024個連接
}
http{
server{ //虛擬主機段
listen 80; //監聽端口
server_name a.com; //監聽域名
location{
root html; //根目錄定位
index index.html;
}
}
}
日誌輪替
#!/bin/bash
LOGPATH =/usr/local/nginx/logs/access.log
BASEPATH=/usr/local/nginx/data/$(date -d yesterday +%Y%m)
mkdir -p $(BASEPATH)
bak=$BASEPATH/$(date -d yesterday +%d%H%M).access.log
mv $LOGPATH $bak
touch $LOGPATH
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
location
location =/ //精準匹配
location / //普通匹配
location ~ //正則匹配
rewrite
location /ecshop {
index index.php;
rewrite goods-([\d]+)\.html$ /ecshop/goods.php?id=$1;
rewrite article-([\d]+)\.html$ /ecshop/article.php?id=$1;
rewrite category-(\d+)-b(\d+)\.html /ecshop/category.php?id=$1&brand=$2;
// 注意:用url重寫時, 正則裏如果有”{}”,正則要用雙引號包起來
}
nginx+php的編譯
// apache一般是把php當做自己的一個模塊來啓動的.
//而nginx則是把http請求變量(如get,user_agent等)轉發給 php進程,即php獨立進程,與nginx進行通信. 稱爲 fastcgi運行方式
//注意: 我們編譯的PHP 要有如下功能:
//連接mysql, gd, ttf, 以fpm(fascgi)方式運行
./configure --prefix=/usr/local/fastphp \
--with-mysql=mysqlnd \
--enable-mysqlnd \
--with-gd \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--enable-fpm
cp /usr/local/src/php/php.ini-development ./lib/php.ini
cp etc/php-fpm.conf.default etc/php-fpm.conf
gzip配置常用參數
gzip on|off //
gzip_buffers 32 4K | 16 8K //緩衝(壓縮在內存中緩衝幾塊? 每塊多大?)
gzip_comp_level[1-9] //級別越高,壓的越小,越浪費CPU計算資源
gzip_disable //正則匹配UA 什麼樣的Uri不進行gzip
gzip_min_length 200 // 開始壓縮的最小長度(再小就不要壓縮了)
gzip_http_version 1.0|1.1 //開始壓縮的http協議版本
gzip_proxied //設置請求者代理服務器,該如何緩存內容
gzip_types text/plain application/xml //對哪些類型的文件用壓縮 如txt,xml,html ,css
gzip_vary on|off //是否傳輸gzip壓縮標誌
反向代理
location ~ \.php${
proxy_set_header X-Forwarded-For $remote_addr
proxy_pass http://192.168.1.1:8080;//把請求的PHP文件代理到Apache服務器。
//修改httpd.conf。allowoverride all引入extra/httpd-vhosts.conf
//修改extra/httpd-vhosts.conf。指定根目錄,服務器名,端口
}
負載均衡
server {}
server {}
upstream servername{
server 192.168.1.1:81 weight=1 max_fails=2 fail_timeout=3;
server 192.168.1.1:82 weight=1 max_fails=2 fail_timeout=3;
}
location ~* .(jpg|jpeg|gif|png){
proxy_pass http://servername;
}
memcache
//安裝memcache擴展
cd memcache
/usr/local/php/bin/phpize
./configure --wit-hphp-config=/usr/local/php/bin/php-config
make && make install
//把擴展添加到php.ini裏
vim /usr/local/php/lib/php.ini
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/memcache.so
//重啓PHP
location \ {
set $memcached_key "$uri";
memcached_pass 127.0.0.1:11211;
error_page 404 /callback.php;
}