jjfjjj 发表于 2018-11-12 09:32:58

nginx.conf和php-fpm.conf配置文件详解

  # > /usr/local/nginx/conf/nginx.conf
  # vi /usr/local/nginx/conf/nginx.conf
  user nobody nobody;
  worker_processes 2;
  error_log /usr/local/nginx/logs/nginx_error.log crit;
  pid /usr/local/nginx/logs/nginx.pid;
  worker_rlimit_nofile 51200;
  events
  {
  use epoll;
  worker_connections 6000;
  }
  http
  {
  include mime.types;
  default_type application/octet-stream;
  server_names_hash_bucket_size 3526;
  server_names_hash_max_size 4096;
  log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
  '$host "$request_uri" $status'
  '"$http_referer" "$http_user_agent"';
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 30;
  client_header_timeout 3m;
  client_body_timeout 3m;
  send_timeout 3m;
  connection_pool_size 256;
  client_header_buffer_size 1k;
  large_client_header_buffers 8 4k;
  request_pool_size 4k;
  output_buffers 4 32k;
  postpone_output 1460;
  client_max_body_size 10m;
  client_body_buffer_size 256k;
  client_body_temp_path /usr/local/nginx/client_body_temp;
  proxy_temp_path /usr/local/nginx/proxy_temp;
  fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
  fastcgi_intercept_errors on;
  tcp_nodelay on;
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 8k;
  gzip_comp_level 5;
  gzip_http_version 1.1;
  gzip_types text/plain application/x-javascript text/css text/htm application/xml;
  server
  {
  listen 80;
  server_name localhost;
  index index.html index.htm index.php;
  root /usr/local/nginx/html;
  location ~ \.php$ {
  include fastcgi_params;
  fastcgi_pass unix:/tmp/php-fcgi.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
  }
  }
  }
  配置解释
  # 指定Nginx的worker进程运行用户以及用户组
  user nobody nobody;
  # 指定Nginx要开启的进程数,设置为CPU的总核数
  worker_processes 2;
  # 指定Nginx全局错误日志路径与级别,日志级别有: debug、info、notice、warn、error、crit
  # 其中debug输出日志最为详细,crit输入日志最少 ;
  error_log /usr/local/nginx/logs/nginx_error.log crit;
  # 指定进程id的存储文件位置
  pid /usr/local/nginx/logs/nginx.pid;
  # 一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
  # ulimit -n 查看系统限制
  worker_rlimit_nofile 51200;
  # 设定Nginx的工作模式及连接数上限
  events
  # 指定工作模式为epoll,工作模式有select、poll、kqueue、epoll、rtsig和/dev/poll,
  # 其中select和poll是标准的工作模式,kqueue和qpoll是高效的工作模式;epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
  use epoll;
  # 单个进程最大连接数(最大连接数=连接数*进程数)
  worker_connections 6000;
  # 文件扩展名与文件类型映射表
  include mime.types;
  # 默认文件类型为二进制流
  default_type application/octet-stream;
  # 服务器名字的hash表大小
  server_names_hash_bucket_size 3526;
  # 服务器名字的hash表的最大量
  server_names_hash_max_size 4096;
  # 指定Nginx日志的输出格式,其中combined_realip为自定义的日志名字
  log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' '$host "$request_uri" $status' '"$http_referer" "$http_user_agent"';
  # 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
  sendfile on;
  # 用于防止网络堵塞
  tcp_nopush on;
  tcp_nodelay on;
  # 长连接超时时间,单位为秒
  keepalive_timeout 65;
  # 设置客户端请求头读取超时时间
  client_header_timeout 10;
  # 设置客户端请求主题2超时时间
  client_body_timeout 10;
  # 指定响应客户端的超时时间
  send_timeout 10;
  # 为每个请求分配的内存池,内存池用于小配额内存块,如果一个块大于内存池 或者大于分页大小,那么它将被分配到内存池之外,如果位于内存池中较小的分配量没有足够的内存,那么将分配一个相同内存池大小的新块,这个指令仅有相当有限的效果
  connection_pool_size 256;
  request_pool_size 4k;
  # 指定来自客户端请求头的大小
  client_header_buffer_size 1k;
  # 指定客户端请求中较大的请求头的最大缓存最大数量和大小
  large_client_header_buffers 8 4k;
  # 输出缓存大小
  output_buffers 4 32k;
  postpone_output 1460;
  # 指令指定允许客户端连接的最大请求主体大小
  client_max_body_size 10m;
  # 这个指令可以指定连接请求主体的缓冲区大小。
  client_body_buffer_size 256k;
  # 指定连接请求主体试图写入的临时文件路径
  client_body_temp_path /usr/local/nginx/client_body_temp;
  # 反向代理临时存储目录
  proxy_temp_path /usr/local/nginx/proxy_temp;
  FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。
  # 为Nginx配置FastCGI缓存指定一个路径
  fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
  # 如果这个选项没有设置,即使创建了404.html和配置了error_page也没有效果
  fastcgi_intercept_errors on;
  # 启用压缩
  gzip on;
  # 最小压缩文件大小
  gzip_min_length 1k;
  # 压缩缓冲区
  gzip_buffers 4 16k;
  # 压缩等级
  gzip_comp_level 5;
  # 压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
  gzip_http_version 1.1;
  # 要压缩的类型
  gzip_types text/plain application/x-javascript text/css text/htm application/xml;
  # 开启虚拟配置目录
  include vhosts/*.conf;
  虚拟主机配置解释:
  server:定义虚拟主机开始的关键字
  listen:指定虚拟主机的服务端口
  server_name:指定IP地址或域名,多个域名之间用空格分开
  index:设定访问的默认首页地址
  root:指定虚拟主机的网页根目录
  charset:设置网页的默认编码格式
  include fastcgi_params:开启fastcgi
  fastcgi_pass:指定fastcgi监听方式:1、sock方式监听;2、TCP/IP方式监听
  fastcgi_index:指定fastcgi默认起始页
  fastcgi_param SCRIPT_FILENAME:设定fastcgi监听的目录
  php-fpm.conf配置文件
  
  pid = /usr/local/php/var/run/php-fpm.pid
  error_log = /usr/local/php/var/log/php-fpm.log
  
  listen = /tmp/php-fcgi.sock
  user = php-fpm
  group = php-fpm
  listen.owner = nobody//和后面的nginx的一致
  listen.group = nobody // 同上
  pm = dynamic
  pm.max_children = 50
  pm.start_servers = 20
  pm.min_spare_servers = 5
  pm.max_spare_servers = 35
  pm.max_requests = 500
  rlimit_files = 1024
  配置多个pool
  
  ...
  ...
  
  ...
  ...
  ...
  
  ...
  ...
  ...
  :全局配置
  pid:指定进程id文件
  error_log:指定错误日志文件
  :指定pool名字
  listen:指定监听方式与Nginx配置中一致
  user:启动进程的用户
  group:启动进程的用户组
  listen.owner:当监听方式设定为sock时,listen.owner为sock文件的所属主
  listen.group:当监听方式设定为sock时,listen.owner为sock文件的所属组
  pm = dynamic:
  如何控制子进程,选项有static和dynamic。
  如果选择static,则由pm.max_children指定固定的子进程数。
  如果选择dynamic,则由下开参数决定:
  pm.max_children ,子进程最大数
  pm.start_servers ,启动时的进程数
  pm.min_spare_servers ,保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程
  pm.max_spare_servers ,保证空闲进程数最大值,如果空闲进程大于此值,此进行清理
  对于专用服务器,pm可以设置为static。
  slowlog:慢日志配置
  request_slowlog_timeout:慢日志超时时间
  php_admin_value:配置open_basedir
  参考:
  http://8802265.blog.51cto.com/8792265/1651899
  http://mylinuxlife.blog.51cto.com/4706737/1651705
  扩展学习:
  nginx.conf 配置详解 http://www.ha97.com/5194.html
  nginx rewrite 知识汇总 http://blog.c1gstudio.com/archives/434

页: [1]
查看完整版本: nginx.conf和php-fpm.conf配置文件详解