Nginx转发找不到PHP服务,报502错误?

Docker配置

version: '3.5'

services:

  nginx:
    image: nginx:alpine
    restart: always
    volumes: 
      - ./config/nginx:/etc/nginx/conf.d
      - ./logs/nginx:/var/log/nginx
      - ./projects:/workspace
    ports: 
      - 80:80
      - 443:443
    links:
      - php

  php:
    image: itxiaoma/php8swoole:1.2
    restart: always
    volumes:
      - ./projects:/workspace
    ports:
      - 8000:8000
    stdin_open: true
    tty: true

Nginx配置

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}
upstream php_swoole {
    server php:8000;
}

server {
    listen 80;
    server_name management.test;
    server_tokens off;
    root /workspace/management/public;

    index index.php;

    charset utf-8;

    location /index.php {
        try_files /not_exists @octane;
    }

    location / {
        try_files $uri $uri/ @octane;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log  /var/log/nginx/domain.com-error.log error;

    error_page 404 /index.php;

    location @octane {
        set $suffix "";

        if ($uri = /index.php) {
            set $suffix ?$query_string;
        }

        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_pass http://php_swoole$suffix;
    }
}

请求出现错误:

2021/11/19 09:52:49 [error] 21#21: *13 connect() failed (111: Connection refused) while connecting to upstream, client: 172.28.0.1, server: management.test, request: "GET / HTTP/1.1", upstream: "http://172.28.0.2:8000/?", host: "management.test"

swoole是laravel的octane

php artisan octane:start --server=swoole --port=8000
阅读 2.8k
2 个回答

试了一下,应该是很 listen 的 host 有关系,octane 里面默认 listen 的是 127.0.0.1 然后在其他容器就访问不到,如果加上参数 host 参数指定一下 0.0.0.0 就可以了

php artisan octane:start --host=0.0.0.0
新手上路,请多包涵

猜测是nginx这段配置有问题,nginx应该是容器内部运行的

upstream php_swoole {
    server php:8000;
}

可以改成这样试试

server 主网ip/内网ip:8000

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题