Nginx 性能参数优化

  1. user www www;
  2.  
  3. # ginx要开启的进程数 一般等于cpu的总核数,没必要开那么多,1个nginx内存消耗10兆左右
  4. worker_processes 4;
  5.  
  6. # 为每个进程分配cpu,上例中将4 个进程分配到4个cpu,当然可以写多个,或者将一 个进程分配到多个cpu。
  7. worker_cpu_affinity 00000001 00000010 00000100 00001000;
  8.  
  9. # 每个nginx进程打开文件描述符最大数目 配置要和系统的单进程打开文件数一
  10. # 致,linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应,应该填写65535
  11. # nginx调度时分配请求到进程并不是那么的均衡,假如超过会返回502错误。我这里写的大一点
  12. worker_rlimit_nofile 100000;
  13.  
  14. # 开启nginx错误日志
  15. error_log logs/error.log;
  16.  
  17. # 告诉nginx只能记录严重的错误
  18. #error_log logs/error.log notice;
  19. #error_log logs/error.log info;
  20.  
  21. #pid logs/nginx.pid;
  22.  
  23. events {
  24. # 每个工作进程允许最大的同时连接数(Maxclient = work_processes * worker_connections)
  25. # 默认1024
  26. worker_connections 65535;
  27. # 告诉nginx收到一个新连接通知后接受尽可能多的连接。
  28. multi_accept on;
  29. # 设置用于复用客户端线程的轮询方法。如果你使用Linux 2.6+,你应该使用epoll。
  30. # 如果你使用*BSD,你应该使用kqueue。
  31. # 值得注意的是如果你不知道Nginx该使用哪种轮询方法的话,它会选择一个最适合你操作系统的
  32. use epoll;
  33. }
  34.  
  35.  
  36. http {
  37. include mime.types;
  38. default_type application/octet-stream;
  39. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  40. '$status $body_bytes_sent "$http_referer" '
  41. '"$http_user_agent" "$http_x_forwarded_for"';
  42.  
  43. limit_req_zone $binary_remote_addr zone=allips:10m rate=20r/s;
  44.  
  45.  
  46. #
  47. #access_log logs/access.log main;
  48.  
  49. # 这个将为打开文件指定缓存,默认是没有启用的,max 指定缓存数量,建议和打开文件数一致,
  50. # inactive 是指经过多长时间文件没被请求后删除缓存
  51. open_file_cache max=204800 inactive=20s;
  52. # open_file_cache 指令中的inactive 参数时间内文件的最少使用次数,
  53. # 如果超过这个数字,文件描述符一直是在缓存中打开的,
  54. # 如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除
  55. open_file_cache_min_uses 1;
  56. # 这个是指多长时间检查一次缓存的有效信息
  57. open_file_cache_valid 30s;
  58.  
  59. # 并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的
  60. server_tokens off;
  61. # 磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)。
  62. # Pre-sendfile是传送数据之前在用户空间申请数据缓冲区
  63. sendfile on;
  64. # 告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送
  65. #tcp_nopush on;
  66. # 告诉nginx不要缓存数据,而是一段一段的发送,
  67. # 当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。
  68. #tcp_nodelay on;
  69.  
  70. upstream phpServer{
  71. server 172.20.17.210:9000 weight=1 max_fails=2 fail_timeout=3;
  72. server 172.20.17.211:9000 weight=1 max_fails=2 fail_timeout=3;
  73. }
  74.  
  75. # keepalive超时时间
  76. keepalive_timeout 65;
  77. client_max_body_size 2m;
  78.  
  79. # 不准许IP直接访问, 直接访问报500错误
  80. server {
  81. listen 80 default_server;
  82. server_name _;
  83. return 500;
  84. }
  85.  
  86. # 配置虚拟主机,过个server就复制多个
  87. server {
  88. listen 80;
  89. # 开启gzip压缩
  90. gzip on;
  91. gzip_min_length 1k;
  92. gzip_buffers 4 16k;
  93. #gzip_http_version 1.0;
  94. gzip_comp_level 2;
  95. gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
  96. gzip_vary off;
  97. gzip_disable "MSIE [1-6]\.";
  98. # 配置域名
  99. server_name www.xxxxx.com xxxxx.com;
  100. # 配置网站目录
  101. root /usr/local/nginx/html/xxxxx.com;
  102. # 只允许我们的域名的访问
  103. if ($host !~ ^(xxxxx.com|www.xxxxx.com|images.xxxxx.com)) {
  104. return 444;
  105. }
  106. # 配置域名重定向
  107. #if ($host != 'www.xxxxx.com' ) {
  108. # rewrite ^/(.*)$ http://www.xxxxx.com/$1 permanent;
  109. #}
  110. # 限制可用的请求方法
  111. if ($request_method !~ ^(GET|HEAD|POST)) {
  112. return 444;
  113. }
  114. # 如何拒绝一些User-Agents
  115. if ($http_user_agent ~* LWP::Simple|BBBike|wget) {
  116. return 403;
  117. }
  118. # 如何防止图片盗链
  119. location /images/ {
  120. valid_referers none blocked www.xxxxx.com xxxxx.com;
  121. if ($invalid_referer) {
  122. return 403;
  123. }
  124. }
  125. location / {
  126. # 配置rewrite
  127. if (!-e $request_filename) {
  128. rewrite ^(.*)/index.php?s=$1 last;
  129. break;
  130. }
  131. # include /usr/local/nginx/html/yphp/.htaccess;
  132. # rewrite ^/(.+)/(.+)[/]?$ /index.php?m=$1&a=$2 last;
  133. # 配置默认访问文件
  134. index index.php index.html index.htm;
  135. }
  136. # 包含虚拟主机公用配置文件
  137. include server.conf;
  138. }
  139. }
posted @ 2017-05-11 11:51  Alex_Footprint  阅读(562)  评论(0编辑  收藏  举报