Nginx负载均衡简易方法
做个简单的测试,一个Nginx, 通过FastCGI协议和另外两台服务器上的基于CppCMS开发的web server通信。配置方法很简单:
首先,必须在nginx.conf文件开头,server 配置之前添加下面的upstream信息:
upstream backend { server 192.168.1.55:8080; server 192.168.1.58:8080; }
然后在某个server配置里面,包含了fastcgi的配置文件:
include ./conf.d/fastcgi.test;
在这个fastcgi.test中,沿用CppCMS需要的fastcgi大多数配置, 除了将fastcgi_pass后面的IP:port 改成前面设置的backend.
location ~ ^/my_site_path.*$ { fastcgi_pass backend; fastcgi_split_path_info ^(/my_site)(.*)$; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_NAME /my_site; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; }
现在测试一下,关闭一台web server,网页依然能够正常浏览。