首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用nginx在docker容器中运行react应用程序

无法使用nginx在docker容器中运行react应用程序
EN

Stack Overflow用户
提问于 2022-07-08 19:01:24
回答 1查看 53关注 0票数 0

我试图运行一个反应应用程序在一个码头容器使用码头-多级。服务器是在deno上编写的,我试图添加nginx服务器来将来自前端的请求分派给服务器。

Dockerfile:

代码语言:javascript
复制
FROM ubuntu:20.04
# install curl
RUN apt-get update && apt-get install -y curl unzip sudo nginx

# install node.js v16.x
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
RUN apt-get install -y nodejs

# install postgresql
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
  curl vim wget \
  build-essential \
  libpq-dev &&\
  apt-get update && apt-get install -y tzdata nodejs yarn postgresql postgresql-contrib

# install deno v1.21.3
RUN curl -fsSL https://deno.land/install.sh | sh -s v1.21.3
ENV DENO_INSTALL="/root/.deno"
ENV PATH="${DENO_INSTALL}/bin:${PATH}"

# Install denon
RUN deno install -qAf --unstable https://raw.githubusercontent.com/nnmrts/denon/patch-4/denon.ts

# The working directory of the project
WORKDIR /app

# Copy the app package and package-lock.json file
COPY frontend/build/ /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/conf.d/default.conf


# Copy the backend directory
RUN mkdir backend
COPY backend/deps.ts ./backend/deps.ts 
RUN cd ./backend && deno cache --unstable deps.ts

ADD backend ./backend

EXPOSE 3000

COPY ./script.sh script.sh
CMD ./script.sh

script.sh:

代码语言:javascript
复制
#!/bin/bash

# create the database in postgres
service postgresql start && su - postgres -c "psql -U postgres -d postgres -c \"alter user postgres with password 'postgres';\"" \
  && su - postgres -c "psql postgres -c \"create database db;\""

# start nginx
sudo service nginx start


# Populate database tables
cd backend && deno run --unstable --allow-env --allow-net database/seeds.ts && denon start &


# Wait for any process to exit
wait -n
  
# Exit with status of process that exited first
exit $?

nginx.conf:

代码语言:javascript
复制
server {
   listen 3000;
   root /usr/share/nginx/html;
   location / {
    try_files $uri $uri/ /index.html;
    proxy_pass  http://localhost:5000/;
  }
}

我塑造了这样的形象:

代码语言:javascript
复制
docker build . -t server-app

我创建了一个新容器:

代码语言:javascript
复制
docker run -p 3000:3000 server-app

一切正常,deno服务器正在监听5000端口,但是当我在localhost:3000/上运行应用程序时,我得到了以下错误:

代码语言:javascript
复制
The connection was reset

The connection to the server was reset while the page was loading.

    The site could be temporarily unavailable or too busy. Try again in a few moments.
    If you are unable to load any pages, check your computer’s network connection.
    If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.

我做的配置有什么问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-08 20:43:35

我通过更新nginx.conf来解决这个问题:

代码语言:javascript
复制
server {
   listen  3000;
   server_name localhost;
   root /usr/share/nginx/html;
   location / {
      root /usr/share/nginx/html;
      index index.html index.htm;
      try_files $uri $uri/ /index.html =404;
   }
   location /api {
      rewrite ^/api/?(.*) /$1 break;
      proxy_pass  http://localhost:5000;
   }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72915920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档