访问索引页面或/ pages / _id页面我没有错误。但是,当我尝试打开/其他页面时,它会崩溃。
重新制定:
/
/pages/_id
_id
/pages/xxx
xxx
方式 asyncData 在nuxt.js中工作如下:
http://yourserver/pages/123
asyncData
http://api:1337/games/123
现在发生了一些不同的事情。
http://yourserver/
http://api:1337/games
这是nuxt.js为您带来的功能,可加快页面内容加载时间。来自 文件 ,重要的信息是:
asyncData 每次加载页面组件之前调用。它将被称为服务器端(在对Nuxt应用程序的第一个请求时)和客户端在导航到其他路由时。
server-side
client-side
现在有趣的部分:
1337
http://api:1337/
http://api:1337
net::ERR_NAME_NOT_RESOLVED
api
http://yourserver/api/
http://yourserver/api
既然你正在使用 nuxt.js Axios模块 为了让你打电话到api容器,你就在那里。
Axios模块有一个 代理 可以设置为的选项 true 在nuxtjs.config.js中
true
Bellow是使用的为您的项目设置反向代理的示例 Traefik ,但文档声明代理与使用不兼容 基本URL 选项。该 字首 必须使用选项。
你的nuxt.config.js应如下所示:
axios: { prefix: '/api', proxy: true }, proxy: { '/api/': { target: 'http://localhost:1337', pathRewrite: { '^/api/': '' } } },
这可以在您的开发计算机上正常工作,如果strapi正在运行并响应 http://localhost:1337 。但这不适用于容器,因为我们需要更换 http://localhost:1337 同 http://api:1337 。 为此,我们可以引入一个环境变量( STRAPI_URL ):
http://localhost:1337
STRAPI_URL
axios: { prefix: '/api', proxy: true }, proxy: { '/api/': { target: process.env.STRAPI_URL || 'http://localhost:1337', pathRewrite: { '^/api/': '' } } },
我们稍后会设定 STRAPI_URL 在docker-compose.yml文件中。
由于我在使用docker时放弃了使用nginx实现反向代理,这里有一个例子 Traefik :
泊坞窗,compose.yml:
version: '3' services: reverseproxy: # see https://docs.traefik.io/#the-traefik-quickstart-using-docker image: traefik command: --docker ports: - "80:80" volumes: - /var/run/docker.sock:/var/run/docker.sock api: image: strapi/strapi environment: - ... expose: - 1337 labels: traefik.frontend.rule: PathPrefixStrip:/api traefik.port: 1337 nuxt: image: ... expose: - 3000 command: "npm run start" labels: traefik.frontend.rule: PathPrefixStrip:/ traefik.port: 3000
现在用户浏览器发出的所有HTTP请求 http://yourserver 将由Traefik反向代理处理。
http://yourserver
Traefik将通过查看以标签开头的标签来配置转发规则 traefik. 在...上 nuxt 和 api 容器。
traefik.
nuxt
您现在有2个反向代理:
我们现在需要指导 nuxt.js代理模块 它必须转发请求 http://api:1337/ 。我们打算用 STRAPI_URL 环境变量。
我们需要指导 nuxt Axios模块 用户浏览器必须调用api http://yourserver/api 。这是通过 API_URL_BROWSER 环境变量。
version: '3' services: reverseproxy: # see https://docs.traefik.io/#the-traefik-quickstart-using-docker image: traefik command: --docker ports: - "80:80" volumes: - /var/run/docker.sock:/var/run/docker.sock api: image: strapi/strapi environment: - ... expose: - 1337 labels: traefik.frontend.rule: PathPrefixStrip:/api traefik.port: 1337 nuxt: image: ... expose: - 3000 command: "npm run start" environment: NUXT_HOST: 0.0.0.0 STRAPI_URL: http://api:1337/ API_URL_BROWSER: /api labels: traefik.frontend.rule: PathPrefixStrip:/ traefik.port: 3000