hyzqb 发表于 2018-11-9 11:12:17

nginx在网站中的7层转发功能

  ____________ client
  +
  |
  |
  |
  |
  +-----------------   LVS并发访问量400万
  |
  |
  |
  |
  +
  |
  |
  ---------------------------
  |         |            |
  Nginx       Nginx       Nginx
  |
  +
  -----------------------------------------------------------------------------------------------------
  |            |         |               |            |          |             |          |               |
  apache   apache    apache   squid    squid   squid    tomcattomcat      tomcat
  ^                  ^         ^          |            |            |
  |            |         |          |            |            |
  +----------- |--------------- |----------+            |            |
  +---------------|---------------------+         |
  +-------------------------------+
  LB-Nginx:
  Client:
  CIP:    202.106.0.56
  Director-Nginx:
  VIP:    202.106.0.254
  DIP:   192.168.0.1
  Director配置:
  1、安装nginx
  cd /etc/nginx/conf.d
  vim rip.conf
  upstream squids {
  server 192.168.0.254:3128;
  server 192.168.0.208:3128;
  server 192.168.0.39:3128;
  RealServer
  }
  upstream apaches {
  server 192.168.0.14:80;
  server 192.168.0.231:80;
  server 192.168.0.49:80;
  }
  upstream tomcats {
  server 192.168.0.119:8080;
  server 192.168.0.188:8080;
  server 192.168.0.194:8080;
  }
  2、vim /etc/nginx/nginx.conf
  location / {
  root   /usr/share/nginx/html;
  indexindex.html index.htm;
  if ($request_uri ~* ".*\.html$")
  {
  proxy_pass http://squids;
  }
  if ($request_uri ~* ".*\.php$")
  {
  proxy_pass http://apaches;
  }
  if ($request_uri ~* ".*\.jsp$")
  {
  proxy_pass http://tomcats;
  }
  RealServer配置(squid):
  vim squid.conf
  http_access allow all
  http_port 3128 vhost
  cache_peer 192.168.0.x parent 80 0 no-query originserver
  #cache_peer 192.168.0.XX parent 80 0 no-query originserver round-robinweight=n
  RealServer配置(apache):
  1, IP
  2,
  准备一个 index.html
  test.php
  RealServer配置(tomcat):
  index.jsp
  补充:
  在Nginx中实现view.
  geo $liu {
  default1;
  192.168.0.247/32 0;
  }
  if ($liu) {
  proxy_pass http://apaches;
  }
  测试
  默认1000
  umlimit -a
  umlimt -n10000
  永久生效
  vim   /etc/security/limits.conf
  *                     hard   nofile         10000
  ab -n 10000 -c 5000 http://localhost/
  根据网友文章,自己实践,介绍3种Nginx防盗链的方法,节省你的宽带
  一:一般的防盗链如下:
  location ~* \.(gif|jpg|png|swf|flv)$ {
  valid_referers none blocked www.ingnix.com ;
  if ($invalid_referer) {
  rewrite ^/ http://www.ingnix.com/retrun.html;
  #return 404;
  }
  }
  第一行:gif|jpg|png|swf|flv
  表示对gif、jpg、png、swf、flv后缀的文件实行防盗链
  第二行: 表示对www.ingnix.com这2个来路进行判断
  if{}里面内容的意思是,如果来路不是指定来路就跳转到http://www.ingnix.com/retrun.html页面,当然直接返回404也是可以的。
  二:针对图片目录防止盗链
  location /images/ {
  alias /data/images/;
  valid_referers none blocked server_names *.xok.la xok.la ;
  if ($invalid_referer) {return 403;}
  }
  三:使用第三方模块ngx_http_accesskey_module实现Nginx防盗链
  实现方法如下:
  实现方法如下:
  1. 下载NginxHttpAccessKeyModule模块文件:Nginx-accesskey-2.0.3.tar.gz;
  2. 解压此文件后,找到nginx-accesskey-2.0.3下的config文件。编辑此文件:替换其中的”$HTTP_ACCESSKEY_MODULE”为”ngx_http_accesskey_module”;
  3. 用一下参数重新编译nginx:
  ./configure --add-module=path/to/nginx-accesskey
  4. 修改nginx的conf文件,添加以下几行:
  location /download {
  accesskey             on;
  accesskey_hashmethodmd5;
  accesskey_arg         "key";
  accesskey_signature   "mypass$remote_addr";
  }
  其中:
  accesskey为模块开关;
  accesskey_hashmethod为加密方式MD5或者SHA-1;
  accesskey_arg为url中的关键字参数;
  accesskey_signature为加密值,此处为mypass和访问IP构成的字符串。
  访问测试脚本download.php:
  
  访问第一个download_add_key链接可以正常下载,第二个链接download_org_path会返回403 Forbidden错误。
  nginx 错误502 upstream sent too big header while reading response header from upstream
  sudo gedit /var/log/nginx/error.log
  查看错误日志
  upstream sent too big header while reading response header from upstream
  你去搜这个错误,网上的解释都差不多,无外乎是cookie携带的header太多了,让你设置:
  fastcgi_buffer_size 128k;
  fastcgi_buffers 8 128k;
  逐步尝试。其中fastcgi_buffers 8 128k 这句,fastcgi_buffers 32 32k 这样更好,内存是整块分配和释放的,减少单位k数能尽可能利用。
  另外,如果你用nginx做负载均衡的话,改了上述参数是没用的,要在转发的配置上,比如以下设置:
  location @to_other {
  proxy_buffer_size128k;
  proxy_buffers   32 32k;
  proxy_busy_buffers_size 128k;
  add_header X-Static transfer;
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP$remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass http://backend;    #请求转发
  }
  加粗的三行才会起作用。
  fastcgi_* 可以理解成nginx接受client请求时的响应使用的。proxy是nginx作为client转发时使用的,如果header过大,超出了默认的1k,
  就会引发上述的upstream sent too big header。
  可以参考:
  http://wiki.nginx.org/NginxHttpProxyModule
  http://blog.sina.com.cn/s/blog_5dc960cd0100i4mt.html
  其它搜索结果可以无视,都是大同小异的。
  location ~ \.php$ {
  fastcgi_buffer_size 128k;
  fastcgi_buffers 32 32k;
  include /etc/nginx/fastcgi_params;
  fastcgi_pass   127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME /host/web/$fastcgi_script_name;
  }
  查看文章
  nginx+fastcgi 502报错原因很多
  2008-11-02 10:44
  如题,最近网站频繁出现502错误,简直无法正常运转,出现这种情况大多是php-cgi超时没有返回信息,或进程僵死等情况造成的,
  参考张宴的这篇关于502错误的解决办法(http://blog.s135.com/read.php?361),并咨询系统管理员高手,我们的nginx已经配置
  到极致这些都已经老早做过修改了,但现在又出然出现。
  经过分析将nginx的error log打开,发现"pstream sent too big header while reading response header from upstream"这样的错误提示,
  查阅了一下资料,大意是nginx缓冲区有一个bug造成的,我们网站的页面消耗占用缓冲区可能过大。参考老外写的修改办法增加了缓冲区容量大小设置,

  502问题彻底解决,后来系统管理员又对参数做了调整只保留了2个设置参数:client head buffer,fastcgi buffer>
页: [1]
查看完整版本: nginx在网站中的7层转发功能