Nextjs nginx docker配置

qzlgjiam  于 2023-11-17  发布在  Nginx
关注(0)|答案(1)|浏览(202)

我正在尝试在docker中使用nginx容器化下一个js应用程序。我有用于nextjs应用程序的Dockerfile:

  1. FROM node:18-alpine as builder
  2. WORKDIR /my-space
  3. COPY package.json package-lock.json ./
  4. RUN npm ci
  5. COPY . .
  6. RUN npm run build
  7. FROM node:18-alpine as runner
  8. WORKDIR /my-space
  9. COPY --from=builder /my-space/package.json .
  10. COPY --from=builder /my-space/package-lock.json .
  11. COPY --from=builder /my-space/next.config.js ./
  12. COPY --from=builder /my-space/public ./public
  13. COPY --from=builder /my-space/.next/standalone ./
  14. COPY --from=builder /my-space/.next/static ./.next/static
  15. EXPOSE 3000
  16. ENTRYPOINT ["npm", "start"]

字符串
我有一个nginx的Dockerfile:

  1. # Base on offical NGINX Alpine image
  2. FROM nginx:stable
  3. # Remove any existing config files
  4. RUN rm /etc/nginx/conf.d/*
  5. # Copy config files
  6. # *.conf files in conf.d/ dir get included in main config
  7. COPY ./default.conf /etc/nginx/conf.d/
  8. # Expose the listening port
  9. EXPOSE 80
  10. # Launch NGINX
  11. CMD [ "nginx", "-g", "daemon off;" ]


我有这个nginx default.conf配置:

  1. proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=STATIC:10m inactive=7d use_temp_path=off;
  2. upstream nextjs_upstream {
  3. server frontend:3000;
  4. }
  5. server {
  6. listen 80 default_server;
  7. server_name _;
  8. server_tokens off;
  9. gzip on;
  10. gzip_proxied any;
  11. gzip_comp_level 4;
  12. gzip_types text/css application/javascript image/svg+xml;
  13. proxy_http_version 1.1;
  14. proxy_set_header Upgrade $http_upgrade;
  15. proxy_set_header Connection 'upgrade';
  16. proxy_set_header Host $host;
  17. proxy_cache_bypass $http_upgrade;
  18. location / {
  19. proxy_pass http://nextjs_upstream;
  20. }
  21. location /_next/static {
  22. proxy_cache STATIC;
  23. proxy_pass http://nextjs_upstream;
  24. }
  25. location /static {
  26. proxy_cache STATIC;
  27. proxy_ignore_headers Cache-Control;
  28. proxy_cache_valid 60m;
  29. proxy_pass http://nextjs_upstream;
  30. }
  31. location /_next {
  32. proxy_pass http://nextjs_upstream;
  33. }
  34. location /_next/public {
  35. proxy_cache STATIC;
  36. proxy_pass http://nextjs_upstream;
  37. }
  38. location /_next/standalone {
  39. proxy_cache STATIC;
  40. proxy_pass http://nextjs_upstream;
  41. }
  42. location /public {
  43. proxy_cache STATIC;
  44. proxy_ignore_headers Cache-Control;
  45. proxy_cache_valid 60m;
  46. proxy_pass http://nextjs_upstream;
  47. }
  48. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
  49. add_header Cache-Control "public, max-age=31536000, immutable";
  50. }
  51. location /api/ {
  52. proxy_pass https://mydev.online/api;
  53. proxy_set_header Host $host;
  54. proxy_set_header X-Real-IP $remote_addr;
  55. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  56. proxy_set_header X-Forwarded-Proto $scheme;
  57. }
  58. }


我用所需的标签构建了nextjs app和nginx,然后运行docker compose:

  1. version: '3.9'
  2. services:
  3. frontend:
  4. image: myfront
  5. container_name: frontend
  6. ports:
  7. - 3000:3000
  8. nginx:
  9. image: nginx-custom
  10. ports:
  11. - 80:80


但问题是它不起作用。在next.config.json中,我有:

  1. /** @type {import('next').NextConfig} */
  2. const nextConfig = {
  3. experimental: {
  4. outputStandalone: true,
  5. },


但我也尝试了没有它的Dockerfile:

  1. # Build Stage
  2. FROM node:16-alpine AS BUILD_IMAGE
  3. WORKDIR /app
  4. COPY package*.json ./
  5. RUN npm ci
  6. COPY . .
  7. RUN npm run build
  8. # Production Stage
  9. FROM node:16-alpine AS PRODUCTION_STAGE
  10. WORKDIR /app
  11. COPY --from=BUILD_IMAGE /app/package*.json ./
  12. COPY --from=BUILD_IMAGE /app/.next ./.next
  13. COPY --from=BUILD_IMAGE /app/public ./public
  14. COPY --from=BUILD_IMAGE /app/node_modules ./node_modules
  15. ENV NODE_ENV=production
  16. EXPOSE 3000
  17. CMD ["npm", "start"]


但是这两个选项都不起作用。我可以在3000端口上打开网站,但不能在80端口上打开nginx,因为当试图访问/_next上的路径时,它会出现很多错误并返回空白页面,相同的路径似乎在3000端口上工作。这里可能有什么问题?
附注:我尝试了这个dockerfile,它也不工作(以及它的一些变体),因为它在构建图像时会出错,因为一些模块丢失,或者类似这样的东西(有很多错误):

  1. # Base on offical Node.js Alpine image
  2. FROM node:alpine
  3. # Set working directory
  4. WORKDIR /usr/app
  5. # Copy package.json and package-lock.json before other files
  6. # Utilise Docker cache to save re-installing dependencies if unchanged
  7. COPY ./package*.json ./
  8. # Install dependencies
  9. RUN npm install --production
  10. # Copy all files
  11. COPY ./ ./
  12. # Build app
  13. RUN npm run build
  14. # Expose the listening port
  15. EXPOSE 3000
  16. # Run container as non-root (unprivileged) user
  17. # The node user is provided in the Node.js Alpine base image
  18. USER node
  19. # Run npm start script when container starts
  20. CMD [ "npm", "start" ]


UPD:我还尝试将docker compose文件中的network_mode: host设置为nginx和frontend容器,并将default.conf中的localhost:3000设置为重定向到,但这也没有帮助。

jucafojl

jucafojl1#

我猜容器之间的网络连接不起作用。可能需要在Docker compose中添加链接和主机

  1. version: '3.9'
  2. services:
  3. frontend:
  4. image: myfront
  5. container_name: frontend
  6. ports:
  7. - "127.0.0.1:3000:3000"
  8. nginx:
  9. image: nginx-custom
  10. ports:
  11. - 80:80
  12. links:
  13. - frontend

字符串

相关问题