1.nginx配置
server{
listen 80; //监听端口号
server_name 域名 localhost 外网ip; //多个域名和ip之间可以用空格隔开
set $root 项目地址;//设置root变量
location ~ .*\.(mp4)$
{
client_body_buffer_size 2048m;
client_max_body_size 120m;
root $root;//项目存放地址
}
//设置php访问 fastcgi配置
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000; //fastcgi定向的地方
fastcgi_connect_timeout 1600; //最大链接时间
fastcgi_send_timeout 1600;//最大发送时间
fastcgi_read_timeout 1600;//最大接收时间
fastcgi_index index.php; //默认访问文件
include fastcgi_params;
#下面两句是给fastcgi权限,可以支持 ?s=/module/controller/action的url访问模式
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#下面两句才能真正支持 index.php/index/index/index的pathinfo模式
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
2.配置负载均衡
upstream apiproxy2{
server ip:端口号 weight=30;
server ip:端口号 weight=30;
server 127.0.0.1:80 weight=30;
}
server{
location /api {
proxy_read_timeout 50;
proxy_next_upstream http_502 http_504 http_404 error invalid_header; #实现失效转移 不配罿timeout 因为 它会导致重复提交
proxy_pass http://apiproxy2; //配置的负载
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host; #让服务器取得的到 客户端ip 而不是 nginx的内网ip
}
}
3.fastcgi_和proxy区别
- fastcgi请求过程
Nginx本身不支持PHP等语言,但是它可以通过FastCGI来将请求扔给某些语言或框架处理(例如PHP、Python、Perl)
2.proxy请求
用proxy来实现负载均衡