- 论坛徽章:
- 3
|
1.大家也可以讨论下工作中遇到此事的解决方法。
如果是整体布局架构我们是由硬件(多服务器用何种架构组合)到软程序(程序代码如何规划,是否采用组件扩展的写法等)的规划,但是遇到了问题以后我们一般是由内而外,先对程序等软性问题进行处理,包括:增加带宽(购买网宿的CDN全站加速)、减少文件的HTTP连接数(包括用CSS精灵)、PHP动态内容静态化、SQL语句优化、数据库分表结构等,如果软性故障排除,再考虑是否需要添加硬件等。
2.2/3/4台服务器如何实现网站的负载均衡高可用呢?
这里以3台服务器组成一个负载均衡高可用为例,1台前端机器安装nginx做负载均衡和WEB缓存服务器(外界访问的都是指向这台机器的IP),后端是2台apache主机,后端2台机器都安装有mysql,并把mysql做主从复制(Replication)的数据同步(这个具体过程配置查百度,大理论就不多说了!)。这样在后端一台机器宕机的时候,另一台可以随时切换过去。
这种架构的优点是整体框架结构和配置简单。缺点是无论是缓存还是负载均衡的总体效率上不是很高!
附加以前做测试时用的一个nginx配置表,很重要!大部分主要配置都在这里,一些常见的应用部分都已做了中文注释,当然商业化应用肯定不只这么简单,还需要做很多更详细的组合配置。
user nginx nginx;
worker_processes 2;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
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 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
################################################
#负载均衡部分
#192.168.1.55是前端nginx主机,244,245是后端apache.
###############################################
upstream test {
#server 192.168.1.55;
ip_hash;
server 192.168.1.244;
server 192.168.1.245;
}
########################################################
#
# The default server
#
server {
listen 80;
server_name 192.168.1.55;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
###########################
#代理部分设置
###########################
proxy_pass http://test;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# rewrite ^/photo/([0-9]+)\.html$ /sinaphoto.php?pid=$1 last;
}
########################
#伪静态重定向
########################
rewrite ^/photo/(.+)\.html$ /sinaphoto.php?pid=$1 last;
error_page 404 /404.htm;
location = /404.html {
root /usr/share/nginx/html;
}
location ~ .*\.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
# deny access to .htaccess files, if Apache's document root
concurs with nginx's one
location ~ /\.ht {
deny all;
}
########################
#简单防盗链
######################
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
valid_referers none blocked 192.168.1.244;
if ( $invalid_referer ) {
rewrite ^/ http://$host/upload/logo.jpg;
}
if (-f $request_filename){
expires 90d;
break;
}
}
location ~ .*\.(js|css)?$
{
expires 24h;
}
}
# Load config files from the /etc/nginx/conf.d directory
#include /etc/nginx/conf.d/*.conf;
}
3.如何在负载均衡高可用/成本上取一个平衡?
如问题2里的结构,后端在2台负载不了的情况下,可以增加到3台,甚至4台,在nginx配置中多增加一个代理。甚至可以把纯静态的部分JS、CSS、图片等,单独列为一台进行处理,尽可能在保证低成本的情况下,完成高可用的稳定和速度!从而达到一个比较理想状态的平衡! |
|