nginx加php-fpm出现502 bad gateway错误的5种解决方法
nginx出现502有很多原因,但大部分原因可以归结为资源数量不够用,也就是说后端php-fpm处理有问题,nginx将正确的客户端请求发给了后端的php-fpm进程,但是因为php-fpm进程的问题导致不能正确解析php代码,最终返回给了客户端502错误。
服务器出现502的原因是连接超时 我们向服务器发送请求 由于服务器当前链接太多,导致服务器方面无法给于正常的响应,产生此类报错
因此如果你服务器并发量非常大,那只能先增加机器,然后按以下方式优化会取得更好效果;但如果你并发不大却出现502,一般都可以归结为配置问题,脚本超时问题。
一、php-fpm进程数不够用
使用 netstat -napo |grep "php-fpm" | wc -l 查看一下当前fastcgi进程个数,如果个数接近conf里配置的上限,就需要调高进程数。
但也不能无休止调高,可以根据服务器内存情况,可以把php-fpm子进程数调到100或以上,在4G内存的服务器上200就可以。
二、调高调高linux内核打开文件数量
可以使用这些命令(必须是root帐号)
- echo 'ulimit -HSn 65536' >> /etc/profile
- echo 'ulimit -HSn 65536' >> /etc/rc.local
- source /etc/profile
三、脚本执行时间超时
如果脚本因为某种原因长时间等待不返回 ,导致新来的请求不能得到处理,可以适当调小如下配置。
nginx.conf里面主要是如下
- fastcgi_connect_timeout 300;
- fastcgi_send_timeout 300;
- fastcgi_read_timeout 300;
php-fpm.conf里如要是如下
- request_terminate_timeout = 10s
四、缓存设置比较小
修改或增加配置到nginx.conf
- proxy_buffer_size 64k;
- proxy_buffers 512k;
- proxy_busy_buffers_size 128k;
五、 recv() failed (104: Connection reset by peer) while reading response header from upstream
可能的原因机房网络丢包或者机房有硬件防火墙禁止访问该域名
但最重要的是程序里要设置好超时,不要使用php-fpm的request_terminate_timeout,
最好设成request_terminate_timeout=0;
因为这个参数会直接杀掉php进程,然后重启php进程,这样前端nginx就会返回104: Connection reset by peer。这个过程是很慢,总体感觉就是网站很卡。
- May 01 10:50:58.044162 [WARNING] [pool www] child 4074, script '/usr/local/nginx/html/quancha/sameip/detail.php' execution timed out (15.129933 sec), terminating
- May 01 10:50:58.045725 [WARNING] [pool www] child 4074 exited on signal 15 SIGTERM after 90.227060 seconds from start
- May 01 10:50:58.046818 [NOTICE] [pool www] child 4082 started
说一千道一万最重要的就是程序里控制好超时,gethostbyname、curl、file_get_contents等函数的都要设置超时时间。
另一个就是多说,这个东西是增加了网站的交互性,但是使用的多了反应就慢了,如果你网站超时且使用了多说是,可以关闭它。
php-fpm.conf max_children 和 max_requests配置:
pm = static
假如使用静态 pm.max_children这个参数会起作用,其余不会。动态反之。
2G内存pm.max_children大概开启50左右,按照实际情况来调优,这个是很必要的。
========================================================
max_children是PHP-FPM Pool 最大的子进程数,他数值取决于你的服务器内存。 假设你打算给10G内存给当前配置的PHP-FPM Pool,一般一个PHP请求占用内存10M-40M,我们按站点每个PHP请求占用内存25M,这样max_children = 10G/25M = 409。所以,这个值可以根据情况算出来
max_requests是每个子进程重生之前处理的请求数, 默认值为unlimited(默认为1024),可以设置小一点(如500左右),这样可以避免内存泄露带来的问题
Nginx代理过程,将业务服务器请求数据缓存到本地文件,再将文件数据转发给请求客户端。高并发的客户端请求,必然要求服务器文件句柄的并发打开限制。使用ulimit命令(ulimit -n),查看Linux系统文件句柄并发限制,默认是1024,我们可以改为65535(2 的 16 次方,这是系统端口的极限)。修改的方法为:修改系统文件/etc/security/limits.conf,添加如下信息,并重新启动系统生效。
- * soft nofile 65535
- * hard nofile 65535
然后在Nginx配置文件中,把文件限制及连接数信息改为65535:
- worker_rlimit_nofile 65535;
- events {
- use epoll;
- worker_connections 65535;
- }
NGINX 重写规则
nginx rewrite 实现二级域名跳转
Nginx 静态文件缓存相关配置
- location ~ \.( html|js|css|png|gif|jpg|jpeg|bmp|swf)$ {
- #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到 upstream负载均衡池中的另一台服务器,实现故障转移
- proxy_next_upstream http_502 http_504 error timeout invalid_header;
- proxy_cache cache_one;
- #对不同的HTTP状态码设置不同的缓存时间
- proxy_cache_valid 200 10m;
- proxy_cache_valid 304 1m;
- proxy_cache_valid 301 302 1h;
- proxy_cache_valid any 1m;
- #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希
- proxy_cache_key $host$uri$is_args$args;
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded-For $remote_addr;
- #如果没有缓存则通过proxy_pass转向tomcat请求
- proxy_pass http://tomcat_server_pool;
- }
Nginx upstream 相关配置
Nginx_upstream实现:设置备份主机及过滤HTTP错误自动切除[8000=>Node服务器 || 2000=>默认错误处理页]:
- #集群中的所有后台服务器的配置信息
- upstream nodeJs {
- #server 123.56.233.208 weight=10;
- #server 127.0.0.1:3000 weight=10;
- server 127.0.0.1:8000;
- server 127.0.0.1:2000 backup;
- }
- #调用所有的Server配置文件
- include /usr/local/nginx/conf/vhosts/*.conf;
- server {
- listen 3000;
- server_name liuxinxiu.com;
- add_header Proxy-By $upstream_http_server; #代理服务器Server
- location / {
- proxy_pass http://nodeJs; #反向代理到后端Server
- proxy_set_header Host $host:3000;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- #proxy_next_upstream error timeout invalid_header http_403 http_404;
- }
- }
Nginx=>2000端口默认错误处理页(相关配置):
- server{
- listen 2000;
- server_name 127.0.0.1;
- index index.html index.htm index.php;
- root /ftp/www/2000;
- error_page 404 /index.html;
- }
NGINX 413 解决方法
Nginx Post最大提交数据限制
原以为是php本身的限制,查看配置文件无论是POST还是单个文件上传都满足要求:
#post最大提交量
post_max_size = 8M
#php给个文件最大限制
upload_max_filesize = 2M
************************************************************************
NGINX错误日志显示:
Nginx 【client intended to send too large body: 1065755 bytes】
在Nginx.conf文件中添加:
client_max_body_size 64M; #多少M根据实际情况填写
重启即可!
重启服务 sh /usr/local/resin/bin/resin.sh -server youyuan restart
查看日志
- root@pts/4 # tail -f /usr/local/resin/log/jvm-y
- jvm-youyuan.log jvm-yylc.log
- 192.168.3.150 [/www/www.youyuan.com/web/WEB-INF/classes] 2015-03-09 17:27:20
- root@pts/4 # tail -f /usr/local/resin/log/jvm-y
- jvm-youyuan.log jvm-yylc.log
- 192.168.3.150 [/www/www.youyuan.com/web/WEB-INF/classes] 2015-03-09 17:27:20
- root@pts/4 # tail -f /usr/local/resin/log/jvm-youyuan.log
- Resin-4.0.35 (built Tue, 12 Feb 2013 10:05:50 PST)
- Copyright(c) 1998-2012 Caucho Technology. All rights reserved.
- Using Resin(R) Open Source under the GNU Public License (GPL).
- See http://www.caucho.com for information on Resin Professional,
- including caching, clustering, JNI acceleration, and OpenSSL integration.
- Starting Resin on Mon, 09 Mar 2015 17:27:23 +0800 (CST)
linux apache安装mod_cband限制虚拟主机带宽
mod_cband是一个限额用户带宽,最高下载速度,每秒访问请求速度和最高并发访问ip连接数的apache2模块。
1.安装mod_cband如下:
- wget http://so.liuxinxiu.com/mod-cband-0.9.7.2.tgz
- tar zxvf mod-cband-0.9.7.2.tgz
- cd mod-cband-0.9.7.2
- ./configure --with-apxs=/usr/local/apache/bin/apxs
- make
- make install
# yum install httpd-devel
//安装
- grep cband /usr/local/apache2/conf/httpd.conf
- 如果是云安装:
- grep cband /etc/httpd/conf/httpd.conf
- #++++++++++默认入口+++++++++++++
- #<VirtualHost *:80>
- # DocumentRoot /usr/local/apache/htdocs
- # ServerName 192.168.1.233
- #</VirtualHost>
- <VirtualHost *:80>
- ServerName 192.168.1.233
- ServerAdmin admin@liuxinxiu.com
- DocumentRoot /usr/local/apache/htdocs
- CBandSpeed 1024 100 30
- CBandRemoteSpeed 50kb/s 5 3
- </VirtualHost>
开启/订制Apache目录列表样式
开启Apache目录列表:
首先找到/usr/local/apache/conf/httpd.conf 文件搜索
#Include conf/extra/httpd-autoindex.conf (去掉注释# 启用)
修改/usr/local/apache/conf/extra/httpd-autoindex.conf
注释掉:#IndexOptions FancyIndexing HTMLTable VersionSort
启用:indexOptions FancyIndexing ScanHTMLTitles NameWidth=128 DescriptionWidth=256 HTMLTable VersionSort FoldersFirst
#######################################################################
之后遇到Apache列表索引中文乱码问,在httpd-autoindex.conf文件中添加:Charset=GBK
indexOptions Charset=GBK FancyIndexing ScanHTMLTitles NameWidth=128 DescriptionWidth=256 HTMLTable VersionSort FoldersFirst
重启Apache问题解决~
虚拟主机配置处添加:
- <Directory "/data/web_bk">
- Options Indexes FollowSymLinks
- AllowOverride All
- Order allow,deny
- Order deny,allow
- Allow from all
- </Directory>
Apache 配置显示版本信息:
- ########### Add ##############
- AddDefaultCharset GBK
- ServerSignature On
- ########### Add ##############
- Include shost/*.conf
设置Discuz论坛伪静态
把下面代码添加到:
<VirtualHost *:80>
DocumentRoot /data/web_bk/bbs.XXXnet/htdocs
ServerName bbs.XXX.net
<Directory "/data/web_bk/bbs.3snews.net/htdocs">
这里.....
</Directory>
</VirtualHost>
- # 将 RewriteEngine 模式打开
- RewriteEngine On
- # Rewrite 系统规则请勿修改
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^topic-(.+)\.html$ portal.php?mod=topic&topic=$1&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^article-([0-9]+)-([0-9]+)\.html$ portal.php?mod=view&aid=$1&page=$2&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^forum-(\w+)-([0-9]+)\.html$ forum.php?mod=forumdisplay&fid=$1&page=$2&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^group-([0-9]+)-([0-9]+)\.html$ forum.php?mod=group&fid=$1&page=$2&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^space-(username|uid)-(.+)\.html$ home.php?mod=space&$1=$2&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^blog-([0-9]+)-([0-9]+)\.html$ home.php?mod=space&uid=$1&do=blog&id=$2&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^archiver/(fid|tid)-([0-9]+)\.html$ archiver/index.php?action=$1&value=$2&%1