我昨天刚刚创建了一个配置,其中 NGINX 服务器作为负载均衡器,后面有 2 个 PHP-FPM 服务器、1 个 Memcache 服务器和 1 个 MySQL 服务器。NGINX 是使用 Upstreaming 特性配置的,相关的配置行是这样的:
html {
...
# loadbalancing
upstream myLoadBalancer {
ip_hash; # makes sure same user uses the same server, not 100% effective - application
# should handle this; in my case 1 Memcached and 1 MySQL servers commonly used
# by all App-servers work just fine. I store sessions in Memcache, so Session
# management isn't a problem at all. Its shared across all App-servers.
server 192.168.1.10:9000; # location of my first php-fpm server
server 192.168.1.11:9000; # second php-fpm server
# server aaa.bbb.ccc.ddd:80; # let's say, an Apache server
}
#vhost
server {
listen 80;
server_name mydomain.com;
index index.php;
location ~* \.php$ {
gzip on;
try_files $uri =404;
include fastcgi_params;
fastcgi_pass myLoadBalancer;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /path/to/webdir$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
}
}
HTTPS:如果我没记错的话,它们应该安装在 NGINX 负载均衡器上——我自己从未尝试过。一旦客户端请求被传递到 App-server,它将处理它,获取请求并将其发送回 NGINX。NGINX 在将响应转发给客户端之前,会对内容进行加密。它当然是一个理论。但我 100% 肯定 NGINX 可以很容易地处理 SSL。当与各种 CLI 的 FASTCGI 功能结合使用时,它是最快、最强大的代理/平衡器和良好的网络服务器。
注意:这不是生产环境的配置,而是测试用例场景。生产环境需要很多安全设置。以下资源可能有用: