近来有几个刚使用nginx的新童鞋老问我,nginx+fastcgi不够稳定,偶尔出现502错误,怎么解决?本人使用nginx也有3年多了,也认为php-fpm模块不够稳定,在访问量不大的时候没事,访问量增大时易出现502,当然这个还跟nginx的一些缓存设置和超时设置有关,设置不合理就易出现。php-fpm动态生成大型页面也没有优势,有时候会使php-cgi进程变成僵尸进程。据说php 5.4版本已自带php-fpm模块,稳定性是否有改进?太新还没用过,不评论。其实本人比较喜欢nginx跑静态和做负载反向代理,动态php还是交给apache处理比较稳定,jsp就交给tomcat、resin或jboss。nginx跑静态的能力是无与伦比的,是目前web服务器里最强的。nginx和apache、tomcat、resin的动静分离配置其实很简单,就几句配置,稳定性也非常好。
1、nginx和apache的动静分离配置:
把下面配置放到nginx配置文件相应的server {}
里面,如果使用其他端口号,改一下就行:
#所有php的动态页面均交由apache处理 location ~ .(php)?$ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:88; } #所有静态文件由nginx直接读取不经过apache location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { expires 15d; } location ~ .*.(js|css)?$ { expires 1h; }
如果之前设置了FastCGI的,把下面的配置注释掉:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root /var/www/html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # include fastcgi.conf; #}
重启nginx就生效,如图所示,标头显示nginx,phpinfo里面显示是apache,说明动静分离生效。
2、nginx和tomcat、resin的动静分离配置:
同上,把下面配置放到nginx配置文件相应的server{}
里面:
#所有jsp的页面均交由tomcat或resin处理 location ~ .(jsp|jspx|do)?$ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8080; } #所有静态文件由nginx直接读取不经过resin location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { expires 15d; } location ~ .*.(js|css)?$ { expires 1h; }
暂无评论
写评论