docker nginx 不通过上游 Next.JS 提供静态文件

docker nginx 不通过上游 Next.JS 提供静态文件

我无法从其他类似的问题中找到我的问题的答案。

因此,我有两个 Docker 容器:

  • Next.JS 网页应用
  • nginx 反向代理

如果我通过连接到 nginx 容器,docker exec -it...我可以看到与共享卷绑定的静态文件。

我使用 docker-compose 运行它们:

volumes:
  nextjs-build:

version: '3.9'

services:
  nginx:
    image: arm64v8/nginx:alpine
    container_name: nginx
    ports:
      - "80:80"
      - "443:443"
    networks:
      - blog
    restart: unless-stopped
    depends_on:
      - website-front
    volumes:
      - type: volume
        source: nextjs-build
        target: /nextjs
        read_only: true
      - type: bind
        source: /etc/ssl/private/blog-ssl
        target: /etc/ssl/private/
        read_only: true
      - type: bind
        source: ./nginx/includes
        target: /etc/nginx/includes
        read_only: true
      - type: bind
        source: ./nginx/conf.d
        target: /etc/nginx/conf.d
        read_only: true
      - type: bind
        source: ./nginx/dhparam.pem
        target: /etc/nginx/dhparam.pem
        read_only: true
      - type: bind
        source: ./nginx/nginx.conf
        target: /etc/nginx/nginx.conf
        read_only: true

  website-front:
    build: ./website
    container_name: website-front
    ports:
      - "3000"
    networks:
      - blog
    restart: unless-stopped
    volumes:
      - nextjs-build:/app/.next

networks:
  blog:
    external:
      name: nat

我的 nginx 配置:

upstream nextjs_upstream {
  server website-front:3000;
}
server {
    listen       443 http2 ssl;
    listen       [::]:443 http2 ssl;


    server_name  website_url;

    ssl_certificate         /etc/ssl/private/chain.crt;
    ssl_certificate_key     /etc/ssl/private/server.key;
    ssl_trusted_certificate /etc/ssl/private/ca.ca-bundle;

    # access_log  /var/log/nginx/host.access.log  main;

    # security
    include     includes/security.conf;
    include     includes/general.conf;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;

    location /_next {
        proxy_pass http://nextjs_upstream/.next;
    }

    location / {
        proxy_pass http://nextjs_upstream;
    }

}

尝试了多个 nginx 配置静态路由:

localhost /_next {
   root /nextjs;
}

NextJS dockerfile:

FROM node:alpine AS deps
# this ensures we fix simlinks for npx, Yarn, and PnPm
RUN apk add --no-cache libc6-compat
RUN corepack disable && corepack enable
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# builder
FROM node:alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN yarn build

# runner
FROM node:alpine AS runner
WORKDIR /app

ENV NODE_ENV production

COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/yarn.lock ./yarn.lock

RUN chown -R node:node /app/.next

USER node

EXPOSE 3000

CMD [ "yarn", "start" ]

通过该配置,我可以看到我的网站,但是对于静态文件,我通过上游获得了 404。

相关内容