博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx 做系统的前端反向proxy
阅读量:7296 次
发布时间:2019-06-30

本文共 5835 字,大约阅读时间需要 19 分钟。

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

。。。。很伤感。

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

1.设置server location

  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] 
  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.         
  6.         <strong>server localhost:8080 max_fails=1 fail_timeout=30s;</strong>  
  7.         # nginx plus function route=node1;  
  8.         <strong>server localhost:9090 max_fails=1 fail_timeout=30s;</strong>  
  9.         # nginx plus function route=node2;  
  10.         #sticky route $route_cookie $route_uri;  
  11.    <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。最小设置系统分页大小(getconf PAGESIZE)    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
\w+)$ $route;          #}                    #map $request_uri $route_uri {          #    ~jsessionid=.+\.(?P
\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,两者的比較能够看

转载地址:http://cmgjm.baihongyu.com/

你可能感兴趣的文章
PHP全栈开发(八):CSS Ⅸ dispaly & visibility
查看>>
正则表达式
查看>>
【Oracle 12c】最新CUUG OCP-071考试题库(56题)
查看>>
C#使用Xamarin开发可移植移动应用进阶篇(6.使用渲染器针对单个平台自定义控件..很很很很重要..),附源码...
查看>>
实验二
查看>>
简单安装ubuntu
查看>>
20160331javaweb 之JSP page 指令
查看>>
用Ruby批量获取电影的评分与影片信息
查看>>
2019.5.29 区块链论文翻译
查看>>
Centos6.6安装mysql记录
查看>>
OCP读书笔记(5) - 使用RMAN创建备份
查看>>
java的接口和抽象类区别
查看>>
能够提高PHP的性能的一些注意事项
查看>>
020-请你说一说app测试的工具
查看>>
软件测试2019:第五次作业—— 安全测试(含安全测试工具实验)
查看>>
SSM框架搭建总结(2)
查看>>
Python学习(19)正则表达式
查看>>
PHP中空字符串、0、null、empty和false之间的关系
查看>>
【深度学习篇】---CNN和RNN结合与对比,实例讲解
查看>>
201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结
查看>>