wxin 发表于 2018-11-14 12:39:41

LNMP-源码 nginx ,keepalived

  每次安装源码都会有一些忘记的模块开启;和需要插件;
  上次完全做已经多年了,在总结下;
  nginx1.10.0
  安装目录 /usr/local/nginx
  *为支持rewrite安装pcre
  # yum install pcre* -y
  # yum installl openssl*
  yum install gcc gcc-c++ gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel -y
  yum install –y autoconf automake imake libxml* expat-devel cmake libaio libaio-devel bzr bison libtool* ncurses5-devel zlib*
  # tra -xf nginx-1.10.1.tar.gz
  # cd nginx-1.10.1
  # ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
  # yum install make
  # make
  # make install
  查看安装信息;
  # /usr/local/nginx/sbin/nginx -V
  启动 nginx
  # /usr/local/nginx/sbin/nginx
  直接使用curl命令来读取web信息
  # curl -s http://localhost | grep nginx.com
  # lsof -i :80
  # /usr/local/nginx/sbin/nginx -s stop
  # lsof -i :80
  重启

  # /usr/local/nginx/sbin/nginx -s>  links 127.0.0.1
  Nginx高并发的优化配置:
  内核参数的优化 net.ipv4.tcp_max_tw_buckets = 6000
  timewait的数量,默认是180000。
  net.ipv4.ip_local_port_range = 1024    65000
  允许系统打开的端口范围。
  net.ipv4.tcp_tw_recycle = 1
  启用timewait快速回收。
  net.ipv4.tcp_tw_reuse = 1
  开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接。
  net.ipv4.tcp_syncookies = 1
  开启SYN Cookies,当出现SYN等待队列溢出时,启用cookies来处理。
  net.core.somaxconn = 262144
  web应用中listen函数的backlog默认会给我们内核参数的
  net.core.somaxconn限制到128,
  而nginx定义的NGX_LISTEN_BACKLOG默认为511,所以有必要调整这个值。
  net.core.netdev_max_backlog = 262144
  每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目。
  net.ipv4.tcp_max_orphans = 262144
  系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤儿连接将即刻被复位并打印出警告信息。这个限制仅仅是为了防止简单的DoS***,不能过分依靠它或者人为地减小这个值,更应该增加这个值(如果增加了内存之后)。
  net.ipv4.tcp_max_syn_backlog = 262144
  记录的那些尚未收到客户端确认信息的连接请求的最大值。对于有128M内存的系统而言,缺省值是1024,小内存的系统则是128。
  net.ipv4.tcp_timestamps = 0
  时间戳可以避免序列号的卷绕。一个1Gbps的链路肯定会遇到以前用过的序列号。时间戳能够让内核接受这种"异常"的数据包。这里需要将其关掉。
  net.ipv4.tcp_synack_retries = 1
  为了打开对端的连接,内核需要发送一个SYN并附带一个回应前面一个SYN的ACK。也就是所谓三次握手中的第二次握手。这个设置决定了内核放弃连接之前发送SYN+ACK包的数量。
  net.ipv4.tcp_syn_retries = 1
  在内核放弃建立连接之前发送SYN包的数量。
  net.ipv4.tcp_fin_timeout = 1
  如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间。对端可以出错并永远不关闭连接,甚至意外当机。缺省值是60秒。2.2 内核的通常值是180秒,你可以按这个设置,但要记住的是,即使你的机器是一个轻载的WEB服务器,也有因为大量的死套接字而内存溢出的风险,FIN- WAIT-2的危险性比FIN-WAIT-1要小,因为它最多只能吃掉1.5K内存,但是它们的生存期长些。
  net.ipv4.tcp_keepalive_time = 30
  当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时。
  nginx + keepalived 负载均衡配置参考;
  # cat /usr/local/nginx/conf/nginx.conf
  #usernobody;
  worker_processes24;## 根据实际情况配置
  events {
  worker_connections204800;
  }
  http {
  include       mime.types;
  default_typeapplication/octet-stream;
  sendfile      on;
  keepalive_timeout30;
  tcp_nopush      on;
  gzipon;
  gzip_min_length   10k;
  gzip_buffers   16 128k;
  gzip_http_version1.1;
  gzip_comp_level 6;
  gzip_vary on;
  gzip_types text/plaintext/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  gzip_proxied any;
  gzip_disable "MSIE \.";
  server_names_hash_bucket_size 1024;
  client_header_buffer_size 1024k;
  large_client_header_buffers 4 1024k;
  client_max_body_size 3000m;
  proxy_redirect off;
  proxy_set_header Host $http_host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_connect_timeout 90;
  proxy_send_timeout 90;
  proxy_read_timeout 90;
  proxy_buffer_size 128k;
  proxy_buffers 4 128k;
  proxy_busy_buffers_size 128k;
  proxy_temp_file_write_size 128k;
  upstream server # 自定义
  {
  #   server ip:port;
  server 192.168.1.11:80;
  server 192.168.1.12:80;
  server 192.168.1.13:80;
  server 192.168.1.14:80;
  server 192.168.1.15:80;
  }
  server {
  listen       80;
  server_namelocalhost;
  location / {
  root   html;
  indexindex.html index.htm;
  proxy_pass http://server; # upstream 自定义的
  proxy_set_header Host $host:80;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For$remote_addr;
  proxy_set_header Via "nginx";
  break;
  }
  error_page   500 502 503 504/50x.html;
  location = /50x.html {
  root   html;
  }
  location ~ .*\.(jpg|js|html|mp3|gif|jpeg|png|bmp|swf|ico|css)$
  {
  expires      7d;
  }
  location ~ .*\.(js|css)?$
  {
  expires      24h;
  }
  }
  }

页: [1]
查看完整版本: LNMP-源码 nginx ,keepalived