安装nginx

下载安装包:wget http://nginx.org/download/nginx-1.16.1.tar.gz
解压:tar -zxvf nginx-1.16.1.tar.gz
进入解压后的nginx目录,执行./configure --prefix=/usr/local/nginx --add-module=/home/chuyf/Downloads/ngx_http_consistent_hash-master --with-http_stub_status_module
--prefix=/usr/local/nginx #安装在/usr/local/nginx目录
--add-module=/home/chuyf/Downloads/ngx_http_consistent_hash-master #添加哈希一致性模块,zip下载地址:https://github.com/replay/ngx_http_consistent_hash/archive/master.zip
--with-http_stub_status_module #添加http请求统计模块
缺少pcre:
yum install pcre
yum install pcre-devel
成功之后执行make && make install

kill -INT pid: 直接关闭进程
kill -QUIT pid: 上次请求完毕后关闭进程
kill -HUP pid: 上次请求完毕后关闭旧的工作进程,开启新的工作进程
kill -USR1 pid: 重读日志

日志备份:
LOGPATH=/usr/local/nginx/logs/access.log
BASEPATH=/data/$(date -d yesterday +%Y-%m)

mkdir -p $BASEPATH
mv $LOGPATH $BASEPATH/access_$(date -d yesterday +%Y年%m月%d日%H时%m分%S秒).log
touch $LOGPATH
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

linux定时任务:
crontab根据用户显示
crontab -e : 编辑定时任务
crontab -l : 查看定时任务
crontab -r : 删除定时任务

nginx文档: nginx.org/docs

location:
精准匹配(=),匹配成功后直接返回
一般匹配(/),多个匹配时记录最长匹配,然后正则匹配(~|*),正则匹配成功后直接返回,否则返回最长的一般匹配

rewrite:
常用的命令
if  (条件) {}  设定条件,再进行重写
set #设置变量
return #返回状态码
break #跳出rewrite
rewrite #重写
#if ($request_method = GET) {
#    return 405;
# }
if ($http_user_agent ~ MSIE) {
    #return 404;
    rewrite ^(.*)$ /ie.html break;
}

gzip(压缩提升速度):
gzip on;#开启gzip压缩
gzip_buffers 32 4k;#压缩多少个块,每个块大小
gzip_comp_level 6;#压缩级别
gzip_min_length 4000;#最小压缩字节,小于指定值不在压缩
gzip_types text/css text/xml application/x-javascript;#压缩类型

expires(设置过期时间,实际日期与服务器日期不一致时可能会导致缓存失效):
location ~* .(jpg|jpeg|gif|png) {
    root html;
   expires 1d;
}

304也是一种很好的缓存手段:
原理:服务器响应文件内容时,同时响应etag标签(内容的签名,内容变,标签也变)和last_modified_since,浏览器下次请求时,头信息发送这两个标签,服务器检测文件有没有发生变化,如无。直接头信息返回etag,last_modified_since,浏览器知道内容无改变,于是直接调用本地缓存。这个过程也调用了服务器,但是传输的内容极少。对于变化周期较短的,如静态html,js,css,比较适用于这种方式。

代理、负载均衡:

upstream imgserver {
    server l92.168.137.130:81 weigth=1 max_fails=2 fail_timeout=3;
    server l92.168.137.131:81 weigth=1 max_fails=2 fail_timeout=3;
}

location ~* .(jpg|jpeg|gif|png) {
    proxy_set_header X-Forwarded-For $remote_addr;
   proxy_pass http://imgserver;
}

配置php:
location ~ .php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
}

nginx连接memchached:
upstream mcserver {
    consistent_hash $request_uri;#哈希一致性
    server 127.0.0.1:11211;
    server 127.0.0.1:11212;
    server 127.0.0.1:11213;
}
location / {
    set $memcached_key "$uri";#根据uri获取memcache中的数据
    #memcached_pass 127.0.0.1 11211;
    memcached_pass mcserver;#负载
    error_page 404 /callback.php;#404回调
}

注:php配置文件中需要指定memcache.hash_strategy=consistent以保证哈希一致性
callback.php:
<?php
//获得uri,用来当key
$uri = $_SERVER['REQUEST_URI'];
//获取uid
$uid = substr($uri, 5, strpos($uri, '.') - 5);
//php连接memcache
$mem = new Memcache();
#$mem->connect('localhost', 11211);
$mem->addServer('127.0.0.1', 11211);
$mem->addServer('127.0.0.1', 11212);
$mem->addServer('127.0.0.1', 11213);
//连接数据库,查询并写入memcached
$conn = mysql_connect('localhost', 'root', '');
$sql = 'use test';
mysql_query($sql, $conn);
$sql = 'set names utf8';
mysql_query($sql, $conn);
$sql = 'select * from user where uid='.$uid;
$rs = mysql_query($sql, $conn);

echo 'from mysql query<br/>';

$user = mysql_fetch_assoc($rs);
if (empty($user)) {
     echo 'no this user';
} else {
    //print_r($user);
    $html = 'h1'.$user['uname'].'h1';
    echo $html;

    $mem-add($uri, $html, 0, 300);
    $mem->close();
}

压力测试:ab -c 2000 -n 100000 http://192.168.137.127/index.html
-c 并发次数
-n 总请求次数

并发优化:
socket:   
        nginx: 子进程允许打开的连接(worker_connections)
        系统:    最大连接数(somaxconn)
            加快tcp连接的回收(tcp_tw_recycle)
            空的tcp是否允许回收利用(tcp_tw_reuse)
            SYN攻击(tcp_syncookies)
文件:
        nginx: 子进程允许打开的文件(worker_rlimit_nofile)
        系统:ulimit -n
linux系统参数:
临时修改:
echo 50000 > /proc/sys/net/core/somaxconn #定义了系统中每一个端口最大的监听队列的长度,这是个全局的参数,默认值为128
echo 1 > /proc/sys/net/core/ipv4/tcp_tw_recycle #TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭
echo 1 > /proc/sys/net/core/ipv4/tcp_tw_reuse #允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭
echo 0 > /proc/sys/net/core/ipv4/tcp_syncookies #出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭
永久修改:
vi /etc/sysctl.conf
net.core.somaxconn = 50000
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 0
sysctl -p

临时修改打开文件限制:
ulimit -n 50000
永久修改:
vi /etc/security/limits.conf
* soft noproc 11000
        * hard noproc 11000
        * soft nofile 4100
        * hard nofile 4100
       说明:* 代表针对所有用户
            noproc 是代表最大进程数
            nofile 是代表最大文件打开数

nginx参数:
worker_rlimit_nofile 10000; #最大打开文件数量
worker_connections 10240; #单进程最大打开的连接数

keepalive_timeout 0;#连接存活时间 http1.1

原文地址:https://www.cnblogs.com/cyf18/p/14289129.html