开发者社区> 问答> 正文

加速吧 - Nginx 的优化收集



前言


由于目前 LNMP 环境很火爆而且也很简单,一键包和安装教程也都很丰富,所以我们这里先讲讲 Nginx 的优化。

内容

  1. 版本越高越好,不要现在已经 1.10.x 的时代了,你还在 1.0.x 徘徊,偶数位是稳定版例如:1.8,奇数是测试版,例如:1.9
  2. 使用 socket 方式连接 php-fpm
  3. 如果 Nginx 用于反代无论是类似内网 Tomcat、Node.js 还是外网谷歌不要简单几句反代代码,去搜索一下,还可以加缓存什么的优化性能
  4. 有实力的就申请一个证书上 HTTP/2 ,获得更佳的阅读体验和安全性还有速度
  5. 开启 Gzip 压缩


衍生版


Nginx 方面淘宝、Cloudflare 都是是利用大户,因此它们的代码贡献也是不小,对于 Nginx 的贡献也是不小,而且它们也有自己的 Nginx 修改版。
  1. Tengine |淘宝和搜狗能企业一起干出来的,非常不错的衍生版,可玩性大。
  2. Openresty | 前淘宝员工,现 Cloudflare 员工章亦春的衍生版,功能强大。
  3. SEnginx |  基于 Tengine 的安全衍生版




来自: https://www.vobe.io/378

展开
收起
妙正灰 2015-10-14 22:11:17 10625 0
3 条回答
写回答
取消 提交回答
  • 一个程序员,欢迎骚扰!!!
    O(∩_∩)O哈哈~  有空尝试一下
    2015-10-16 20:37:38
    赞同 展开评论 打赏
  • 谷歌的链接无法访问。
    2015-10-14 23:22:53
    赞同 展开评论 打赏
  • 解决方案工程师,负责为企业规划上云迁移方案和云上架构设计,在网站建设开发和云计算领域有多年经验,专注于Linux平台的系统维护以及应用部署。致力于以场景化的方式让云计算,用更加通俗易懂的方式让更多人体验云计算,让云端的计算更质朴的落地。
    内核优化


    编辑   /etc/sysctl.conf
    net.ipv4.ip_forward = 0
    net.ipv4.conf.default.rp_filter = 1
    net.ipv4.conf.default.accept_source_route = 0
    kernel.sysrq = 0
    kernel.core_uses_pid = 1
    net.ipv4.tcp_syncookies = 1
    kernel.msgmnb = 65536
    kernel.msgmax = 65536
    kernel.shmmax = 68719476736
    kernel.shmall = 4294967296
    net.ipv4.tcp_max_tw_buckets = 6000
    net.ipv4.tcp_sack = 1
    net.ipv4.tcp_window_scaling = 1
    net.ipv4.tcp_rmem = 4096 87380 4194304
    net.ipv4.tcp_wmem = 4096 16384 4194304
    net.core.wmem_default = 8388608
    net.core.rmem_default = 8388608
    net.core.rmem_max = 16777216
    net.core.wmem_max = 16777216
    net.core.netdev_max_backlog = 262144
    net.core.somaxconn = 262144
    net.ipv4.tcp_max_orphans = 3276800
    net.ipv4.tcp_max_syn_backlog = 262144
    net.ipv4.tcp_timestamps = 0
    net.ipv4.tcp_synack_retries = 1
    net.ipv4.tcp_syn_retries = 1
    net.ipv4.tcp_tw_recycle = 1
    net.ipv4.tcp_tw_reuse = 1
    net.ipv4.tcp_mem = 94500000 915000000 927000000
    net.ipv4.tcp_fin_timeout = 1
    net.ipv4.tcp_keepalive_time = 30
    net.ipv4.ip_local_port_range = 1024 65000


    再输入 /sbin/sysctl -p 使其生效

    Conf 设置

    worker_processes 2 # 2 就是服务器的核心数
    worker_cpu_affinity 01 10 # 填法和服务器的线程数相关,一般云服务器/vps 线程和核心数是一致的,具体写法不展开。


    如果是 tengine 的话,后面直接填 auto 即可。


    worker_connections  #用高效的event驱动,可以获得最大性能

    其他的参数最好根据你的服务器配置进行调整,以避免 502 的产生。

    TCP 优化



    http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 60;


    第一行的 sendfile 配置可以提高 Nginx 静态资源托管效率。sendfile 是一个系统调用,直接在内核空间完成文件发送,不需要先 read 再 write,没有上下文切换开销。
    TCP_NOPUSH 是 FreeBSD 的一个 socket 选项,对应 Linux 的 TCP_CORK,Nginx 里统一用 tcp_nopush 来控制它,并且只有在启用了 sendfile 之后才生效。启用它之后,数据包会累计到一定大小之后才会发送,减小了额外开销,提高网络效率。
    TCP_NODELAY 也是一个 socket 选项,启用后会禁用 Nagle 算法,尽快发送数据,可以节约 200ms。Nginx 只会针对处于 keep-alive 状态的 TCP 连接才会启用 tcp_nodelay。

    -------------------------



    Socket 连接 php-fpm


    修改 php-fpm.conf

    listen = 127.0.0.1:9000
    改为
    listen = /dev/shm/php-cgi.sock

    修改 nginx.conf
    location ~ .*\.(php|php5)?$ {
    #fastcgi_pass 127.0.0.1:9000;
    fastcgi_pass unix:/dev/shm/php-cgi.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    }


    事后不要忘记重启就好。

    反代优化


    Nginx 安装时,或者升级时编译 ngxcachepurge 组件
    以反代 Node.js 应用 Ghost 为例:
    server {  
       server_name domain.com;
       add_header X-Cache $upstream_cache_status;
       location / {
            proxy_cache STATIC;
            proxy_cache_valid 200 30m;
            proxy_cache_valid 404 1m;
            proxy_pass http://127.0.0.1:2368;
            proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
            proxy_ignore_headers Set-Cookie;
            proxy_hide_header Set-Cookie;
            proxy_hide_header X-powered-by;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            expires 10m;
        }
        location /content/images {
            alias /path/to/ghost/content/images;
            access_log off;
            expires max;
        }
        location /assets {
            alias /path/to/ghost/content/themes/uno-master/assets;
            access_log off;
            expires max;
        }
        location /public {
            alias /path/to/ghost/core/built/public;
            access_log off;
            expires max;
        }
        location /ghost/scripts {
            alias /path/to/ghost/core/built/scripts;
            access_log off;
            expires max;
        }
        location ~ ^/(?:ghost|signout) {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $http_host;
            proxy_pass http://127.0.0.1:2368;
            add_header Cache-Control "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0";
        }

    }




    HTTP/2 + 优化


    因为 Nginx 稳定支持HTTP/2 的 1.10 版本还没有出,所以暂时不写。
    可以先参考 SPDY:或许是 Nginx 下 SPDY 配置最实际的教程

    开启 Gzip


    对于文本文件,在服务端发送响应之前进行 GZip 压缩也很重要,通常压缩后的文本大小会减小到原来的 1/4 - 1/3。
    http {
    gzip on;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_min_length 1000;
    gzip_proxied any;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
    ... ...
    }



    PageSpeed




    谷歌推出的很好很强大的优化软件,由于篇幅有限,所以请自行搜索安装教程


    来自: https://www.vobe.io/378
    2015-10-14 22:12:26
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
《Nginx 代理系统常用手册》 立即下载
CentOS Nginx PHP JAVA 多语言镜像使用手 立即下载
CentOS Nginx PHP JAVA多语言镜像使用手册 立即下载