Errors

[php-fpm] nginx(웹 서버)와 php-fpm 을 연결했을 때 502 (Bad Gateway) 에러 해결

이제곱 2023. 3. 12. 23:46

에러 내용

php-fpm 을 사용하는 도커 컨테이너 하나와 웹 서버로 nginx 를 사용하는 도커 컨테이너 하나를 띄우고 둘 사이에 통신할 수 있게 세팅하는 와중에 자꾸 502 에러나 connection reset by peer 가 발생하였다.

발생한 에러

curl -v localhost:1234
*   Trying ::1:1234...
* Connected to localhost (::1) port 1234 (#0)
> GET / HTTP/1.1
> Host: localhost:1234
> User-Agent: curl/7.74.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 502 Bad Gateway
< Server: nginx/1.20.2
< Date: Sun, 05 Mar 2023 11:09:50 GMT
< Content-Type: text/html
< Content-Length: 157
< Connection: keep-alive
< 
<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.20.2</center>
</body>
</html>
* Connection #0 to host localhost left intact

php-fpm 측에서 에러 로그가 안떠서 원인 파악이 힘들었는데 그냥 설정 파일을 잘못 건드려서 문제가 생긴 것이었다.

기존 설정 파일

docker-compose.yml

 wordpress:
    container_name: wordpress
    image: wordpress
    ...
    networks:
      - inception
    expose:
      - "9000"
    init: true
    restart: on-failure
  nginx:
    container_name: nginx
    image: nginx
    ...
    networks:
      - inception
    ports:
      - "443:443"
    init: true
    restart: on-failure
networks:
  inception:
    name: inception
    driver: bridge

 

 

동일한 docker bridge 네트워크에 넣고 컨테이너명:포트로 nginx -> wordpress(php-fpm running) 으로 통신 시도했는데도 안됐다..

 

 

default.conf (nginx config file)

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    index       index.php index.html index.htm;
    ...

    error_page  404  /404.html;
    error_page  500 502 503 504/50x.html;

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass wordpress:9000; # .php 요청은 wordpress:9000 으로 보낸다.
        fastcgi_param SCRIPT_FILENAME /wordpress/$fastcgi_script_name;
     }
}

 

 

www.conf (php-fpm config file)

; Start a new pool named 'www'.
; the variable $pool can be used in any directive and will be replaced by the
; pool name ('www' here)
[www]

user = nobody

listen = 0.0.0.0:9000

; Default Value: any
listen.allowed_clients = 0.0.0.0

해결책

php-fpm 설정 파일의 access 설정이 잘못되어서 문제였다.
listen.allowed_clients 의 기본 설정은 이미 any 인데 굳이 0.0.0.0 으로 모든 주소로부터 요청을 받으려고 시도했다..
심지어 예시 컨피그 파일에 아주 친절하게 써져있었다.

; List of addresses (IPv4/IPv6) of FastCGI clients which are allowed to connect.
; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
; must be separated by a comma. If this value is left blank, connections will be
; accepted from any ip address.
; Default Value: any

DefaultValue.. ANY!

해당 줄을 삭제해주면 아주 잘 해결이 된다.
listen 도 port 만 주어졌을 때 모든 ip 를 listen 하는 것이 기본값이므로 9000만 작성해 주었다.

변경 후 config file

; Start a new pool named 'www'.
; the variable $pool can be used in any directive and will be replaced by the
; pool name ('www' here)
[www]

user = nobody

listen = 9000

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

access.log = /var/log/php8/access.logs