优化到极致的 LNMP 架构(nginx 终极优化)

优化到极致的 LNMP 架构

1 优化 Linux 系统

[19:33:08 root@yun ~]#cat /etc/sysctl.conf 

kernel.msgmax = 65536
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

# 使其生效
[19:37:10 root@yun ~]#sysctl -p

2 究极优化 nginx

[19:37:18 root@yun ~]#cat /apps/nginx/conf/nginx.conf
user  nginx;
worker_processes  1;
worker_priority -20;
worker_rlimit_nofile 65536;
error_log  /var/log/nginx/error.log error;
events {
    worker_connections  204800;
    accept_mutex on;
    multi_accept on;
    use epoll;
    }

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  /var/log/nginx/access.log  main;

    sendfile        on;
    tcp_nopush     on; 
    tcp_nodelay   off; 
    keepalive_timeout  65 65;
    gzip  on; 
    gzip_comp_level 5;
    gzip_disable "MSIE [1-6]\."
    gzip_min_length 1k;
    gzip_buffers 16 8k;
    gzip_http_version 1.0;
    gzip_types text/plain application/javascript application/x-javascript text/cssapplication/xml text/javascript application
    /x-httpd-php image/jpeg
    gzip_vary on;

 server_tokens off;

  server {
  listen       80;
  server_name  www.1717zgy.com *.www.1717zgy.com;
  charset utf-8;
  
  location / {
    error_page   500 502 503 504 404  /50x.html;
    root  /apps/nginx/html/wordpress;
    index index.php index.html index.htm;
  aio on;
  }

  location = /50x.html {
    root   html;
    }

  location ~ \.php$ {
    root          /apps/nginx/html/wordpress;
    fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
   }
 }
}

3 终极优化 php

这是根据自身服务器来优化。

pm.max_children = 50         #静态方式下开启的php-fpm进程数量,在动态方式下他限定php-fpm的最大进程数 
pm.start_servers = 10        #动态模式下初始进程数,必须大于等于pm.min_spare_servers和小于等于pm.max_children的值。  
pm.min_spare_servers = 10    #最小空闲进程数(最小和最大空闲进程数他们是 2 的倍数关系)
pm.max_spare_servers = 20    #最大空闲进程数 
pm.max_requests = 50000      #进程累计请求回收值,会回收并重新生成新的子进程
点赞