php - 502 bad gateway when access nginx from a docker container ← (PHP, HTML)

I am trying to create a php inside my docker container

However, when I go to the localhost on my browser, I keep getting the 502 bad gateway.

The error log shows

[error] 11#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: (my IP), server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"

My nginx conf looks like this

server {
  listen       80;
  server_name  jenkins.local;
  root         /var/www/html;
  index        index.php;
  access_log   /var/log/nginx/localhost-access.log;
  error_log    /var/log/nginx/localhost-error.log;

  location / {

    try_files $uri $uri/ /index.php?$args;

  }

  location ~ \.php$ {

    try_files $uri =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    fastcgi_intercept_errors on;

  }

}

I am new to docker and PHP so please let me know if I need to provide any other information.

Answer



Solution:

Did you start php-fpm?

Is port 9000 open?

Brief edition

 upstream websocketserver {
      server localhost:8080;
  }

  server {
      listen       80;
      server_name  jenkins.local; //Or try localhost;
      root         /var/www/html;
      index        index.php;

      access_log   /var/log/nginx/localhost-access.log;
      error_log    /var/log/nginx/localhost-error.log;
    
      location / {
    
        proxy_pass http://websocketserver;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_read_timeout 86400; # neccessary to avoid websocket timeout disconnect
        proxy_send_timeout 900s;
        proxy_redirect off;
    
      }
    
      location ~ \.php$ {
    
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock; //Check if your php is 7.4, run php -v
    
      }
    
    }

Source