centos安装nginx,配置负载均衡



1、安装nginx

   安装教程,参照:http://mp.weixin.qq.com/s/RVaRlRpHqZRjCaXGmOlfKw

2、反向代理的配置

 修改部署目录下conf子目录的nginx.conf文件的内容

[html]   view plain  copy
  1. location / {    
  2.             #设置主机头和客户端真实地址,以便服务器获取客户端真实IP    
  3.             proxy_set_header Host $host;    
  4.             # nginx非80端口处理     
  5.             #proxy_set_header Host $host:$server_port;     
  6.             # 获取真实IP     
  7.             proxy_set_header X-Real-IP $remote_addr;    
  8.             #获取代理者的真实ip    
  9.             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    
  10.     
  11.             #禁用缓存    
  12.             proxy_buffering off;    
  13.             #设置反向代理的地址    
  14.             proxy_pass http://192.168.1.1;(根据实际情况修改)    
  15.       }    


 
 

3、负载均衡的配置

 nginx 的 upstream默认是以轮询的方式实现负载均衡,这种方式中,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。另外一种方式是ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 

[html]   view plain  copy
  1. #user nobody;  
  2. worker_processes 2;  
  3.    
  4. #error_log logs/error.log;  
  5. #error_log logs/error.log notice;  
  6. #error_log logs/error.log info;  
  7.    
  8. #pid    logs/nginx.pid;  
  9.    
  10.    
  11. events {  
  12.   accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on  
  13.   multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off  
  14.   worker_connections 1024;#最大连接数  
  15. }  
  16.    
  17.    
  18. http {  
  19.   include    mime.types;#文件扩展名与文件类型映射表,此映射表主要用于部署在本nginx上的静态资源  
  20.   default_type application/octet-stream;  
  21.    
  22.   #日志格式  
  23.   log_format main '$remote_addr - $remote_user [$time_local] "$request" '  
  24.            '$status $body_bytes_sent "$http_referer" '  
  25.            '"$http_user_agent" "$http_x_forwarded_for"';  
  26.    
  27.   access_log logs/access.log main;  
  28.    
  29.   sendfile    on;  
  30.   #tcp_nopush   on;  
  31.    
  32.   #keepalive_timeout 0;  
  33.   keepalive_timeout 65;#连接超时时间  
  34.    
  35.   gzip on;  
  36.    
  37.   #反向代理  
  38.    
  39.   #【配置1】此配置是[配置4]和[配置5]的结合  
  40.   #此配置将请求转发到两个WEB服务器,根据客户端IP分配目标主机,同时按权重分配流量  
  41.   upstream app1 {  
  42.     ip_hash;  
  43.     server 192.168.14.132:8080 weight=5;  
  44.     server 192.168.14.133:80 weight=3;  
  45.   }  
  46.    
  47.   #【配置2】  
  48.   #默认负载平衡配置,nginx应用HTTP负载平衡来分发请求。  
  49.   #upstream app1 {  
  50.   #  server 192.168.14.132:8080;  
  51.   #  server 192.168.14.133:80;  
  52.   #}  
  53.    
  54.   #【配置3】  
  55.   #最小连接负载平衡配置,nginx将尽量不使用繁忙的服务器,而是将新请求分发给不太忙的服务器。  
  56.   #upstream app1 {  
  57.   #  least_conn;  
  58.   #  server 192.168.14.132:8080;  
  59.   #  server 192.168.14.133:80;  
  60.   #}  
  61.    
  62.   #【配置4】  
  63.   #会话持久性配置,使用ip-hash,客户端的IP地址用作散列密钥,  
  64.   #以确定应为客户端请求选择服务器组中的哪个服务器。  
  65.   #此方法确保来自同一客户端的请求将始终定向到同一服务器,除非此服务器不可用。  
  66.   #upstream app1 {  
  67.   #  ip_hash;  
  68.   #  server 192.168.14.132:8080;  
  69.   #  server 192.168.14.133:80;  
  70.   #}  
  71.    
  72.   #【配置5】  
  73.   #加权负载平衡配置,通过使用服务器权重进一步影响nginx负载平衡算法。  
  74.   #未配置权重的服务器,意味着所有指定的服务器被视为对特定负载平衡方法同等资格。  
  75.   #upstream app1 {  
  76.   #  ip_hash;  
  77.   #  server 192.168.14.132:8080 weight=3;  
  78.   #  server 192.168.14.133:80 weight=2;  
  79.   #  server 192.168.14.134:80;  
  80.   #  server 192.168.14.135:80;  
  81.   #}  
  82.    
  83.    
  84.   server {#可配置多个server以监听不同IP和不同端口  
  85.     listen    80;#监听的端口  
  86.     server_name localhost;#监听的服务器  
  87.    
  88.     #charset koi8-r;  
  89.    
  90.     #access_log logs/host.access.log main;  
  91.    
  92.     #反斜杆代表所有连接,此配置目的是将所有连接交给名为app1的upstream代理,实现负载平衡  
  93.     location / {      
  94.     #设置主机头和客户端真实地址,以便服务器获取客户端真实IP  
  95.         proxy_set_header Host $host;  
  96.   
  97.         proxy_set_header X-Real-IP $remote_addr;  
  98.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  99.   
  100.         #禁用缓存  
  101.         proxy_buffering off;  
  102.   
  103.        #反向代理的配置(地址按实际情况配置)  
  104.        proxy_pass http://app1;  
  105.     }  
  106.    
  107.     #图片文件路径,一般来说,静态文件会部署在本机以加快响应速度  
  108.     #可配置多个这样的location,满足各种需求  
  109.     location ~\.(gif|jpg|png)$ {  
  110.       root /home/root/images;  
  111.     }  
  112.    
  113.     location ~\.(iso|zip|txt|doc|docx)$ {  
  114.       root /home/root/files;  
  115.     }  
  116.    
  117.    
  118.     #error_page 404       /404.html;  
  119.    
  120.     # redirect server error pages to the static page /50x.html  
  121.     #  
  122.     error_page  500 502 503 504 /50x.html;  
  123.     location = /50x.html {  
  124.       root  html;  
  125.     }  
  126.    
  127.    
  128.     # FastCGI是CGI全称是“公共网关接口”(Common Gateway Interface)  
  129.     #对于我来说,使用Tomcat代替即可,请忽略此配置。  
  130.     #location ~ \.php$ {  
  131.     #  root      html;  
  132.     #  fastcgi_pass  127.0.0.1:9000;  
  133.     #  fastcgi_index index.php;  
  134.     #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;  
  135.     #  include    fastcgi_params;  
  136.     #}  
  137.    
  138.     # 添加黑名单,禁止某某访问特定文件  
  139.     # concurs with nginx's one  
  140.     #  
  141.     #location ~ /\.ht {  
  142.     #  deny all;  
  143.     #}  
  144.   }  
  145.    
  146.    
  147.   # another virtual host using mix of IP-, name-, and port-based configuration  
  148.   #  
  149.   #server {  
  150.   #  listen    8000;  
  151.   #  listen    somename:8080;  
  152.   #  server_name somename alias another.alias;  
  153.    
  154.   #  location / {  
  155.   #    root  html;  
  156.   #    index index.html index.htm;  
  157.   #  }  
  158.   #}  
  159.    
  160.    
  161.   # HTTPS server  
  162.   #  
  163.   #server {  
  164.   #  listen    443 ssl;  
  165.   #  server_name localhost;  
  166.    
  167.   #  ssl_certificate   cert.pem;  
  168.   #  ssl_certificate_key cert.key;  
  169.    
  170.   #  ssl_session_cache  shared:SSL:1m;  
  171.   #  ssl_session_timeout 5m;  
  172.    
  173.   #  ssl_ciphers HIGH:!aNULL:!MD5;  
  174.   #  ssl_prefer_server_ciphers on;  
  175.    
  176.   #  location / {  
  177.   #    root  html;  
  178.   #    index index.html index.htm;  
  179.   #  }  
  180.   #}  
  181.    
  182. }  

4、配置完后,记得执行以下命令生效配置

[html]   view plain  copy
  1. nginx -s reload  
5、Nginx内置变量含义

[html]   view plain  copy
  1. 名称    版本    说明(变量列表来源于文件 ngx_http_variables )   
  2. $args    1.0.8    请求中的参数;   
  3. $binary_remote_addr    1.0.8    远程地址的二进制表示   
  4. $body_bytes_sent    1.0.8    已发送的消息体字节数   
  5. $content_length    1.0.8    HTTP请求信息里的"Content-Length";   
  6. $content_type    1.0.8    请求信息里的"Content-Type";   
  7. $document_root    1.0.8    针对当前请求的根路径设置值;   
  8. $document_uri    1.0.8    与$uri相同;   
  9. $host    1.0.8    请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名;   
  10. $hostname    1.0.8       
  11. $http_cookie    1.0.8    cookie 信息   
  12. $http_post    1.0.8       
  13. $http_referer    1.0.8    引用地址   
  14. $http_user_agent    1.0.8    客户端代理信息   
  15. $http_via    1.0.8     最后一个访问服务器的Ip地址。   
  16. $http_x_forwarded_for    1.0.8     相当于网络访问路径。   
  17. $is_args    1.0.8       
  18. $limit_rate    1.0.8    对连接速率的限制;   
  19. $nginx_version    1.0.8       
  20. $pid    1.0.8       
  21. $query_string    1.0.8    与$args相同;   
  22. $realpath_root    1.0.8       
  23. $remote_addr    1.0.8    客户端地址;   
  24. $remote_port    1.0.8    客户端端口号;   
  25. $remote_user    1.0.8    客户端用户名,认证用;   
  26. $request    1.0.8    用户请求   
  27. $request_body    1.0.8       
  28. $request_body_file    1.0.8    发往后端的本地文件名称   
  29. $request_completion    1.0.8       
  30. $request_filename    1.0.8    当前请求的文件路径名   
  31. $request_method    1.0.8    请求的方法,比如"GET"、"POST"等;   
  32. $request_uri    1.0.8    请求的URI,带参数;   
  33. ...  

猜你喜欢

转载自blog.csdn.net/pbymw8iwm/article/details/80010584