一、什么是nginx?
nginx是作为一个强大的Web服务器软件,具有高性能、高并发性和低内存占用的特点。
nginx启动后,会以daemon的方式在后台运行。后台进程包含一个master进程和多个worker进程。
master进程主要用来管理woker进程,包含:接收来自外界的信号,向各worker进程发送信号,监控worker进程的运行状态。当worker进程一次退出后(异常),会自动启动新的worker进程。
协议支持:
nginx1.4.0 对websocket 和SPDY都做了正式的支持。
nginx1.6.0对DPDY3.

块级指令 和 简单指令
块级指令和简单指令类似,块级指令用{}包裹,简单指令是用分号结束,如果块级指令的大括号内部有其它指令,则他叫做上下文(events, http, server,, location)

当location中没有root时,server 中的root才会起效果
server {
listen 8080;
root /data/dist;

location / {

}
}

location 讲解 location 表示请求级别
基本语法 【=|~|~|^~|@】h或者正则
server {
server_name www.baidu.com;
location /abc {} //表示模糊匹配 baidu.com/abc 或 baidu.com/abc/ 或baidu.com/abcde 或 baidu.com/abc?p1
location = /abc {} 表示只能匹配abc baidu.com/abc 或 baidu.com/abc?p1
location ~ ^/abc {} 表示匹配只能以abc开头,并且只能匹配abc
location ~
^/abc {} 表示匹配的正则表达式不区分大小写
@:定义命名location字段,可以由内部产生的请求来访问 如 try_files 或 error_page;

root和alias的区别
alias 访问/img/目录内部文件时,nginx会自动去 /var/www/image/去查找,最后文件夹必须“/”结束
root 访问/img/目录时,nginx只会去/var/www/image/目录文件下找文件,最后结束没有规定
location /img/ {
alias /var/www/image/;
}
location /img/ {
root /var/www/image;
}
}
二、nginx正向代理和反向代理
#user nobody; # 用户权限
worker_processes auto; # 工作进程的数量
#worker_cpu_affinity auto;

全局错误日志及PID文件
error_log logs/error.log; # 日志输出
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid logs/nginx.pid;

  1. events {
  2. #epoll是多路复用IO(I/O Multiplexing)中的一种方式,
  3. #use 设置用于复用客户端线程的轮询方法。如果你使用Linux 2.6+,你应该使用epoll。如果你使用*BSD,你应该使用kqueue。
  4. use epoll; # IOCP[window] , kqueue[bsd] , epoll[linux]
  5. #单个后台worker process进程的最大并发链接数
  6. worker_connections 1024;
  7. #multi_accept 告诉nginx收到一个新连接通知后接受尽可能多的连接。
  8. multi_accept on;
  9. # 并发总数是 worker_processes 和 worker_connections 的乘积
  10. # 即 max_clients = worker_processes * worker_connections
  11. # 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4 为什么
  12. # 为什么上面反向代理要除以4,应该说是一个经验值
  13. # 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000
  14. # worker_connections 值的设置跟物理内存大小有关
  15. # 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数
  16. # 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右
  17. # 我们来看看360M内存的VPS可以打开的文件句柄数是多少:
  18. # $ cat /proc/sys/fs/file-max
  19. # 输出 34336
  20. # 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内
  21. # 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置
  22. # 使得并发总数小于操作系统可以打开的最大文件数目
  23. # 其实质也就是根据主机的物理CPU和内存进行配置
  24. # 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。
  25. # ulimit -SHn 65535
  26. }
  1. http {
  2. include mime.types; #设定mime类型,类型由mime.type文件定义
  3. default_type application/octet-stream;
  4. #设定日志格式
  5. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  6. # '$status $body_bytes_sent "$http_referer" '
  7. # '"$http_user_agent" "$http_x_forwarded_for"';
  8. #设置nginx是否将存储访问日志。关闭这个选项可以让读取磁盘IO操作更快
  9. #access_log logs/access.log main;
  10. #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
  11. #对于普通应用,必须设为 on,
  12. #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
  13. #以平衡磁盘与网络I/O处理速度,降低系统的uptime.
  14. # 高效文件传输
  15. sendfile on;
  16. tcp_nopush on;
  17. tcp_nodelay on;
  18. # types_hash_max_size 影响散列表的冲突率。types_hash_max_size越大,就会消耗更多的内存,但散列key的冲突率会降低,检索速度就更快。types_hash_max_size越小,消耗的内存就越小,但散列key的冲突率可能上升。
  19. types_hash_max_size 2048;
  20. #连接超时时间
  21. #keepalive_timeout 0;
  22. keepalive_timeout 65;
  23. gzip on;
  24. gzip_disable "MSIE [1-6].";
  25. gzip_comp_level 6;
  26. gzip_min_length 1000; #置对数据启用压缩的最少字节数。如果一个请求小于1000字节,我们最好不要压缩它,因为压缩这些小的数据会降低处理此请求的所有进程的速度。
  27. gzip_proxied expired no-cache no-store private auth;
  28. gzip_types text/plain application/x-javascript text/xml text/css application/xml;
  29. #Buffers:另一个很重要的参数为buffer,如果buffer太小,Nginx会不停的写一些临时文件,这样会导致磁盘不停的去读写,现在我们先了解设置buffer的一些相关参数:
  30. #client_body_buffer_size:允许客户端请求的最大单个文件字节数
  31. #client_header_buffer_size:用于设置客户端请求的Header头缓冲区大小,大部分情况1KB大小足够
  32. #client_max_body_size:设置客户端能够上传的文件大小,默认为1m
  33. #large_client_header_buffers:该指令用于设置客户端请求的Header头缓冲区大小
  34. #设定请求缓冲
  35. client_body_buffer_size 10K;
  36. client_header_buffer_size 1k;
  37. client_max_body_size 8m;
  38. large_client_header_buffers 2 1k;
  39. map $http_user_agent $outdated { # 判断浏览器版本
  40. default 0;
  41. "~MSIE [6-9].[0-9]" 1;
  42. "~MSIE 10.0" 1;
  43. }
  44. # weight:轮询权值,默认值为1。
  45. # down:表示当前的server暂时不参与负载。
  46. # max_fails:允许请求失败的次数,默认为1。当超过最大次数时,返回 proxy_next_upstream 模块定义的错误。
  47. # fail_timeout:有两层含义,一是在fail_timeout时间内最多容许max_fails次失败;二是在经历了max_fails次失败以后,30s时间内不分配请求到这台服务器。
  48. # backup : 备份机器。当其他所有的非 backup 机器出现故障的时候,才会请求backup机器,因此这台机器的压力最轻。
  49. # max_conns: 限制同时连接到某台后端服务器的连接数,默认为 0。即无限制。
  50. # proxy_next_upstream : 这个指令属于 http_proxy 模块的,指定后端返回什么样的异常响应
  51. #负载均衡
  52. #upstream DataBase {
  53. # ip_hash; # 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
  54. # server 10.xx.xx.xx weight=1 max_fails=2 fail_timeout=30s;
  55. # server 10.xx.xx.xx;
  56. # server 10.xx.xx.xx;
  57. #}
  58. server {
  59. listen 80; #端口号
  60. server_name v.fpdiov.com; #域名
  61. location /api { #代理API
  62. proxy_pass http://api.fpdiov.com:8090;
  63. # proxy_set_header Host $host;
  64. # proxy_set_header X-Real-IP $remote_addr; #在web服务器端获得用户的真实ip
  65. # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  66. proxy_redirect off;
  67. proxy_connect_timeout 600; #nginx跟后端服务器连接超时时间(代理连接超时)
  68. proxy_read_timeout 600; #连接成功后,后端服务器响应时间(代理接收超时)
  69. proxy_send_timeout 600; #后端服务器数据回传时间(代理发送超时)
  70. proxy_buffer_size 32k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
  71. proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
  72. proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
  73. proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服>务器传
  74. keepalive_requests 500;
  75. proxy_http_version 1.1;
  76. proxy_ignore_client_abort on;
  77. }
  78. location ^~ / {
  79. root /mnt/www/fpd-car-manage-frontend; #启动根目录
  80. if ($outdated = 1){
  81. rewrite ^ http://oisbyqrnc.bkt.clouddn.com redirect; #判断浏览器版本跳转
  82. }
  83. index index.html; #默认访问页面
  84. try_files $uri $uri/ /index.html;
  85. }
  86. }
  87. #server {
  88. # listen 80; #侦听80端口
  89. # server_name localhost; #访问域名
  90. #charset koi8-r; # 字符集
  91. #access_log logs/host.access.log main;
  92. #location / {
  93. # root html;
  94. # index index.html index.htm;
  95. #}
  96. #error_page 404 /404.html; #&emsp;错误页面
  97. # redirect server error pages to the static page /50x.html
  98. #
  99. #error_page 500 502 503 504 /50x.html;
  100. #location = /50x.html {
  101. # root html;
  102. #}
  103. #静态文件,nginx自己处理
  104. #location ~ ^/(images|javascript|js|css|flash|media|static)/ {
  105. #过期30天,静态文件不怎么更新,过期可以设大一点,
  106. #如果频繁更新,则可以设置得小一点。
  107. # expires 30d;
  108. #}
  109. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  110. #
  111. #location ~ \.php$ {
  112. # proxy_pass http://127.0.0.1;
  113. #}
  114. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  115. #
  116. #location ~ \.php$ {
  117. # root html;
  118. # fastcgi_pass 127.0.0.1:9000;
  119. # fastcgi_index index.php;
  120. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  121. # include fastcgi_params;
  122. #}
  123. # deny access to .htaccess files, if Apache's document root
  124. # concurs with nginx's one
  125. #
  126. #location ~ /\.ht {
  127. # deny all;
  128. #}
  129. #}
  130. # another virtual host using mix of IP-, name-, and port-based configuration
  131. #
  132. #server {
  133. # listen 8000;
  134. # listen somename:8080;
  135. # server_name somename alias another.alias;
  136. # location / {
  137. # root html;
  138. # index index.html index.htm;
  139. # }
  140. #}
  141. # HTTPS server
  142. #
  143. #server {
  144. #启用 https, 使用 http/2 协议, nginx 1.9.11 启用 http/2 会有bug, 已在 1.9.12 版本中修复.
  145. # listen 443 ssl;
  146. # server_name localhost;
  147. # ssl on;
  148. # ssl_certificate cert.pem; #证书路径;
  149. # ssl_certificate_key cert.key; #私钥路径;
  150. # ssl_session_cache shared:SSL:1m;
  151. # ssl_session_timeout 5m;
  152. # ssl_ciphers HIGH:!aNULL:!MD5; #指定的套件加密算法
  153. # ssl_prefer_server_ciphers on; # 设置协商加密算法时,优先使用我们服务端的加密套件,而不是客户端浏览器的加密套件。
  154. # ssl_session_timeout 60m; #缓存有效期
  155. # ssl_session_cache shared:SSL:10m; #储存SSL会话的缓存类型和大小
  156. # location / {
  157. # root html;
  158. # index index.html index.htm;
  159. # }
  160. #}
  161. }

三、负载均衡
负载均衡特点:
1、高并发连接;
2、内存消耗小;
3、配置文件非常简单;
4、成本低廉;
5、支持Rewrite重写规则;
6、内置的健康检查功能;
7、节省宽带;
8、稳定性高