从一个容器到另一个容器的Nginx反向代理在Laravel应用中返回404

r6vfmomb  于 2023-10-17  发布在  Nginx
关注(0)|答案(1)|浏览(164)

首先,让我声明,我已经在这个问题上工作了几天,并尝试了许多解决方案。有两个服务userdiet。这是饮食Docker撰写文件

version: '3'
networks:
  common_network:
    external: true
  diet_network:
    name: diet_network
    driver: bridge

services:
  diet_php:
     ...
  diet_nginx:
    build:
      context: .
      dockerfile: nginx.Dockerfile
    container_name: diet_nginx
    restart: unless-stopped
    image: diet_nginx
    tty: true
    ports:
      - "8015:80"
    networks:
      - diet_network
      - common_network
    depends_on:
      - diet_php
    volumes:
      - .:/var/www/html
  diet_mysql:
    - ...

volumes:
  mysqldata:
    driver: local

这是用户docker-compose文件和default.conf

version: '3'

networks:
  common_network:
    external: true
  user_network:
    name: user_network
    driver: bridge

services:
  user_php:
     ...
  user_nginx:
    build:
      context: .
      dockerfile: nginx.Dockerfile
    container_name: user_nginx
    image: user_nginx
    restart: unless-stopped
    tty: true
    ports:
      - "8014:80"
    networks:
      - user_network
      - common_network
    volumes:
      - .:/var/www/html
    depends_on:
      - user_php
  user_mysql:
       ...

volumes:
  mysqldata:
    driver: local

这是用户服务的default.conf

server {
    listen 80;
    server_name localhost;
    root /var/www/html/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    client_max_body_size 64M;

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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


    location ~ \.php$ {
        fastcgi_pass user_php:9000;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;

    }

    location /diet {
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    Host $http_host;
        proxy_set_header    X-Forwarded-Proto $scheme;
        proxy_pass          http://diet_nginx;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

diet_nginxuser_nginx可以在容器内互相ping,这是可以的。我需要从用户服务向饮食服务发出HTTP请求。这是用户服务的api.php中的路由

Route::get('/', function () {
    return 'default index';
});

Route::get('/hello-world', function () {
    $client = new GuzzleHttp\Client();
//    $res = $client->get('user_nginx/api');
    $res = $client->get('user_nginx/diet/api/hello');
    return $res;
});

以及饮食服务的api.php中的路线

Route::get('/hello', function () {
    return 'hi!';
});

我确信这个路由存在,http://localhost:8015/api/hello返回响应。但是用户服务的调用路由http://localhost:8014/api/hello-world不工作,返回404 not found错误。
值得一提的是,

$res = $client->get('user_nginx/api');

工作并返回响应。

at0kjp5o

at0kjp5o1#

对于那些将来会看到这一点的人,我把代理通行证改为follows,它起作用了

location /diet/ {
    proxy_pass   http://diet_nginx/;
}

相关问题