reactjs 为什么在生产环境中部署Next.js样例项目时CSS不工作?

kx5bkwkv  于 2023-03-12  发布在  React
关注(0)|答案(3)|浏览(297)

我目前正在Docker中使用Next.jsNginx开发应用程序。
CSS不工作后,我build一个项目,并检查在浏览器中的显示如下.
我该如何解决这个问题?
要部署的过程和代码如下所示:
1.制作下一个项目

$ npx create-next-app@latest --use-npm next-nginx-docker

2.创建一个Dockerfile.prod

FROM node:16-alpine AS builder

WORKDIR /app

COPY package.json ./

COPY package-lock.json ./

RUN npm install

COPY . .

RUN npm run build

FROM nginx:1.19-alpine AS server

COPY ./etc/nginx.conf /etc/nginx/conf.d/default.conf

COPY --from=builder /app /

EXPOSE 80

3.创建一个停靠点-compose.prod.yml

version: "3"

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile.prod
    ports:
      - "80:80"

4.创建一个nginx.conf

server {
   listen 80;
   listen [::]:80;

   server_name mydomain.com;
   root /.next/server/pages;
   index index.html;

}

5.将创建的项目文件上载到远程服务器
6.在生产环境中启动容器

$ sudo docker-compose -f docker-compose.prod.yml up -d

7.在浏览器中检查显示时,CSS不工作

8.检查浏览器中显示的index.html的视图源

  • 404错误404错误提示-页面未找到

index.html

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <meta charSet="utf-8"/>
    <title>Create Next App</title>
    <meta name="description" content="Generated by create next app"/>
    <link rel="icon" href="/favicon.ico"/>
    <meta name="next-head-count" content="5"/>
    <link rel="preload" href="/_next/static/css/27d177a30947857b.css" as="style"/>
    <link rel="stylesheet" href="/_next/static/css/27d177a30947857b.css" data-n-g=""/>
    <link rel="preload" href="/_next/static/css/149b18973e5508c7.css" as="style"/>
    <link rel="stylesheet" href="/_next/static/css/149b18973e5508c7.css" data-n-p=""/>
    <noscript data-n-css=""></noscript>
    <script defer="" nomodule="" src="/_next/static/chunks/polyfills-5cd94c89d3acac5f.js"></script>
    <script src="/_next/static/chunks/webpack-69bfa6990bb9e155.js" defer=""></script>
    <script src="/_next/static/chunks/framework-00b57966872fc495.js" defer=""></script>
    <script src="/_next/static/chunks/main-551bef8982a15171.js" defer=""></script>
    <script src="/_next/static/chunks/pages/_app-f55443f2448c8e66.js" defer=""></script>
    <script src="/_next/static/chunks/pages/index-bf8a59545a570ee7.js" defer=""></script>
    <script src="/_next/static/vL3f544dl4bgUK74SRA_c/_buildManifest.js" defer=""></script>
    <script src="/_next/static/vL3f544dl4bgUK74SRA_c/_ssgManifest.js" defer=""></script>
    <script src="/_next/static/vL3f544dl4bgUK74SRA_c/_middlewareManifest.js" defer=""></script>
</head>
<body>
<div id="__next">
    <div class="Home_container__bCOhY">
        <main class="Home_main__nLjiQ"><h1 class="Home_title__T09hD">Welcome to <!-- --><a href="https://nextjs.org">Next.js!</a>
        </h1>
            <p class="Home_description__41Owk">Get started by editing<!-- --> <!-- --><code class="Home_code__suPER">pages/index.js</code>
            </p>
            <div class="Home_grid__GxQ85"><a href="https://nextjs.org/docs" class="Home_card___LpL1"><h2>Documentation
                →</h2>
                <p>Find in-depth information about Next.js features and API.</p></a><a href="https://nextjs.org/learn"
                                                                                       class="Home_card___LpL1"><h2>
                Learn →</h2>
                <p>Learn about Next.js in an interactive course with quizzes!</p></a><a
                    href="https://github.com/vercel/next.js/tree/canary/examples" class="Home_card___LpL1"><h2>Examples
                →</h2>
                <p>Discover and deploy boilerplate example Next.js projects.</p></a><a
                    href="https://vercel.com/new?utm_source=create-next-app&amp;utm_medium=default-template&amp;utm_campaign=create-next-app"
                    class="Home_card___LpL1"><h2>Deploy →</h2>
                <p>Instantly deploy your Next.js site to a public URL with Vercel.</p></a></div>
        </main>
        <footer class="Home_footer____T7K"><a
                href="https://vercel.com?utm_source=create-next-app&amp;utm_medium=default-template&amp;utm_campaign=create-next-app"
                target="_blank" rel="noopener noreferrer">Powered by<!-- --> <!-- --><span
                class="Home_logo__27_tb"><span
                style="box-sizing:border-box;display:inline-block;overflow:hidden;width:initial;height:initial;background:none;opacity:1;border:0;margin:0;padding:0;position:relative;max-width:100%"><span
                style="box-sizing:border-box;display:block;width:initial;height:initial;background:none;opacity:1;border:0;margin:0;padding:0;max-width:100%"><img
                style="display:block;max-width:100%;width:initial;height:initial;background:none;opacity:1;border:0;margin:0;padding:0"
                alt="" aria-hidden="true"
                src="data:image/svg+xml,%3csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20version=%271.1%27%20width=%2772%27%20height=%2716%27/%3e"/></span><img
                alt="Vercel Logo" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
                decoding="async" data-nimg="intrinsic"
                style="position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%"/><noscript><img
                alt="Vercel Logo" srcSet="/vercel.svg 1x, /vercel.svg 2x" src="/vercel.svg" decoding="async"
                data-nimg="intrinsic"
                style="position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%"
                loading="lazy"/></noscript></span></span></a></footer>
    </div>
</div>
<script id="__NEXT_DATA__" type="application/json">{
    "props": {
        "pageProps": {}
    },
    "page": "/",
    "query": {},
    "buildId": "vL3f544dl4bgUK74SRA_c",
    "nextExport": true,
    "autoExport": true,
    "isFallback": false,
    "scriptLoader": []
}</script>
</body>
</html>

9.检查容器中CSS文件的目录

/.next/static/css # pwd

[已更新]

与发布的答案一样,我更改了Dockerfile,但CSS不起作用

FROM node:16-alpine AS builder

WORKDIR /app

COPY package.json ./

COPY package-lock.json ./

RUN npm install

COPY . .

RUN npm run build

FROM nginx:1.19-alpine AS server

COPY ./etc/nginx.conf /etc/nginx/conf.d/default.conf

COPY --from=builder /app /
COPY --from=builder /app/public /public

EXPOSE 80

节点16.0
下一步. js 12.1

jm81lzqq

jm81lzqq1#

在HTML文件中,静态文件路径以_next开头,但Next.js生成的代码中的静态文件路径以.next开头。
您必须更改nginx配置才能从正确的位置获取CSS/静态文件。

server {
   listen 80;
   listen [::]:80;

   location ~ ^/_next/static/(.*)$ {
      root /.next;
      try_files "/static/$1" "/server/static/o$1" @proxy_pass;
   }
   server_name mydomain.com;
   root /.next/server/pages;
   index index.html;

}
7jmck4yq

7jmck4yq2#

我想你可能错过了一些东西

COPY --from=builder /app/public ./public

在你

RUN npm run build

在你的Dockerfile里,这应该能复制你的资产
在任何情况下,您都可能需要引用to the offical Dockerfile example

w80xi6nr

w80xi6nr3#

因为Next JS使用SSR,而ngix用于静态网站,所以你需要使用节点来托管下一个js网站。下面是我的下一个js应用程序docker文件,它适合我。尝试一下,然后告诉我。

# Install dependencies only when needed
FROM node: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:alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN export NODE_OPTIONS=--openssl-legacy-provider && yarn build && yarn install --production --ignore-scripts --prefer-offline

# Production image, copy all the files and run next
FROM node:alpine AS runner
WORKDIR /app

ENV NODE_ENV production

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

ENV PORT=80
EXPOSE 80

# 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"]

相关问题