Nginx是一款很优秀的基于event的webserver。吞吐量大。占用资源少,只是文档就很让人郁闷了,免费的Nginx和收费的Nginx+的文档共用一份,配置完之后才发现免费的Nginx启动某些命令失败。。

。。。。很伤感。

在我们的系统中,一直使用httpd做前端的7层负载均衡,近期想转换到Nginx。学习了下怎么配置,留作纪念。另外nginx的配置须要操作系统參数(linux)也做一些优化才干达到更好的效果

1.设置server locationcopy

  1. location / {
  2. proxy_pass http://appservers/;
  3. #health_check interval=10 fails=3 passes=2; match=server_ok;
  4. }

2. 设置appservers相应的server,server地址。尝试次数。尝试时间间隔。负载算法

ip_hash来取代默认的rr方式,即能够将某clientIP的请求通过哈希算法定位到同一台后端webserver上。这样避免了session丢失。攻克了session问题

设定Nginx与server通信的尝试失败的次数。

fail_timeout參数定义的时间段内,假设失败的次数达到此值,Nginx就觉得server不可用。

在下一个fail_timeout时间段,server不会再被尝试

[plain] view
plain
copy

  1. <strong>upstream appservers {
  2. ip_hash # for session persistence</strong>
  3. #least_conn default: round-robin
  4. #zone appservers 64k; nginx plus function
  5. <strong>server localhost:8080 max_fails=1 fail_timeout=30s;</strong>
  6. # nginx plus function route=node1;
  7. <strong>server localhost:9090 max_fails=1 fail_timeout=30s;</strong>
  8. # nginx plus function route=node2;
  9. #sticky route $route_cookie $route_uri;
  10. <strong> }</strong>

3. 性能调优參数 (以后补充)

nginx.conf配置

user  nginx nginx;  
worker_processes  4;  # == cpu core number, total nginx process: one master + all workers
  
#error_log  logs/error.log;  
#error_log  logs/error.log  notice;  
#error_log  logs/error.log  info;  
  
#pid        logs/nginx.pid;  
  
#http://nginx.org/en/docs/ngx_core_module.html#worker_connections  
events { 
   worker_connections  1024;  # need to check os limitation here
}  
  
  
http {  
    #include       mime.types;  
    #default_type  application/octet-stream;  
  
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
    #                  '$status $body_bytes_sent "$http_referer" '  
    #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  
    #access_log  logs/access.log  main;  
    access_log off;
    #sendfile        on;  
    #tcp_nopush     on;  
  
    #timeout
    #client_body_timeout 12;
    #client_header_timeout 12;
    #send_timeout 10;  
    #keepalive_timeout  0;  
    keepalive_timeout  65;  
    gzip  on;
    gzip_comp_level  2;
    gzip_min_length  1000;
    gzip_proxied     expired no-cache no-store private auth;
    gzip_types       text/plain application/x-javascript text/xml text/css application/xml;  
    # 防止cache不足频繁读写文件,header cache一般不会大于1k。最小设置系统分页大小(<span style="font-family: 'Lucida Grande', Verdana, Lucida, Helvetica, Arial, sans- serif; font-size: 13px; text-align: justify;">getconf PAGESIZE</span>)
    client_body_buffer_size 10K;
    client_header_buffer_size 4k;
    client_max_body_size 8m;
    large_client_header_buffers 2 4k;
    
    #http://nginx.com/resources/admin-guide/load-balancer/  
    #http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream_conf  
    upstream appservers {  
      ip_hash; # for session persistence   
      #least_conn default: round-robin  
      #zone appservers 64k; nginx plus function  
        
        server localhost:8080 max_fails=3 fail_timeout=30s;  
        # nginx plus function route=node1;  
        server localhost:9090 max_fails=3 fail_timeout=30s;  
        # nginx plus function route=node2;  
        #sticky route $route_cookie $route_uri;  
    }  
  
        #map $cookie_jsessionid $route_cookie {  
        #    ~.+\.(?P<route>\w+)$ $route;  
        #}  
          
        #map $request_uri $route_uri {  
        #    ~jsessionid=.+\.(?P<route>\w+)$ $route;  
        #}  
  
    #match server_ok {  
    #    status 200-399;  
    #    body !~ "maintenance mode";  
    #}  
  
    server {  
            location / {  
                proxy_pass http://appservers/;  
                #health_check interval=10 fails=3 passes=2; match=server_ok;  
            }  
              
             # Location for configuration requests  
        #location /upstream_conf {  
        #    upstream_conf;  
        #    allow 127.0.0.1;  
        #    deny all;  
        #}    
              
        listen       80;  
        server_name  localhost;  
  
        charset UTF-8;  
        #access_log  logs/host.access.log  main;  
  
        #location / {  
        #    root   html;  
        #    index  index.html index.htm;  
        #}  
  
        #error_page  404              /404.html;  
  
        # redirect server error pages to the static page /50x.html  
        #  
        #error_page   500 502 503 504  /50x.html;  
        #location = /50x.html {  
        #    root   html;  
        #}  
  
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
        #  
        #location ~ \.php$ {  
        #    proxy_pass   http://127.0.0.1;  
        #}  
  
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
        #  
        #location ~ \.php$ {  
        #    root           html;  
        #    fastcgi_pass   127.0.0.1:9000;  
        #    fastcgi_index  index.php;  
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
        #    include        fastcgi_params;  
        #}  
  
        # deny access to .htaccess files, if Apache's document root  
        # concurs with nginx's one  
        #  
        #location ~ /\.ht {  
        #    deny  all;  
        #}  
    }  
  
  
    # another virtual host using mix of IP-, name-, and port-based configuration  
    #  
    #server {  
    #    listen       8000;  
    #    listen       somename:8080;  
    #    server_name  somename  alias  another.alias;  
  
    #    location / {  
    #        root   html;  
    #        index  index.html index.htm;  
    #    }  
    #}  
  
  
    # HTTPS server  
    #  
    #server {  
    #    listen       443 ssl;  
    #    server_name  localhost;  
  
    #    ssl_certificate      cert.pem;  
    #    ssl_certificate_key  cert.key;  
  
    #    ssl_session_cache    shared:SSL:1m;  
    #    ssl_session_timeout  5m;  
  
    #    ssl_ciphers  HIGH:!aNULL:!MD5;  
    #    ssl_prefer_server_ciphers  on;  
  
    #    location / {  
    #        root   html;  
    #        index  index.html index.htm;  
    #    }  
    #}  
  
}

PS: Nginx和Httpd都是很优秀的Webserver,两者的比較能够看Apache_vs_nginx

Nginx 做系统的前端反向proxy的更多相关文章

  1. 使用nginx做反向代理来访问tomcat服务器

    本次记录的是使用nginx来做一个反向代理来访问tomcat服务器.简单的来说就是使用nginx做为一个中间件,来分发客户端的请求,将这些请求分发到对应的合适的服务器上来完成请求及响应. 第一步:安装 ...

  2. Nginx做反向代理总是被系统kill

    公司使用Nginx做反向代理,以前都挺正常的,最近不知怎么回事总是无端被系统kill,而在nginx错误日志中也没有信息输出. 网上查了很多资料,也没什么靠谱的回答,唯一觉得有点关联的就是linux ...

  3. 利用nginx做反向代理解决前端跨域问题

    最近朋友再群里提了一个问题,他们公司给他提供了一个获取数据的接口,在浏览器访问这个接口能获取到json数据,但是放在项目里使用ajax就产生了跨域问题,一般这个需要提供接口的后台方面需要做跨域处理,但 ...

  4. Nginx做web服务器反向代理

    实验目的 通过nginx实现反向代理的功能,类似apache反向代理和haproxy反向代理 工作中用nginx做反向代理和负载均衡的也越来越多了 有些公司从web服务器到反向代理,都使用nginx. ...

  5. 把www.domain.com均衡到本机不同的端口 反向代理 隐藏端口 Nginx做非80端口转发 搭建nginx反向代理用做内网域名转发 location 规则

    负载均衡-Nginx中文文档 http://www.nginx.cn/doc/example/loadbanlance.html 负载均衡 一个简单的负载均衡的示例,把www.domain.com均衡 ...

  6. 端口被占用通过域名的处理 把www.domain.com均衡到本机不同的端口 反向代理 隐藏端口 Nginx做非80端口转发 搭建nginx反向代理用做内网域名转发 location 规则

    负载均衡-Nginx中文文档 http://www.nginx.cn/doc/example/loadbanlance.html 负载均衡 一个简单的负载均衡的示例,把www.domain.com均衡 ...

  7. nginx做反向代理负载均衡 Java怎么获取后端服务器获取用户IP

    nginx做反向负载均衡,后端服务器获取真实客户端ip   首先,在前端nginx上需要做如下配置: location / proxy_set_hearder host                 ...

  8. 用nginx做反向代理来访问防外链图片

    用nginx做反向代理来访问防外链图片 女儿的博客从新浪搬到wordpress后,发现原来博客上链接的新浪相册的图片都不能访问了,一年的博客内容,一个个去重新上传图片,修正链接也是个大工程.还是得先想 ...

  9. 使用nginx做反向代理

    很多同学喜欢用nginx做反向代理访问某些网站,原因大家都懂的,今天老高记录一下如何使用nginx做反向代理以及如何配置和优化nginx的反向代理. 准备工作 首先,你需要一个稳定的国外的便宜的VPS ...

随机推荐

  1. Linux下开启vim高亮

    默认是不高亮的. [root@local ~]# vi ~/.vimrc 没有则新建这个文件. 或者修改 [root@local vim74]# vi /etc/vimrc 添加一行. syntax ...

  2. 【撸码caffe 二】 blob.hpp

    Blob类是caffe中对处理和传递的实际数据的封装,是caffe中基本的数据存储单元,包括前向传播中的图像数据,反向传播中的梯度数据以及网络层间的中间数据变量(包括权值,偏置等),训练模型的参数等等 ...

  3. Solr快速入门(一)

    概述 本文档介绍了如何获取和运行Solr,将各种数据源收集到多个集合中,以及了解Solr管理和搜索界面. 首先解压缩Solr版本并将工作目录更改为安装Solr的子目录.请注意,基本目录名称可能随Sol ...

  4. 安装windwos7 iis 出现错误,并非所有都成功更改的解决办法

    1.首先排除网上说的 安装的WIN7是精简版的问题,我这个是旗舰版,以前是正常安装IIS的,后来程序问题我卸载了,就安装不上了 2.网上说的修改什么UAC权限,也是胡扯,因为默认都是最低的 3.排除网 ...

  5. 30秒就能理解的JavaScript优秀代码

    数组 arrayMax 返回数组中的最大值. 将Math.max()与扩展运算符 (...) 结合使用以获取数组中的最大值. const arrayMax = arr => Math.max(. ...

  6. RN配置

    Write by lz: 详细参考官方网址: http://reactnative.cn/docs/0.43/getting-started.html#content 若是无法安装 Chocolate ...

  7. 目录处理文件&链接命令

    一.目录处理文件 1.删除文件或目录 rm -rf [文件或目录]           //remove:删除文件或目录  -r:删除目录  -f:强制 2.复制文件或目录 cp [选项] [原文件或 ...

  8. 【转】VGG网络结构及参数

     VGG网络  VGG16输入224*224*3的图片,经过的卷积核大小为3x3x3,stride=1,padding=1,pooling为采用2x2的max pooling方式: 1.输入224x2 ...

  9. 无法打开文件"CChart_d.lib"

    把4个.lib文件删掉重新加一遍就好了

  10. Linux crontab 在每月最后一天执行

    59  23 * * * if [ `date +%d -d tomorrow` = 01 ]; then; command(/usr/bin/curl -s -o temp.txt  http:// ...