【发布时间】:2021-12-02 15:48:31
【问题描述】:
我使用docker-compose 构建和运行 SSR Nuxt.JS 应用程序。 Web 服务器是nginx。
我不能使用 nginx 反向代理来运行这个应用程序,因为目录dist 不是在/app/ 中创建的。这是我的Dockerfile:
FROM node:14.17.0-alpine
WORKDIR /app
COPY . /app
RUN apk add python make g++
RUN yarn install
RUN yarn build
ENV HOST 0.0.0.0
ENV PORT 3000
ENV NODE_ENV production
EXPOSE 3000
CMD [ "yarn", "start" ]
它不起作用。但是如果我在我的 docker 主机路径 /home/path/project-1/ 中运行这个命令(路径 Nuxt.JS 文件存在):
yarn start
我可以通过以下方式暂时查看应用程序:
http://ip:3000
我搜索并尝试了一些解决方案,但都没有奏效(很遗憾)。
这是我在终端中运行yarn start 时的日志:
yarn run v1.22.11
$ nuxt start
ℹ [HPM] Proxy created: /api/ -> https://api.site.com/api
ℹ [HPM] Proxy rewrite rule created: "^/api/" ~> ""
╭───────────────────────────────────────────╮
│ │
│ Nuxt.js @ v2.14.3 │
│ │
│ ▸ Environment: production │
│ ▸ Rendering: server-side │
│ ▸ Target: server │
│ │
│ Memory usage: 32.7 MB (RSS: 105 MB) │
│ │
│ Listening: http://ip:3000/ │
│ │
╰───────────────────────────────────────────╯
跑步有什么问题?
我无法将其代理传递给 nginx 的原因是没有文档根目录。如果我在root 指令的开头使用#,我在nginx 中使用此错误:
2021/10/14 12:02:29 [error] 25#25: *180 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: my_ip, server: app.site.com, request: "GET / HTTP/1.1", host: "app.site.com"
2021/10/14 12:02:29 [error] 25#25: *181 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: my_ip, server: app.site.com, request: "GET /favicon.ico HTTP/1.1", host: "app.site.com", referrer: "https://app.
更新 1
正如kissu所说,这是nuxt.config.js中的相关块:
target: 'server',
server: {
port: 3000, // default: 3000
host: '0.0.0.0', // default: localhost
},
【问题讨论】:
-
这里发生了很多事情。让我们稍微缩小一下这个问题。
nuxt.config.js中的target是什么? -
那么,当您
yarn build时会发生什么?你没有在项目的根目录看到dist目录吗? -
.nuxt仅用于开发目的。对于生产,你需要有一个dist,否则它根本不会被优化。 -
哦不,我失败了。
.nuxt也用于server,而dist用于static。这将使用 Nuxt3 进行标准化。同时,如果您yarn build,它将在.nuxt中创建它,如下所示:nuxtjs.org/docs/get-started/commands#server-deployment -
那么,你的问题解决了吗?!
标签: node.js docker nginx nuxt.js