Nginx   服务器

nginx  是运行于后台的计算机应用程序,是作为守护进程运行的程序。
1、启动和停止 nginx 守护进程 , nginx -s 向进程发送信号(signal)。

nginx -s stop 立即停止守护进程  
nginx -s quit 温和的停止守护进程
nginx -s reopen 重新打开日志文件
nginx -s reload 重新载入配置文件 
首先会解析和确认配置文件,如果配置文件无效,不管提交什么命令都会失败。危机情况可使用killall nginx
 2、测试配置文件
    检测配置文件的完整性
$ /usr/local/nginx/sbin/nginx -t 
    测试临时配置文件
$ ./nginx -t -c /home/wzq/nginxTest.conf
 确定它作为nginx配置文件的有效性,再确定新的有效配置文件,然后重新载入服务器配置文件
$ cp /home/wzq/nginxTest.conf /usr/local/nginx/conf/nginx.conf 
cp: erase 'nginx.conf'  ? yes
$ ./nginx -s reload
3 、nginx.conf 完整配置事例
# 使用的用户和用户组
# user nobody;
# 指定工作衍生进程数,(一般等于cpu 的总核数或intel超线程cpu总核数的两倍,由于最新一代intel cpu 酷睿i7  至强 xeon 系类cpu 支持最新的intel 多线程技术,拥有
指令分支预测技术,很短的流水线,高带宽及高容量三级高速缓存的优势,可以将超线程技术的功效发挥到极致 ,
激发出每个核心的运算潜能,将从而提升系统的资源利用率,四核心处理器加进了超线程技术,处理器同时支持处理八个线程的工作。例如 四核8线程i7cpu 建议设为 8 )

worker_processes 8;

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  on;

    server {
        listen       80;
        server_name  wangmeng2111.360.cn;                        # 虚拟主机的域名
        root         /Users/wzq/Sites/360/src_V2/front/public;    # 虚拟主机的绝对路径
        access_log   logs/host.access.log  main;                     # 虚拟主机的日志文件名和路径
        location / {
            index index.html index.htm index.php;                    # 默认读取的文件名
        }

        location ~ \.php$ {
           fastcgi_pass   127.0.0.1:9000;                                    # fastcgi ip 端口
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  # fastcgi  参数
            include        fastcgi_params; 
     }
    }
    server {
        listen       80;
        server_name  localhost;
        root   /Users/wzq/Sites;
        #charset koi8-r;
        access_log  logs/host.access.log  main;

        location / {
            index  index.html index.htm index.php;
        }

        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$ {
             fastcgi_pass   127.0.0.1:9000;
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME  $document_root$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;
        #}
    }

}
4、 php Fast-CGI 安装
  php FastCGI 是一种新的更快速的cgi程序,由php-fpm php fastCGI 进程管理 (php fastCGI Process Manager)进程管理器 控制,自从 PHP 5.3.3 版以后就自带了php-FPM 程序,建议小伙伴们安装php 5.3.3 以后的 php版本
php-FPM 启动:
$ php-FPM
查看 php-FPM 进程: 
 ps -aux | grep php-fpm
停止php-fpm
$ killall php-fpm 

5、nginx 性能测试工具
     nginx 性能测试工具:
  (1)httperf  www.hpl.hp.com 、 
  (2)autobench、
  (3)OpenWebload   openwebload.sourceforge.net
 

6、nginx Rewrite 重写规则
    Rewrite 重写规则 参考的是 pcre 正则函数库,和php的 preg 正则 语法类似
    http://www.qg66.cn/serach.php/wzq
    http://www.qg66.cn/serach.php?a=wzq
    rewrite  ^/search/(.*)$  /serach.php?a=$1?;
. 表示 除了换行符 以外的所有字符, * 表示 0次或多次 
 ()表示分组
    http://www.qg66.cn/serach.php/wzq/handsome boy
    http://www.qg66.cn/serach.php?a=wzq&b=handsome boy
    rewrite  ^/search/(.*)(.*)$  /serach.php?a=$1&b=$2?;