Nginx 配置

  • nginx.conf

基础配置

  • ngx_core_module 核心
  1. # 配置用户和用户组
  2. user nginx nginx;
  3. # 工作进程数,建议设置为CPU的总核数
  4. worker_processes 4;
  5. # 全局错误日志定义类型,日志等级从低到高依次为: debug | info | notice | warn | error | crit
  6. error_log logs/error.log info;
  7. # 记录主进程ID的文件(不要动)
  8. #pid nginx.pid;
  9. # 一个进程能打开的文件描述符最大值,理论上该值因该是最多能打开的文件数除以进程数. 但是由于nginx负载并不是完全均衡的,
  10. # 所以这个值最好等于最多能打开的文件数. 执行 sysctl -a | grep fs.file 可以看到 linux 文件描述符.
  11. worker_rlimit_nofile 65535;
  12. # 工作模式与连接数上限
  13. events {
  14. # uname -a 工作模式,linux2.6版本以上用 epoll
  15. # 比如 select、poll、epoll 只能设置在 events 模块中设置
  16. use epoll;
  17. # 单个进程允许的最大连接数
  18. worker_connections 65535;
  19. # multi_accept 告诉 nginx 收到一个新连接通知后接受尽可能多的连接,默认是 on
  20. # 设置为 on 后,多个 worker 按串行方式来处理连接,也就是一个连接只有一个 worker 被唤醒,其他的处于休眠状态,
  21. # 设置为 off 后,多个 worker 按并行方式来处理连接,也就是一个连接会唤醒所有的 worker,直到连接分配完毕,没有取得连接的继续休眠。
  22. # 结论: 当你的服务器连接数不多时,开启这个参数会让负载有一定的降低,但是当服务器的吞吐量很大时,为了效率,可以关闭这个参数。
  23. multi_accept off;
  24. }

变量表

  1. $args | 请求中的参数;
  2. $binary_remote_addr | 远程地址的二进制表示
  3. $body_bytes_sent | 已发送的消息体字节数
  4. $content_length | HTTP请求信息里的"Content-Length";
  5. $content_type | 请求信息里的"Content-Type";
  6. $document_root | 针对当前请求的根路径设置值;
  7. $document_uri | $uri相同;
  8. $host | 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名;
  9. $hostname |
  10. $http_cookie | cookie 信息
  11. $http_post |
  12. $http_referer | 引用地址
  13. $http_user_agent | 客户端代理信息
  14. $http_via | 最后一个访问服务器的Ip地址.
  15. $http_x_forwarded_for | 相当于网络访问路径.
  16. $is_args |
  17. $limit_rate | 对连接速率的限制;
  18. $nginx_version |
  19. $pid |
  20. $query_string | $args相同;
  21. $realpath_root |
  22. $remote_addr | 客户端地址;
  23. $remote_port | 客户端端口号;
  24. $remote_user | 客户端用户名,认证用;
  25. $request | 用户请求
  26. $request_body |
  27. $request_body_file | 发往后端的本地文件名称
  28. $request_completion |
  29. $request_filename | 当前请求的文件路径名
  30. $request_method | 请求的方法,比如"GET""POST"等;
  31. $request_uri | 请求的URI,带参数;
  32. $scheme | 所用的协议,比如http或者是https,比如rewrite^(.+)$$scheme://example.com$1redirect;
  33. $sent_http_cache_control |
  34. $sent_http_connection |
  35. $sent_http_content_length |
  36. $sent_http_content_type |
  37. $sent_http_keep_alive |
  38. $sent_http_last_modified |
  39. $sent_http_location |
  40. $sent_http_transfer_encoding |
  41. $server_addr | 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费);
  42. $server_name | 请求到达的服务器名;
  43. $server_port | 请求到达的服务器端口号;
  44. $server_protocol | 请求的协议版本,"HTTP/1.0""HTTP/1.1";
  45. $uri | 请求的URI,可能和最初的值有不同,比如经过重定向之类的.