繁体   English   中英

在服务器上的 Docker 容器中创建 React 和 Laravel 之间的内部连接

[英]Create internal connection between React and Laravel in Docker containers on server

我使用 React 应用程序作为前端,使用 Laravel 应用程序作为后端。 这两者通过 Laravel Sanctum API 相互连接。 整个环境使用Docker部署在服务器上,前端和后端是独立的容器,但与network: someNetwork

The API call is done from the frontend using the URL HTTP://myserverip:8000 - this is working, but I would like to close the 8000 port (externally) and just keep open the 3000 port where the frontend is working. 现在,当我关闭 8000 端口(带防火墙)并尝试从前端发出 API 请求时,出现网络错误。

问题是,如何在内部发出 API 请求,这样我只能保持 3000 个打开,我是否需要在 nginx 的 .conf 文件中进行某种重定向? 这是 my.conf 文件:

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

谢谢,任何提示将不胜感激。

你试图做的事情永远不会奏效。

ReactJS(我猜这就是 ReactApp 的意思)在用于生产之前应该“构建”。 结果将是一个包含一堆 static 文件的build文件夹。

这些文件可以使用 NGINX 部署/服务。 来自 ReactJS(您的前端应用程序)的 API 调用将始终来自您的网络外部,因为客户端/客户将不属于您的 Docker NAT 网络(我猜)。

我将使用 NGINX 作为您的 ReactApp(ReactJS 构建)的 Web 服务器以及您的 Laravel 后端的反向代理/代理。

就像是:

server {
  listen 443 ssl;
  server_name example.com
  .....
  root /path/to/your/reactapp/static/files;
 
  location / {
   try_files $uri /index.html;
  }
}

server {
  listen 443 ssl;
  server_name api.example.com;
  
  ...all the laravel PHP config here

}

如果您不能或不想为您的 API 创建子域,请使用位置

server {
  listen 443 ssl;
  server_name example.com
  .....
  root /path/to/your/reactapp/static/files;
 
  location / {
   try_files $uri /index.html;

  }
  
  location /api/ {
    Do your Laravel config here (use nested locations for handling the php files)
    Or do a `proxy_pass` and configure an internal server (listen `8000`) and do      not expose it.
  }

}

暂无
暂无

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

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