nginx+apache负载均衡

 学习过程记录如下:感谢为我提供帮助的朋友们!

1: 在一台机器上安装nginx(过程省略),该机器的ip为:192.168.1.2
2:在另一台机器上安装php+mysql+apache(过程省略) ,该机器的ip为:192.168.1.9
3:将nginx和apache整合起来!
nginx.conf如下:

  1. [root@qht124 conf]# cat nginx.conf
  2. user www www;
  3. worker_processes 4;
  4.  
  5. # [ debug | info | notice | warn | error | crit ]
  6. error_log /usr/local/nginx/logs/nginx_error.log crit;
  7. pid /usr/local/nginx/nginx.pid;
  8. #Specifies the value for maximum file descriptors that can be opened by this process.
  9. worker_rlimit_nofile 51200;
  10. events
  11. {
  12. use epoll;
  13. worker_connections 51200;
  14. }
  15.  
  16. http
  17. {
  18. include mime.types;
  19. default_type application/octet-stream;
  20. source_charset GB2312;
  21. server_names_hash_bucket_size 256;
  22. client_header_buffer_size 256k;
  23. large_client_header_buffers 4 256k;
  24.  
  25. #size limits
  26. client_max_body_size 50m;
  27. client_body_buffer_size 256k;
  28. client_header_timeout 3m;
  29. client_body_timeout 3m;
  30. send_timeout 3m;
  31. keepalive_timeout 120;
  32. tcp_nodelay on;
  33.  
  34. include vhosts/upstream.conf;
  35. include vhosts/www.def.com.conf;
  36.  
  37. }

在nginx的安装目录下新建一个conf目录,目录里有两个文件,分别为upstream.conf和www.def.com.conf 这两个文件的内容如下:

  1. [root@qht124 conf]# pwd
  2. /usr/local/nginx/conf
  3. [root@qht124 conf]# cd vhosts/
  4. [root@qht124 vhosts]# ls
  5. upstream.conf www.def.com.conf
  6. [root@qht124 vhosts]# pwd
  7. /usr/local/nginx/conf/vhosts
  8. [root@qht124 vhosts]# cat upstream.conf
  9. upstream www.def.com {
  10. server 192.168.1.9:80;
  11. }
  12. [root@qht124 vhosts]# cat www.def.com.conf
  13. server
  14. {
  15. listen 80;
  16. server_name www.def.com;
  17. charset GB2312;
  18. index index.php index.html index.htm;
  19. root /usr/vhome/d/e/f/def.com/www;
  20.  
  21. location ~ ^/NginxStatus/ {
  22. stub_status on;
  23. access_log off;
  24. }
  25.  
  26. location / {
  27. proxy_redirect off ;
  28. proxy_set_header Host $host;
  29. proxy_set_header X-Real-IP $remote_addr;
  30. proxy_set_header REMOTE-HOST $remote_addr;
  31. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  32. client_max_body_size 50m;
  33. client_body_buffer_size 256k;
  34. proxy_connect_timeout 30;
  35. proxy_send_timeout 30;
  36. proxy_read_timeout 60;
  37. proxy_buffer_size 256k;
  38. proxy_buffers 4 256k;
  39. proxy_busy_buffers_size 256k;
  40. proxy_temp_file_write_size 256k;
  41. proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
  42. proxy_max_temp_file_size 128m;
  43. proxy_pass http://www.def.com;
  44. }
  45.  
  46. #location ~* \.(jpg|jpeg|gif|png|swf|html)$ {
  47. #if (-f $request_filename) {
  48. #root /usr/vhome/d/e/f/def.com/www;
  49. #expires 1d;
  50. #break;
  51. #} ##将这一段注释掉,则所有的访问都叫给apache去处理!否则静态文件会去执行192.168.1.2下的/usr/vhome/d/e/f/def.com/www下的内容。我在192.168.1.9的/usr/vhome/d/e/f/def.com/www下方一个index.php,在192,168.1.2的/usr/vhome/d/e/f/def.com/www下放一个a.jpg和index.html文件,然后手动运行www.def.com/index.html和www.def.com/a.jpg则会执行192.168.1.2下的内容(显然是nginx处理了静态页面)
  52. #}
  53.  
  54. log_format access '$remote_addr - $remote_user [$time_local] "$request" '
  55. '$status $body_bytes_sent "$http_referer" '
  56. '"$http_user_agent" $http_x_forwarded_for';
  57. access_log /exp/nginxlogs/www.def.com_access.log access;
  58.  
  59. }
  60. [root@qht124 vhosts]#

然后再192.168.1.9上httpd.conf中的配置如下:
在httpd.conf中的最后加入一句:
Include conf/vhost.conf

在/usr/local/apache2/conf目录下vhosts.conf文件内容如下:

  1. [root@qht2 conf]# pwd
  2. /usr/local/apache2/conf
  3. [root@qht2 conf]# cat vhost.conf
  4. NameVirtualHost *
  5. <VirtualHost *>
  6. ServerName www.def.com
  7. ScriptAlias /cgi-bin/ /usr/vhome/d/e/f/def.com/cgi-bin/
  8. DocumentRoot /usr/vhome/d/e/f/def.com/www
  9. DirectoryIndex index.html index.jsp index.php
  10. <Directory /usr/vhome/d/e/f/def.com/www>
  11. Order allow,deny
  12. Allow from all
  13. </Directory>
  14. php_admin_value open_basedir /usr/vhome/d/e/f/def.com:/usr/vhome/tmp:/opt/tomcat/bin
  15. </VirtualHost>

最后在一台客户端上,我的是xp系统,做本地解析,在C:\WINDOWS\system32\drivers\etc下hosts文件添加记录如下:

  1. 192.168.1.2 def.com
  2. 192.168.1.2 www.def.com

并在1912.168.1.9上:
mkdir -p /usr/vhome/d/e/f/def.com/www
cd /usr/vhome/d/e/f/def.com/www
touch index.php
内容为:

  1. [root@qht2 www]# cat index.php
  2. <?
  3. phpinfo();
  4. ?>
  5. [root@qht2 www]# pwd
  6. /usr/vhome/d/e/f/def.com/www
  7. [root@qht2 www]#

 测试访问见附件:

你可能感兴趣的:(apache,nginx)