Centos下安装、Nginx笔记(二) 简单负载均衡

来源:互联网 发布:java 遍历ftp文件夹 编辑:程序博客网 时间:2024/06/05 20:49

from  http://blog.chinaunix.net/uid-1840233-id-3754213.html

说明:
1、环境准备
Nginx反向代理服务器:

系统 Centos 5.4 + ngix 
外网IP: 60.x.x.13
内网IP: 192.168.10.13

web1服务器:
系统: windows 2003  IIS 
内网 ip: 192.168.10.2  

web2服务器
系统windows 2003+IIS 
内网 ip 192.168.10.3   (2/3的页面内容设置不一样以便最后测试的时候进行查看)

2、Nginx反向代理服务器 做负载均衡配置: 
此次配置实现内容:
1)访问代理服务器http://60.x.x.13 访问的是Nginx代理服务器本身的网站;
2)访问代理服务器http://60.x.x.13:8800时实现跳转到内网2台web服务器进行负载;
具体见配置说明。

#vi /usr/local/nginx/conf/nginx.config  
------------------------------- nginx.config  -----------------------------------

点击(此处)折叠或打开

  1. #user nobody;
  2. worker_processes 1;

  3. error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;

  6. pid logs/nginx.pid;


  7. events {
  8. use epoll;
  9. worker_connections 1024;
  10. }


  11. http {
  12. include mime.types;
  13. default_type application/octet-stream;

  14. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15. '$status $body_bytes_sent "$http_referer" '
  16. '"$http_user_agent" "$http_x_forwarded_for"';

  17. access_log logs/access.log main;

  18. sendfile on;
  19. #tcp_nopush on;

  20. #keepalive_timeout 0;
  21. keepalive_timeout 65;
  22. tcp_nodelay on;

  23. #gzip on;
  24. gzip on;
  25. gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  26. #========= server 80 ====== 如果访问centos 80端口是访问到nginx代理服务器自身的站点
  27. server {
  28. listen 80;
  29. #有域名可用域名
  30. server_name 60.x.x.13;

  31. access_log logs/13-80.host.access.log main;

  32. location / {
  33. root html;
  34. index index.html index.htm;
  35. }

  36. error_page 500 502 503 504 /50x.html;
  37. location = /50x.html {
  38. root html;
  39. }
  40. }

  41. #======balance 8800======= 如果访问:http://60.x.x.13:8800,则会跳转到内部网络的windows 2/3
  42. upstream balance
  43. {
  44. server 192.168.10.2:9900;
  45. server 192.168.10.3:9901;
  46. }
  47. #========server 8800-> balance 2/3
  48. server {
  49. listen 8800;
  50. server_name 60.x.x.13; 域名

  51. access_log logs/58.host.access.log main;

  52. location / {
  53. proxy_pass http://balance; #此处跟upstream balance名称对应
  54. proxy_set_header Host $host;
  55. proxy_set_header X-Real-IP $remote_addr;
  56. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  57. }

  58. error_page 500 502 503 504 /50x.html;
  59. location = /50x.html {
  60. root html;
  61. }
  62. }
  63. }


 ------------------------------- 
重启nginx服务, #/usr/local/nginx/sbin/nginx -s reload

客户端刷新访问:http://60.x.x.13:8800 看是不是分别对应到192.168.10.2/3网站内容了。(2/3的页面内容设置不一样就可以查看出来)


0 0
原创粉丝点击