微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP-FPM和Nginx:502错误的网关

组态

ubuntu服务器11.10 64位

在AWS上托pipe的Amazon AWS,Ec2

t1.micro实例

在我写任何东西之前,我想说明我已经检查了Nginx 502错误网关和Nginx + PHP-FPM 502错误的网关线程,不幸的是在这方面没有帮助。

这个问题似乎是相当普遍的:configuration错误NginxPHP-fpm可能会导致502 Bad Gateway错误,这是我一直无法摆脱的东西。 请注意, 即使我转到我的域根目录时 , 也会出现这种情况,而不指定任何特定的目录。

我运行的是Amazon EC2networking服务器,启用了端口9000,端口80打开等。

AWS Linux(Ubuntu)托pipe应用程序不能从公共IP访问

打开GTS构build失败

在ec2上安装mscorefonts

Linux启动时启动java程序(EC2实例)

如何让SOLR在远程服务器上运行

这个问题特别是,我怎样才能摆脱这个肮脏的错误呢? 或者,更好的是,我怎样才能让PHP5-fpm 实际工作 。

我迄今为止的尝试

configuration文件的编辑大多是一致的,特别是PHP-fpm.conf和Nginx.conf 。

一世。 PHP-fpm.conf

添加了以下内容,这并没有太多帮助:

;;;;;;;;;;;;; ; Fpm Start ; ;;;;;;;;;;;;; ;pm.start_servers = 20 ;pm.min_spare_servers = 5 ;pm.max_spare_servers = 35

现在,之后我试着包括我的configuration文件

include=/etc/PHP5/fpm/*.conf

这只会把我搞得更深。

完整configuration

;;;;;;;;;;;;;;;;;;;;; ; FPM Configuration ; ;;;;;;;;;;;;;;;;;;;;; ; All relative paths in this configuration file are relative to PHP's install ; prefix (/usr). This prefix can be dynamicaly changed by using the ; '-p' argument from the command line. ; Include one or more files. If glob(3) exists,it is used to include a bunch of ; files from a glob(3) pattern. This directive can be used everywhere in the ; file. ; Relative path can also be used. They will be prefixed by: ; - the global prefix if it's been set (-p arguement) ; - /usr otherwise ;include=/etc/PHP5/fpm/*.conf ;;;;;;;;;;;;;;;;;; ; Global Options ; ;;;;;;;;;;;;;;;;;; [global] ; Pid file ; Note: the default prefix is /var ; Default Value: none pid = /var/run/PHP5-fpm.pid ; Error log file ; Note: the default prefix is /var ; Default Value: log/PHP-fpm.log error_log = /var/log/PHP5-fpm.log ; Log level ; Possible Values: alert,error,warning,notice,debug ; Default Value: notice log_level = notice ; If this number of child processes exit with SIGSEGV or SIGBUS within the time ; interval set by emergency_restart_interval then FPM will restart. A value ; of '0' means 'Off'. ; Default Value: 0 ;emergency_restart_threshold = 0 ; Interval of time used by emergency_restart_interval to determine when ; a graceful restart will be initiated. This can be useful to work around ; accidental corruptions in an accelerator's shared memory. ; Available Units: s(econds),m(inutes),h(ours),or d(ays) ; Default Unit: seconds ; Default Value: 0 emergency_restart_interval = 0 ; Time limit for child processes to wait for a reaction on signals from master. ; Available units: s(econds),or d(ays) ; Default Unit: seconds ; Default Value: 0 ;process_control_timeout = 0 ; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging. ; Default Value: yes daemonize = no ;;;;;;;;;;;;; ; Fpm Start ; ;;;;;;;;;;;;; ;pm.start_servers = 20 ;pm.min_spare_servers = 5 ;pm.max_spare_servers = 35 ;;;;;;;;;;;;;;;;;;;; ; Pool DeFinitions ; ;;;;;;;;;;;;;;;;;;;; ; Multiple pools of child processes may be started with different listening ; ports and different management options. The name of the pool will be ; used in logs and stats. There is no limitation on the number of pools which ; FPM can handle. Your system will tell you anyway :) ; To configure the pools it is recommended to have one .conf file per ; pool in the following directory: include=/etc/PHP5/fpm/pool.d/*.conf

iiNginx.conf

诚实地说,这个configuration是我访问过的一些网站的一小部分,但是我可以告诉你,在这502坏的网关业务之前,服务器运行正常(没有PHP工作时间)。

这个问题主要在于事情非常糟糕,非常错误。 而现在,当我尝试做一个service PHP5-fpm restart ,它挂在我猜测是一个无限循环或什么,我甚至不能CTRL – C出来。

完整configuration

user www-data; worker_processes 1; pid /var/run/Nginx.pid; events { worker_connections 64; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush off; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/Nginx/mime.types; default_type application/octet-stream; ## # Logging Settings ## access_log /var/log/Nginx/access.log; error_log /var/log/Nginx/error.log; ## # Gzip Settings ## gzip on; gzip_disable "msie6"; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+RSS text/javascript; ## # Virtual Host Configs ## include /etc/Nginx/conf.d/*.conf; include /etc/Nginx/sites-enabled/*; server { listen 80; server_name ec2-xx-xx-xx-xx.compute-x.amazonaws.com; location ~ ^(.+.PHP)(.*)$ { root /home/wayvac/public; fastcgi_pass unix:/var/run/PHP5-fpm.pid; #fastcgi_pass 127.0.0.1:9000; #Un-comment this and comment "fastcgi_pass unix:/var/run/PHP-fpm/PHP-fpm.sock;" if you are not using PHP-fpm. fastcgi_index index.PHP; set $document_root2 $document_root; if ($document_root2 ~ "^(.*\\).*?[\\|/]../(.*)$") { set $document_root2 $1$2; } if ($document_root2 ~ "^(.*\\).*?[\\|/]../(.*)$") { set $document_root2 $1$2; } if ($document_root2 ~ "^(.*\\).*?[\\|/]../(.*)$") { set $document_root2 $1$2; } if ($document_root2 ~ "^(.*\\).*?[\\|/]../(.*)$") { set $document_root2 $1$2; } if ($document_root2 ~ "^(.*\\).*?[\\|/]../(.*)$") { set $document_root2 $1$2; } fastcgi_split_path_info ^(.+.PHP)(.*)$; fastcgi_param SCRIPT_FILENAME $document_root2$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root2$fastcgi_path_info; include fastcgi_params; fastcgi_param DOCUMENT_ROOT $document_root2; } access_log /var/log/Nginx/access.log; error_log /var/log/Nginx/error.log; location / { root /home/wayvac/public; index index.html index.htm index.PHP; } location ~* .(?:ico|css|js|gif|jpe?g|png)$ { # Some basic cache-control for static files to be sent to the browser expires max; add_header Pragma public; add_header Cache-Control "public,must-revalidate,proxy-revalidate"; } #include drop.conf; #include PHP.conf; } }

Amazon EC2:无法卸载和删除EBS驱动器文件系统

Nginx试图下载文件而不是显示

AWS实例运行时间

如何在Windows EC2实例中执行UserData内容

亚马逊Linux AMI vs Ubuntu

如果有人遇到同样的问题,我发现这个网页,我在这里找到答案: http : //www.howtoforge.com/installing-Nginx-with-PHP5-and-PHP-fpm-and-MysqL-support-lemp -酮的ubuntu-12.04-LTS

对于那些不能自己点击和解决问题的人…;)

条件:

Ubuntu或Debian服务器与NginxPHP 5.3工作正常,但升级PHP到5.4给502坏网关错误。 寻找在端口9000上运行的服务(通常运行netstat -lp或类似的)不会返回任何内容

修正:

打开/etc/PHP5/fpm/pool.d/www.conf并记下“listen”参数(在我的例子中是/var/run/PHP5-fpm.sock ):

; The address on which to accept FastCGI requests. ; Valid Syntaxes are: ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses on a ; specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. listen = /var/run/PHP5-fpm.sock

并用您刚才提到的位置替换您的虚拟主机中的fastcgi_pass变量。

所以这个示例symfony2配置(取自http://wiki.Nginx.org/Symfony ):

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ ^/(app|app_dev).PHP(/|$) { fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^(.+.PHP)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; }

变成这样:

# pass the PHP scripts to FastCGI server at /var/run/PHP5-fpm.sock location ~ ^/(app|app_dev).PHP(/|$) { fastcgi_pass unix:/var/run/PHP5-fpm.sock; fastcgi_split_path_info ^(.+.PHP)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; }

然后重新启动Nginx

sudo /etc/init.d/Nginx restart

注意:替换~ ^/(app|app_dev).PHP(/|$) { ~ ^/index.PHP(/|$) {如果你不在SF2上

希望这可以节省一些时间:)

编辑

当然,你可以在/etc/PHP5/fpm/pool.d/www.conf中将listen = /var/run/PHP5-fpm.sock改为listen = 127.0.0.1:9000 ,然后重新启动PHP5-fpm(这会不必改变你的虚拟主机),但是你必须假定他们改变了PHP5-fpm来运行socket而不是在端口9000上运行。

EDIT2

如果您仍然遇到502错误,请参阅此答案: Nginx错误连接到PHP5-fpm.sock失败(13:权限被拒绝)

尝试设置这些值,它解决了fast-CGI中的问题

fastcgi_buffer_size 16k; fastcgi_buffers 4 16k;

我做了所有这些类似的调整,但不时我得到501/502错误(每天)。

这是我在/etc/PHP5/fpm/pool.d/www.conf中的设置,以避免501和502Nginx错误…服务器有16Gb的RAM。 这个配置是一个8Gb的RAM服务器,所以…

sudo nano /etc/PHP5/fpm/pool.d/www.conf

然后设置以下值

pm.max_children = 70 pm.start_servers = 20 pm.min_spare_servers = 20 pm.max_spare_servers = 35 pm.max_requests = 500

在这个改变之后重新启动PHP-fpm

sudo service PHP-fpm restart

如果你像我这样升级PHP-fpm后遇到问题,试试这个:打开/etc/PHP5/fpm/pool.d/www.conf,取消注释如下:

listen.owner = www-data listen.group = www-data listen.mode = 0666

然后重新启动PHP-fpm。

不要忘了, PHP-fpm是一项服务。 安装完成后,请确保启动它:

# service PHP-fpm start # chkconfig PHP-fpm on

对于任何其他人努力达到这一点,我试图调整超时建议,因为我不想停止使用Unix套接字…经过大量的故障排除,并没有太多继续我发现这个问题正在造成通过几个月前在PHP-fpm中启用的APC扩展。 禁用这个扩展解决了间歇性的502错误,最简单的方法是通过注释掉以下行:

;extension = apc.so

这为我做了诡计!

在5.4中将端口改为9001,只需将Nginx conf中的端口从9000更改为9001,并在PHP-fpm配置中为我工作。

希望这个技巧会拯救别人的生命。 在我的情况下,问题是我用尽了内存,但只是稍微,很难考虑。 浪费了3小时。 我建议运行:

sudo htop

要么

sudo free -m

…在服务器上运行有问题的请求,看看你的内存是否没有用完。 如果它像我的情况,你需要创建交换文件(除非你已经有一个)。

我已经按照这个教程在Ubuntu server 14.04上创建交换文件,它工作得很好: http : //www.cyberciti.biz/faq/ubuntu-linux-create-add-swap-file/

我也发现这个错误可能会导致写入json_encoded()数据到MysqL。 为了解决它我base64_encode()的JSON。 请不要解码时,JSON编码可以更改值。 铌。 24可以变成24.00

在网络上尝试了所有的解决方案后,我就结束了使用非常简单的方法解决了这个问题,首先我尝试了PHP-fpm err log

cat /var/log/PHP5-fpm.log

最重复的错误

" WARNING: [pool www] server reached pm.max_children setting (5),consider raising it "

我编辑PHP-fpm池设置

nano /etc/PHP5/fpm/pool.d/www.conf

我绞死了这条线

pm.max_children = 5

为了新的价值

pm.max_children = 10

顺便说一句我正在使用128MB内存的低端VPS像其他人一样,我想使用pm.max_children将使我的服务器运行得更快消耗更少的内存,但是我们使用的设置太低,即使启动PHP-fpm进程。 我希望这可以帮助其他人,因为我经过24小时的测试和失败后发现这一点,我的网络主机支持无法解决问题。

你应该看到错误日志。 认情况下,它的位置在/var/log/Nginx/error.log

在我的情况下,502得到方式,因为:

GET /app_dev.PHP HTTP/1.1",upstream: "fastcgi://unix:/run/PHP/PHP7.0-fpm.sock:",host: "symfony2.local" 2016/05/25 11:57:28 [error] 22889#22889: *3 upstream sent too big header while reading response header from upstream,client: 127.0.0.1,server: symfony2.local,request: "GET /app_dev.PHP HTTP/1.1",host: "symfony2.local" 2016/05/25 11:57:29 [error] 22889#22889: *3 upstream sent too big header while reading response header from upstream,host: "symfony2.local"

当我们确切地知道什么是错的,然后修复它。 对于这些错误,只需修改缓冲区:

fastcgi_buffers 16 512k; fastcgi_buffer_size 512k;

我对这个游戏已经很晚了,但是我的问题是在升级服务器上的PHP的时候开始的。 我能够删除.socket文件并重新启动我的服务。 那么,一切工作。 不知道为什么它有所作为,因为该文件大小为0,所有权和权限是相同的,但它的工作。

在搞乱Nginx配置之前,先尝试先禁用ChromePHP

1 – 打开app / config / config_dev.yml

2 – 评论这些行:

chromePHP: type: chromePHP level: info

ChromePHP将调试信息json编码在X-ChromePHP-Data标头中,这对于使用fastcgi的Nginx认配置来说太大了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐