從 Node Docker 容器連接到上游時主機無法存取

從 Node Docker 容器連接到上游時主機無法存取

我有一個 API backend.mysite.local,可以直接從瀏覽器存取它。但是,當我嘗試從 Node 容器內呼叫 API 時,收到 502 Bad Gateway 並顯示以下錯誤:

連線到上游時 connect() 失敗(113:主機無法存取),客戶端:172.20.0.1,伺服器:www.mysite.local,請求:“GET /api/items HTTP/1.1”,上游:“http://172.20.0.3:3000/api/items”,主機:“www.mysite.local”

這是我的nginx 配置(我是nginx 的新手,所以我可能會以更短的方式完成它,但它主要是從端口80 到端口443 的重定向塊- 所以如果您也查看它,我將不勝感激)

server {
    listen 443 ssl;
    server_name backend.mysite.local;
    ssl_certificate /etc/ssl/mysite.local.crt;
    ssl_certificate_key /etc/ssl/mysite.local.key;
    
    index index.php index.html;
    server_name backend.mysite.local;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ \.php$ {        
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;

        
        set $METHODS  'GET, POST, OPTIONS, HEAD';
        set $HEADERS  'Authorization, Origin, X-Requested-With, Content-Type, Accept';
        
        
        if ( $request_method = POST ){
          add_header 'Access-Control-Allow-Origin' 'https://www.mysite.local';
          add_header 'Access-Control-Allow-Credentials' 'true';
        }

    }
    
}

server {
    listen 80;
    server_name http.backend.mysite.local;
    return 301 https://backend.mysite.local$request_uri;

}

server {
    listen 80;
    server_name nginx;
    return 301 https://backend.mysite.local$request_uri;

}



server {
    listen 80;
    server_name www.mysite.local;
    return 301 https://www.mysite.local$request_uri;
}

server {
    listen 80;
    server_name mysite.local;
    return 301 https://www.mysite.local$request_uri;
}

server {
    listen 443 ssl;
    server_name mysite.local;
    ssl_certificate /etc/ssl/mysite.local.crt;
    ssl_certificate_key /etc/ssl/mysite.local.key;
    return 301 https://www.mysite.local$request_uri;
}


  server {   
    listen 443 ssl;
    server_name www.mysite.local;     
    ssl_certificate /etc/ssl/mysite.local.crt;
    ssl_certificate_key /etc/ssl/mysite.local.key;
    

     location / {      
     proxy_pass      http://node:3000;
      
    }
  }

編輯:這是docker-compose.yml

networks:
    mysite:
        driver: bridge

services:
    nginx:
        image: nginx:stable-alpine
        container_name: nginx
        ports:
            - "8088:8088"
            - "80:80"  
            - "443:443"          
   
        networks:
            - mysite                

    php:        
        build:
            context: ./laravel
            dockerfile: Dockerfile
        container_name: php
        ports:
            - "9000:9000"
        networks:
            - mysite

    node:
        build:
            context: ./react
            dockerfile: Dockerfile
        container_name: react
        
        ports:
            - "3000:3000"       

相關內容