记一次nginx负载均衡配置情况
准备工作
- 1台负载均衡服务器(x.x.x.29),2台web服务器(x.x.x.30,x.x.x.31)
- 在29安装nginx,在30和31分别装上PHP和nginx
29服务器的nginx配置
worker_processes 1; events { worker_connections 1024; } http { upstream x.x.x.29 { server x.x.x.30:8080;//web服务器尽量避免监听80端口 server x.x.x.31:8080; } server { listen 80; server_name localhost; location / { proxy_pass http://x.x.x.29;//将所有请求代理到x.x.x.29这一块,就是上面的upstream } } }
30和31服务器的nginx配置
配置好之后,访问x.x.x.29,就会把请求分发到30和31服务器上。server { listen 8080; server_name localhost; root xx/xxx/public; location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } }
注意
如果有上传图片的功能,需要保证静态资源能共享,解决方案:1.30和31挂载一个共享磁盘存储文件,2.使用第三方存储系统,如oss。
本作品采用《CC 协议》,转载必须注明作者和本文链接