简体   繁体   English

从 docker 容器访问 nginx 时出现 502 错误网关

[英]502 bad gateway when access nginx from a docker container

I am trying to create a php inside my docker container我正在尝试在我的 docker 容器中创建一个 php

However, when I go to the localhost on my browser, I keep getting the 502 bad gateway.但是,当我在浏览器上访问本地主机时,我不断收到 502 错误网关。

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我的 nginx conf 看起来像这样

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.我是 docker 和 PHP 的新手,所以如果我需要提供任何其他信息,请告诉我。

Did you start php-fpm ?你启动php-fpm吗?

Is port 9000 open? 9000端口打开了吗?

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
    
      }
    
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM