AWS 上的 docker 容器中无法访问节点服务器

问题描述 投票:0回答:1

我制作了一个 React Web 应用程序,它使用 dockerfile 和supervisord.conf 构建,并部署到带有负载均衡器的 aws 容器中。我的 React 应用程序在 localhost:3000 上运行,可以通过 http://www.jermasearch.com/

访问

我想向我的 React 应用程序添加一个数据库连接,它需要一个客户端密钥环境变量来进行数据库连接/查询。为了安全地包含这个秘密变量,我正在尝试制作一个简单的小型后端节点服务器,它与我的 React 应用程序一起运行。

当我使用反应应用程序的命令

npm start
和节点服务器的命令
node server.js
在本地运行我的存储库时,它们都启动正常。我可以向
http://localhost:3030/dbtest
发出邮递员请求,它会返回预期的正确文本响应:
Hello from the Node.js server!

如果我构建 docker 映像并运行它,如果我向相同的 url 发出邮递员请求,/dbtest 路由也可以工作。

但是当我将代码推送到我的 aws ec2 容器并尝试向生产 url

http://jermasearch.com:3030/dbtest
http://jermasearch.com:3030/dbtest
发出请求时,它返回的 React 应用程序页面的 html 内容不正确:

<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <link rel="icon" href="/favicon.ico" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta name="description" content="Web site created using create-react-app" />
    <link rel="apple-touch-icon" href="/logo192.png" />
    <link rel="manifest" href="/manifest.json" />
    <title>React App</title>
    <script defer="defer" src="/static/js/main.9daeaa7a.js"></script>
    <link href="/static/css/main.f855e6bc.css" rel="stylesheet">
</head>

<body><noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
</body>

</html>

我的所有文件都在这里:https://github.com/MartinBarker/aws-react-docker-ghactions

当前我的 dockerfile 复制构建的react-app 和 server.js 内容,然后公开端口并运行最终命令:

# Specify a base image
FROM node:18-alpine as build

# Set the working directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application files
COPY ./ ./

# Build the React application
RUN npm run build

# Use a multi-stage build to keep the final image small
FROM nginx:alpine

# Install supervisor and Node.js
RUN apk add --no-cache supervisor nodejs npm

# Set the working directory
WORKDIR /app

# Copy nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf

# Copy the built application from the previous stage
COPY --from=build /app/build /usr/share/nginx/html

# Copy the source files for the Node server and React app
COPY server.js ./server.js
COPY package*.json ./
COPY src/ ./src/
COPY public/ ./public/

# Copy node_modules from the build stage
COPY --from=build /app/node_modules ./node_modules

# Install production dependencies (this may be optional if all dependencies are already installed in the build stage)
RUN npm install --only=production

# Copy the supervisord configuration
COPY supervisord.conf /etc/supervisord.conf

# Expose ports
EXPOSE 80 3000 3030

# Command to run supervisord
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]

在 dockerfile 的末尾,它运行 mu

supervisord.conf
文件运行
npm start
node server.js
以及将输出指向一些日志文件:

[supervisord]
nodaemon=true

[program:nginx]
command=nginx -g 'daemon off;'
autostart=true
autorestart=true
stdout_logfile=/var/log/nginx.log
stderr_logfile=/var/log/nginx_err.log

[program:node-server]
command=node server.js
autostart=true
autorestart=true
stdout_logfile=/var/log/node_server.log
stderr_logfile=/var/log/node_server_err.log

[program:react-app]
command=npm start
directory=/app
autostart=true
autorestart=true
stdout_logfile=/var/log/react_app.log
stderr_logfile=/var/log/react_app_err.log

我检查了我的 aws 日志,react-app 和 node-server 似乎都启动良好:

2024-07-03 09:04:58,002 CRIT Supervisor is running as root.  Privileges were not dropped because no user is specified in the config file.  If you intend to run as root, you can set user=root in the config file to avoid this message.
2024-07-03 09:04:58,006 INFO supervisord started with pid 1
2024-07-03 09:04:59,009 INFO spawned: 'nginx' with pid 7
2024-07-03 09:04:59,014 INFO spawned: 'node-server' with pid 8
2024-07-03 09:04:59,018 INFO spawned: 'react-app' with pid 9
2024-07-03 09:05:00,859 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2024-07-03 09:05:00,860 INFO success: node-server entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2024-07-03 09:05:00,860 INFO success: react-app entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2024-07-03 09:11:51,682 WARN received SIGTERM indicating exit request
2024-07-03 09:11:51,683 INFO waiting for nginx to die
2024-07-03 09:11:53,719 INFO stopped: nginx (exit status 0)

我在我的网址上运行了 nmap,看起来端口 3030 应该打开,我如何在生产中向它发出请求?我的 nginx 配置是否不允许端口访问?

$ nmap www.jermasearch.com
Warning: Nmap may not work correctly on Windows Subsystem for Linux.
For best performance and accuracy, use the native Windows build from https://nmap.org/download.html#windows.
Starting Nmap 7.80 ( https://nmap.org ) at 2024-07-03 02:30 PDT
Problem binding to interface , errno: 92
socket_bindtodevice: Protocol not available
Problem binding to interface , errno: 92
socket_bindtodevice: Protocol not available
NSOCK ERROR [0.6100s] mksock_bind_device(): Setting of SO_BINDTODEVICE failed (IOD #1): Protocol not available (92)
NSOCK ERROR [0.6110s] mksock_bind_device(): Setting of SO_BINDTODEVICE failed (IOD #2): Protocol not available (92)
Problem binding to interface , errno: 92
socket_bindtodevice: Protocol not available
...
Problem binding to interface , errno: 92
socket_bindtodevice: Protocol not available
Nmap scan report for www.jermasearch.com (54.177.88.67)
Host is up (0.16s latency).
Other addresses for www.jermasearch.com (not scanned): 54.176.90.55
rDNS record for 54.177.88.67: ec2-54-177-88-67.us-west-1.compute.amazonaws.com
Not shown: 998 filtered ports
PORT     STATE SERVICE
80/tcp   open  http
3030/tcp open  arepa-cas

Nmap done: 1 IP address (1 host up) scanned in 14.93 seconds

我什至尝试确保我的

nginx.conf
文件允许 :3030 端口:

worker_processes 1;

events {
  worker_connections 1024;
}

http {
  include mime.types;
  default_type application/octet-stream;

  sendfile on;
  keepalive_timeout 65;

  server {
    listen 80;

    location /internal-api {
        proxy_pass http://localhost:3030/;
        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 $scheme;
    }

    location / {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';

      root /usr/share/nginx/html;
      index index.html;
      try_files $uri $uri/ /index.html;
    }
  }
}

我的 server.js 运行在哪里:

const express = require('express');
const cors = require('cors');
const app = express();
const port = 3030;

console.log('server.js staring')

// Enable CORS for all routes
app.use(cors());

app.get('/dbtest', (req, res) => {
  res.send('Hello from the Node.js server!');
});

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

reactjs node.js docker nginx amazon-ec2
1个回答
1
投票

Nmap 表明您有 2 个端口打开,80 和 3030:

Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-07-02 12:04 PDT
Nmap scan report for www.jermasearch.com (54.176.90.55)
Host is up (0.011s latency).
Other addresses for www.jermasearch.com (not scanned): 54.177.88.67
rDNS record for 54.176.90.55: ec2-54-176-90-55.us-west-1.compute.amazonaws.com
Not shown: 998 filtered tcp ports (no-response)
PORT     STATE SERVICE
80/tcp   open  http
3030/tcp open  arepa-cas

Nmap done: 1 IP address (1 host up) scanned in 10.46 seconds

但是获取数据按钮只能访问端口 80,而不是 3030。 访问端口 3030 时,似乎显示相同的输出。

根据您的 docker 文件,端口 80、3000 和 3030 应该打开。 您的主管配置在端口 3000 上运行 CRA(不是构建),在端口 3030 上运行后端,以及 nginx(80)。

Nginx 在端口 80 上,但端口 80 和 3030 都显示 nginx 输出。 您的 nginx 配置还声明您的后端应该由

/internal-data/
访问,但返回
502 Bad Gateway
错误,表明服务器已关闭。

您的日志还表明

node-server
进程和
react-app
进程在初始化后不久关闭。即使他们开始了,似乎还是有错误。

react-app
返回错误代码 127,表示未找到脚本。这可能是由于您没有安装
node_modules
而是尝试使用
react-scripts
。这个过程也是多余的,因为 nginx 提供构建的文件。

node-server
应用程序以代码 1 退出,该代码提供的信息很少。我怀疑这可能与 nginx 也在端口 3030 上有关。

总的来说,这意味着 nginx 占用端口 3030 时出现错误,节点服务器无法启动,并且可能您的 API 访问了错误的 API。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.