title: nginx + fastcgi + docker throws 502 bad gateway
body: <p>I'm trying to run an Nginx with FastCGI in a <strong>Docker container</strong> to handle request through <strong>python script</strong> backend.</p>
  <p>The problem is that, when I post a request to nginx Docker container, I get <strong>502 bad gateway</strong> error all the time.</p>
  <pre><code>2022/03/14 07:50:05 [info] 1130#0: *2 client closed connection while waiting for request, client: 172.17.0.1, server: 0.0.0.0:8888
  
  172.17.0.1 - - [14/Mar/2022:07:50:05 +0000] &quot;GET /test.py HTTP/1.1&quot; 502 28 &quot;-&quot; &quot;PostmanRuntime/7.29.0&quot;
  </code></pre>
  <br>
  <ul>
  <li>My Dockerfile: builds nginx from source file</li>
  </ul>
  <pre><code># For python script
  FROM python:3.9-slim-bullseye
  
  # nginx, nginx upload module version
  ENV NGINX_VERSION 1.20.2
  
  # install dependencies
  RUN apt-get update \
      &amp;&amp; apt-get install -y wget gcc g++ make \
      &amp;&amp; apt-get install -y spawn-fcgi fcgiwrap zlib1g zlib1g-dev openssl libssl-dev libpcre3 libpcre3-dev
  
  # create nginx user/group and add nginx user to nginx group
  RUN addgroup --system --gid 102 nginx \
      &amp;&amp; adduser --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos &quot;nginx user&quot; --shell /bin/false --uid 102 nginx
  
  # compile and build nginx with nginx upload module
  RUN set -x \
      &amp;&amp; wget -P /usr/src &quot;https://github.com/vkholodkov/nginx-upload-module/archive/${UPLOAD_MODULE_VERSION}.tar.gz&quot; \
      &amp;&amp; tar -xzvf /usr/src/$UPLOAD_MODULE_VERSION.tar.gz -C /usr/src \
      &amp;&amp; wget -P /usr/src &quot;https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz&quot; \
      &amp;&amp; tar -xzvf /usr/src/nginx-$NGINX_VERSION.tar.gz -C /usr/src \
      &amp;&amp; cd /usr/src/nginx-$NGINX_VERSION \
      &amp;&amp; CONFIG=&quot;\
      --user=nginx \
      --group=nginx \
      --sbin-path=/usr/local/sbin/nginx \
      # --modules-path=/usr/lib/nginx/modules \
      --conf-path=/etc/nginx/nginx.conf \
      --pid-path=/var/run/nginx/nginx.pid \
      --lock-path=/var/lock/nginx/nginx.lock \
      --http-log-path=/var/log/nginx/access.log \
      --error-log-path=/var/log/nginx/error.log \
      --with-debug \
      --with-compat \
      &quot; \
      &amp;&amp; ./configure $CONFIG \
      &amp;&amp; make -j $(getconf _NPROCESSORS_ONLN) \
      &amp;&amp; make install \
      &amp;&amp; mkdir /var/lock/nginx \
      &amp;&amp; mkdir $UPLOAD_FOLDER \
      &amp;&amp; mkdir /etc/nginx/conf.d/ \
      &amp;&amp; mkdir /etc/nginx/scripts/
  
  # mount configuration and python scripts
  COPY config/nginx.conf /etc/nginx/nginx.conf
  COPY config/http.conf /etc/nginx/conf.d/http.conf
  COPY scripts /etc/nginx/scripts
  
  # change mode of *.py files in script directory
  RUN chmod 777 -R /etc/nginx/scripts
  
  # forward request, error logs to nginx-image-server Docker
  RUN ln -sf /dev/stdout /var/log/nginx/access.log \
      &amp;&amp; ln -sf /dev/stdout /var/log/nginx/error.log \
      &amp;&amp; mkdir /docker-entrypoint.d
  
  # docker entrypoint
  COPY docker-entrypoint.sh /
  COPY set-upload-store.sh /docker-entrypoint.d
  COPY fcgiwrap.sh /docker-entrypoint.d
  ENTRYPOINT [&quot;/docker-entrypoint.sh&quot;]
  
  # nginx server listening port
  EXPOSE 8888
  
  # stopsignal
  STOPSIGNAL SIGQUIT
  
  # disable nginx daemon mode
  CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]
  </code></pre>
  <ul>
  <li>test.py: for testing if nginx is handling request correctly</li>
  </ul>
  <pre><code>
  #!/usr/local/bin/python
  import cgitb
  import sys
  
  cgitb.enable(format=&quot;text&quot;)
  sys.exit(0)
  
  print(&quot;Content-Type: text/plain&quot;)
  print()
  print(&quot;Hello, world!&quot;)
  </code></pre>
  <p>Absolute path for python interpreter in Docker container is <code>/usr/local/bin/python</code>, and I checked if the test.py script is executed in Docker container.</p>
  <pre><code>$ docker exec -it f53a bash
  root@f53a67efc39e:/# /usr/local/bin/python /etc/nginx/scripts/test.py
  Content-Type: text/plain
     
  Hello, world!
  </code></pre>
  <br>
  <p>I've been trying to figure out what the cause for 502 bad gateway error, but couldn't got it through. Can someone bring me some light please?</p>
  <p><em>For more information,</em></p>
  <ul>
  <li>docker entrypoint.sh</li>
  </ul>
  <pre><code>#!/bin/sh
  spawn-fcgi -s /var/run/fcgiwrap.socket -M 766 /usr/sbin/fcgiwrap 
  </code></pre>
  <ul>
  <li>Nginx debug log: Does <code>2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi parser: 0</code> have something to do with 502 bad gateway error?</li>
  </ul>
  <pre><code>2022/03/14 07:53:11 [debug] 1129#0: *2 http run request: &quot;/test.py?&quot;
  2022/03/14 07:53:11 [debug] 1129#0: *2 http upstream check client, write event:1, &quot;/test.py&quot;
  2022/03/14 07:53:11 [debug] 1129#0: epoll: fd:4 ev:0004 d:00007F6DB26FC2E1
  2022/03/14 07:53:11 [debug] 1129#0: *2 http upstream request: &quot;/test.py?&quot;
  2022/03/14 07:53:11 [debug] 1129#0: *2 http upstream dummy handler
  2022/03/14 07:53:11 [debug] 1129#0: timer delta: 0
  2022/03/14 07:53:11 [debug] 1129#0: worker cycle
  2022/03/14 07:53:11 [debug] 1129#0: epoll timer: 2048000
  2022/03/14 07:53:11 [debug] 1129#0: epoll: fd:4 ev:2005 d:00007F6DB26FC2E1
  2022/03/14 07:53:11 [debug] 1129#0: *2 http upstream request: &quot;/test.py?&quot;
  2022/03/14 07:53:11 [debug] 1129#0: *2 http upstream process header
  2022/03/14 07:53:11 [debug] 1129#0: *2 malloc: 000055C128615180:49152
  2022/03/14 07:53:11 [debug] 1129#0: *2 recv: eof:1, avail:-1
  2022/03/14 07:53:11 [debug] 1129#0: *2 recv: fd:4 104 of 49152
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi record byte: 01
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi record byte: 06
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi record byte: 00
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi record byte: 01
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi record byte: 00
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi record byte: 46
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi record byte: 02
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi record byte: 00
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi record length: 70
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi parser: 0
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi header: &quot;Status: 502 Bad Gateway&quot;
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi parser: 0
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi header: &quot;Content-Type: text/plain&quot;
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi parser: 1
  2022/03/14 07:53:11 [debug] 1129#0: *2 http fastcgi header done
  2022/03/14 07:53:11 [debug] 1129#0: *2 HTTP/1.1 502 Bad Gateway
  Server: nginx/1.20.2
  Date: Mon, 14 Mar 2022 07:53:11 GMT
  Content-Type: text/plain
  Transfer-Encoding: chunked
  Connection: keep-alive
  2022/03/14 07:53:11 [debug] 1129#0: *2 write new buf t:1 f:0 000055C12856FCA0, pos 000055C12856FCA0, size: 165 file: 0, size: 0
  2022/03/14 07:53:11 [debug] 1129#0: *2 http write filter: l:0 f:0 s:165
  2022/03/14 07:53:11 [debug] 1129#0: *2 http cacheable: 0
  2022/03/14 07:53:11 [debug] 1129#0: *2 posix_memalign: 000055C128581BF0:4096 @16
  
  
  </code></pre>