[英]How to rightfully connect dockerized api and frontend (next's application with strapi)
几天以来,我试图在 dockerized 环境中使用 strapi 作为 api 部署我的 next.js 站点。
到目前为止,我已经发现我的前端无法使用“普通”DNS,因此会将 api url 解析为内部 docker ip。 这首先没有问题,但是我的 docker 应用程序(前端和后端)都在使用 https 的 Hynix 代理后面运行。 为了实现这一点,我在我的服务器上使用jwilder nginx-proxy代理。
现在的问题是,由于它使用的是内部 ip,因此我无法将 https 用于 api url。 它适用于使用 http 的构建,没有任何问题,但是,例如,一旦我尝试通过 axios 加载内容,它就会抛出(令人惊讶的)混合内容错误。
我想我错过了如何解决整个烂摊子的最后提示。
这是我的 Dockerbuild 文件:
Nextjs前端
# Install dependencies only when needed
FROM node:14-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
# Rebuild the source code only when needed
FROM node:14-alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
ENV NODE_ENV=production
ENV NEXT_PUBLIC_STRAPI_API_URL=http://<myapi.com>:1337
RUN yarn build
# Production image, copy all the files and run next
FROM node:14-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV process=production
ENV NEXT_PUBLIC_STRAPI_API_URL=http://<myapi.com>:1337
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
USER nextjs
EXPOSE 3000
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
# ENV NEXT_TELEMETRY_DISABLED 1
CMD ["yarn", "start"]
API (Strapi) 后端
FROM strapi/base
WORKDIR /backend
COPY ./package.json ./
COPY ./yarn.lock ./
RUN yarn install
COPY . .
ENV NODE_ENV production
ENV CLOUDINARY_NAME <name>
ENV CLOUDINARY_API_KEY <apikey>
ENV CLOUDINARY_API_SECRET <apisec>
RUN yarn build
EXPOSE 1337
CMD ["yarn", "start"]
Docker 撰写
version: "3"
services:
backend:
image: backend:latest
restart: always
ports:
- "1337:1337"
volumes:
- strapidata:/data
environment:
VIRTUAL_HOST: <myapi.com>
LETSENCRYPT_HOST: <myapi.com>
container_name: <myapi.com>
frontent:
image: frontend:latest
restart: always
ports:
- "1338:3000"
depends_on:
- backend
environment:
VIRTUAL_HOST: <myfrontend.com>
LETSENCRYPT_HOST: <myfrontend.com>
NEXT_PUBLIC_STRAPI_API_URL: <myapi.com>:1337
container_name: <myfrontend.com>
networks:
default:
external:
name: webproxy
volumes:
strapidata:
非常感谢!
编辑:所以只是为了澄清,我所描述的主要问题是,当使用 https:// 时,它不会解决,因为我的前端将使用 docker dns 将 https:// 请求解析到内部 ip,它不会接受 https//,因为 ssl 证书是通过 ngnix 反向代理提供的。 因此,我需要一个解决方案,说明如何在不获取混合错误内容的情况下使用内部网络(尝试从我的 Webbrowser 获取数据),或者使用公共 DNS 解析 api 调用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.