nginx简单实现负载均衡
2015-07-08 11:00
3731 查看
本机ip:192.168.88.134
设置两个虚拟ip:
/sbin/ifconfig eth0:1 192.168.88.135 broadcast 192.168.88.255 netmask 255.255.255.0 up
/sbin/route add -host 192.168.88.135 dev eth0:1
/sbin/ifconfig eth0:2 192.168.88.136 broadcast 192.168.88.255 netmask 255.255.255.0 up
/sbin/route add -host 192.168.88.136 dev eth0:2
nginx.conf 配置如下
解析ip必须开启php-cgi
spawn-fcgi -a 127.0.0.1 -p 8000 -C 10 -u nobody -f /usr/bin/php-cgi
sbin/nginx -s reload
/etc/init.d/nginx restart
test.php:
http://192.168.88.136:8080/test.php
打印结果为:
string(14) "192.168.88.134" 或者 string(14) "192.168.88.135"
两个IP地址的来回切换说明这个负载使用的简单的轮训处理机制
文章参考:
http://www.cnblogs.com/xiaogangqq123/archive/2011/03/04/1971002.html http://blog.chinaunix.net/uid-301743-id-4801730.html
设置两个虚拟ip:
/sbin/ifconfig eth0:1 192.168.88.135 broadcast 192.168.88.255 netmask 255.255.255.0 up
/sbin/route add -host 192.168.88.135 dev eth0:1
/sbin/ifconfig eth0:2 192.168.88.136 broadcast 192.168.88.255 netmask 255.255.255.0 up
/sbin/route add -host 192.168.88.136 dev eth0:2
nginx.conf 配置如下
#user nobody; worker_processes 4; error_log logs/error.log; error_log logs/error.log notice; error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 1024; } 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; sendfile on; tcp_nopush on; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; #keepalive_timeout 0; keepalive_timeout 90; tcp_nodelay on; #fastcgi settings fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; #gzip settings gzip on; gzip_min_length 1k; gzip_buffers 4 16k; #gzip_http_version 1.1 gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml gzip_vary on; #允许客户端请求的最大单个文件字节数 client_max_body_size 50m; #缓冲区代理缓冲用户端请求的最大字节数,可以理解为保存到本地再传给用户 client_body_buffer_size 256k; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; proxy_connect_timeout 300s; #nginx跟后端服务器连接超时时间(代理连接超时) proxy_read_timeout 300s; #连接成功后,后端服务器响应时间(代理接收超时) proxy_send_timeout 300s; proxy_buffer_size 64k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传递请求,而不缓冲到磁盘 proxy_ignore_client_abort on; #不允许代理端主动关闭连接 #配置负载均衡实例 upstream 192.168.88.136 //负载均衡地址 { server 192.168.88.134:8080; server 192.168.88.135:8080; # server 192.168.88.136:8080; } server { listen 192.168.88.134:8080; server_name 192.168.88.134; #charset koi8-r; access_log logs/host.access.log main; location / { # root html; # index index.html index.htm index.php; #autoindex on; } #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:8000; fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name; include fastcgi_params; } location ~ \.cgi$ { root html; fastcgi_pass 127.0.0.1:7000; 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; #} } #set localhost server # another virtual host using mix of IP-, name-, and port-based configuration # server { listen 192.168.88.135:8080; server_name 192.168.88.135; location / { root html; index index.html index.htm index.php; } location ~ \.cgi$ { root html; fastcgi_pass 127.0.0.1:7000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } location ~ \.php$ { root /usr/local/nginx/html2; fastcgi_pass 127.0.0.1:8000; fastcgi_index index.php; #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name; include fastcgi_params; } } server { listen 192.168.88.136:8080; server_name 192.168.88.136; location / { root html; index index.html index.htm index.php; proxy_pass http://192.168.88.136; //所有这个server的请求处理都由配置的其他负载处理 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ \.cgi$ { root html; fastcgi_pass 127.0.0.1:7000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } } }
解析ip必须开启php-cgi
spawn-fcgi -a 127.0.0.1 -p 8000 -C 10 -u nobody -f /usr/bin/php-cgi
sbin/nginx -s reload
/etc/init.d/nginx restart
test.php:
<?php // phpinfo(); var_dump($_SERVER['SERVER_ADDR']); ?>
http://192.168.88.136:8080/test.php
打印结果为:
string(14) "192.168.88.134" 或者 string(14) "192.168.88.135"
两个IP地址的来回切换说明这个负载使用的简单的轮训处理机制
文章参考:
http://www.cnblogs.com/xiaogangqq123/archive/2011/03/04/1971002.html http://blog.chinaunix.net/uid-301743-id-4801730.html
相关文章推荐
- 在Nginx服务器下配置StartSSL和SSL的教程
- ubuntu nginx 创建多个站点
- nginx+php的配置
- Nginx/LVS/HAProxy 负载均衡软件的优缺点详解
- Centos搭建PHP5.3.8+Nginx1.0.9+Mysql5.5.17
- Nginx 负载均衡配置和策略
- nginx配置
- nginx配置为windows服务中的坑
- 一、linux下nginx1.7.8+php5.6.3的环境配置
- 完美解决Nginx 504 Gateway time-out 502 Bad Gateway
- 构建基于Nginx的文件服务器思路与实现
- Nginx 编译,添加未编译安装模块ngx_cache_purge
- nginx 1.6.2源码分析1 499问题
- CentOS 6.2 上编译安装 Nginx 1.0.15 + PHP 5.3.16 + MySQL 5.1.62
- nginx配置负载均衡
- unix,mac下安装nginx
- nginx解析php文件及问题
- 08-nginx网站的配置过程记录
- windows下配置nginx+php
- GoAccess nginx日志分析工具