PHP-FPM 및 Nginx : 502 불량 게이트웨이
구성
- Ubuntu Server 11.10 64 비트
- 클라우드에서 호스팅되는 Amazon AWS, Ec2
- t1.micro 인스턴스
다른 글을 작성하기 전에 nginx 502 불량 게이트웨이 와 Nginx + PHP-FPM 502 불량 게이트웨이 스레드를 모두 확인했는데, 이는 불행히도 이와 관련하여 도움이되지 않았습니다.
이 문제는 다소 일반적인 것으로 보입니다. nginx 또는 php-fpm을 잘못 구성하면 502 Bad Gateway오류가 발생할 수 있으며 , 이는 제가 제거 할 수 없었던 것입니다. 특정 디렉토리를 지정하지 않고 내 도메인 루트로 이동할 때도 나타납니다 .
포트 9000이 활성화되고 포트 80이 열린 상태로 Amazon EC2 웹 서버를 실행하고 있습니다.
특히 문제는이 불쾌한 오류를 어떻게 제거 할 수 있습니까? 또는 더 나은 아직, 어떻게 얻을 수 php5-fpm에 실제로 작동 .
내가 지금까지 시도한 것
구성 파일, 특히 php-fpm.conf및 nginx.conf.
나는. php-fpm.conf
다음을 추가했지만 그다지 도움이되지 않았습니다.
;;;;;;;;;;;;;
; Fpm Start ;
;;;;;;;;;;;;;
;pm.start_servers = 20
;pm.min_spare_servers = 5
;pm.max_spare_servers = 35
이제 구성 파일을 포함 해 보았습니다.
include=/etc/php5/fpm/*.conf
더 이상 나를 망쳤습니다.
전체 구성
;;;;;;;;;;;;;;;;;;;;;
; 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), m(inutes), h(ours), 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
ii . nginx.conf
솔직히 말해서이 구성은 내가 방문한 몇 개의 웹 사이트를 흩 뿌린 것입니다.하지만이 502 Bad Gateway 사업 이전에는 서버가 정상적으로 작동하고 있었다고 말할 수 있습니다 (PHP가 작동하지 않습니다. 기간.).
문제는 주로 무언가가 끔찍하고 끔찍하게 잘못되었다는 사실에 있습니다. 나는이 작업을 수행 할 때 그리고 지금, service php5-fpm restart그것은 내가 추측하고있어, 무한 루프 또는 뭔가에 달려있는 내가조차 할 수 CTRL- C아웃.
전체 구성
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;
}
}
누군가 내가 가진 것과 동일한 문제가 발생 하여이 페이지를 찾은 경우 여기 에서 답을 찾았습니다 .
클릭하고 스스로 해결하는 것을 귀찮게 할 수없는 분들을 위해 ...;)
조건:
NGINX 및 PHP 5.3이 설치된 Ubuntu 또는 Debian 서버는 정상적으로 작동하지만 PHP를 5.4로 업그레이드하면 502 Bad Gateway 오류가 발생합니다. 포트 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 구성 ( 여기 에서 가져옴 ) :
# 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에하지 않으면 **
이것이 누군가에게 약간의 시간을 절약하기를 바랍니다 :)
편집하다
물론, 당신은 변경할 수 listen = /var/run/php5-fpm.sock하기 listen = 127.0.0.1:9000에 /etc/php5/fpm/pool.d/www.conf(당신의 가상 호스트를 변경할 필요에서 당신을 절약 할 수있는) 한 다음 다시 시작 PHP5-FPM,하지만 당신은 그들이 소켓을 통해 실행보다는에 대해 포트 9000에서 수신 대기하는 PHP5-FPM을 변경 가정해야 이유.
편집 2
여전히 502 오류가 발생하는 경우이 답변을 참조하세요 .
이 값을 설정하면 fast-cgi에서 문제가 해결됩니다.
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
I made all this similar tweaks, but from time to time I was getting 501/502 errors (daily).
This are my settings on /etc/php5/fpm/pool.d/www.conf to avoid 501 and 502 nginx errors… The server has 16Gb RAM. This configuration is for a 8Gb RAM server so…
sudo nano /etc/php5/fpm/pool.d/www.conf
then set the following values for
pm.max_children = 70
pm.start_servers = 20
pm.min_spare_servers = 20
pm.max_spare_servers = 35
pm.max_requests = 500
After this changes restart php-fpm
sudo service php-fpm restart
If you met the problem after upgrading php-fpm like me, try this: open /etc/php5/fpm/pool.d/www.conf uncomment the following lines:
listen.owner = www-data
listen.group = www-data
listen.mode = 0666
then restart php-fpm.
Don't forget that php-fpm is a service. After installing it, make sure you start it:
# service php-fpm start
# chkconfig php-fpm on
For anyone else struggling to get to the bottom of this, I tried adjusting timeouts as suggested as I didn't want to stop using Unix sockets...after lots of troubleshooting and not much to go on I found that this issue was being caused by the APC extension that I'd enabled in php-fpm a few months back. Disabling this extension resolved the intermittent 502 errors, easiest way to do this was by commenting out the following line :
;extension = apc.so
This did the trick for me!
The port was changed to 9001 in 5.4, just changing the port from 9000 to 9001 in the nginx conf and in php-fpm configuration worked for me.
Hope this tip will save someone else's life. In my case the problem was that I ran out of memory, but only slightly, was hard to think about it. Wasted 3hrs on that. I recommend running:
sudo htop
or
sudo free -m
...along with running problematic requests on the server to see if your memory doesn't run out. And if it does like in my case, you need to create swap file (unless you already have one).
I have followed this tutorial to create swap file on Ubuntu Server 14.04 and it worked just fine: http://www.cyberciti.biz/faq/ubuntu-linux-create-add-swap-file/
I've also found this error can be caused when writing json_encoded() data to MySQL. To get around it I base64_encode() the JSON. Please not that when decoded, the JSON encoding can change values. Nb. 24 can become 24.00
All right after trying every solution on the web I ended up figuare out the issue using very simple method , first I cheked php-fpm err log
cat /var/log/php5-fpm.log
and the most repeated error was
" WARNING: [pool www] server reached pm.max_children setting (5), consider raising it "
I edit PHP-fpm pools setting
nano /etc/php5/fpm/pool.d/www.conf
I chenged This Line
pm.max_children = 5
To new Value
pm.max_children = 10
BTW I'm using low end VPS with 128MB ram As everyone else I was thinkin redusing pm.max_children will make my server run faster consume less memory , but the setting we using were too low tho even start PHP-fpm process . I hope this help others since I found this after 24 hour testing and failing , ever my webhost support were not able to solve the issue .
You should see the error log. By default, its location is in /var/log/nginx/error.log
In my case, 502 get way because of:
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", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "symfony2.local"
2016/05/25 11:57:29 [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", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "symfony2.local"
2016/05/25 11:57:29 [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", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "symfony2.local"
When we know exactly what is wrong, then fix it. For these error, just modify the buffer:
fastcgi_buffers 16 512k;
fastcgi_buffer_size 512k;
I'm very late to this game, but my problem started when I upgraded php on my server. I was able to just remove the .socket file and restart my services. Then, everything worked. Not sure why it made a difference, since the file is size 0 and the ownership and permissions are the same, but it worked.
Before messing with Nginx config, try to disable ChromePHP first.
1 - Open app/config/config_dev.yml
2 - Comment these lines:
chromephp:
type: chromephp
level: info
ChromePHP pack the debug info json-encoded in the X-ChromePhp-Data header, which is too big for the default config of nginx with fastcgi.
In your NGINX vhost file, in location block which processes your PHP files (usually location ~ \.php$ {) through FastCGI, make sure you have next lines:
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
After that don't forget to restart fpm and nginx.
Additional:
NGINX vhost paths
/etc/nginx/sites-enabled/- Linux- '/usr/local/etc/nginx/sites-enabled/' - Mac
Restart NGINX:
sudo service nginx restart- Linuxbrew service restart nginx- Mac
Restart FPM:
Determine fpm process name: - systemctl list-unit-files | grep fpm - Linux - brew services list | grep php - Mac
and then restart it with:
sudo service <service-name> restart- Linuxbrew services restart <service-name>- Mac
참고URL : https://stackoverflow.com/questions/10003978/php-fpm-and-nginx-502-bad-gateway
'Program Club' 카테고리의 다른 글
| 왜 사용! (0) | 2020.10.15 |
|---|---|
| jquery 클릭이 ajax 생성 콘텐츠에서 작동하지 않습니다. (0) | 2020.10.15 |
| "strlen (s1)-strlen (s2)"는 0보다 작지 않습니다. (0) | 2020.10.15 |
| 가져 오기 android.support.v7을 확인할 수 없습니다. (0) | 2020.10.15 |
| 기본 관련 프로그램으로 파일을 여는 방법 (0) | 2020.10.14 |