nginx 均衡负载配置

时间:2024-01-20 17:38:21

nginx详细配置介绍:

参考资料:http://blog.csdn.net/xmtblog/article/details/42295181

配置实例:

// nginx服务器虚拟为代理服务器和web服务器
#user nobody;
worker_processes 4 ; //一般和CPU核相等 #pid logs/nginx.pid; events { worker_connections 1024 ; //最大连接数
} http {
include mime.types; //服务器能识别的文件类型
default_type application/octet-stream;
sendfile on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; upstream myServer{ //设置服务器集群 server 192.168.1.251:80 weight=1;  //设置服务器,和访问权重
server 192.168.1.252:8080 weight=1;
} server { //设置反向代理服务器,监听端口为80
listen 80;
server_name www.myproxy.com ; #charset koi8-r; #access_log logs/host.access.log main; root /phpstudy/wwww;
location /{ # index index.html index.htm index.php
proxy_pass http://myServer; //指定反向代理的服务器集群
} #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$ { //rewrite URL 正则表达 若url地址以.php结尾,则执行下面代码
proxy_pass http://myServer;
# proxy_set_header X_Real_IP $remote_addr;
# proxy_set_header X-Host $host; }
} server{ //设置web服务器,监听端口为8080,
listen 8080;
server_name www.centos2.com;
root /phpstudy/www;
location / { index index.php index.htm index.html ;
}
#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;
} location ~ \.php(.*)$ { //若遇到以php结尾的文件就交个9000端口的 fastcgi执行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;
}
} include /phpstudy/server/nginx/conf/vhosts/*.conf;
}

注意:http下的server模块是设置虚拟服务器的,不同于upstream下的server模块,另外作为web服务器要对php文件执行,必须要指定提交给fastcgi解析,

否则浏览器无法识别将提示下午。