Google Compute Engine上的Docker映像无法在端口80上运行

时间:2019-04-29 04:02:32

标签: docker nginx google-cloud-platform google-compute-engine next.js

我正在将Next.js / Nginx Docker映像从Container Registry部署到Compute Engine

部署后,该应用程序将按预期运行,但它将在端口3000而不是80上运行-即我想在<ip_address>上访问它,但我只能在<ip_address>:3000上访问它。我已经在Nginx中设置了反向代理,以将端口3000转发到80,但是它似乎不起作用。

当我运行docker-compose up时,可以在localhost上访问该应用程序(而不是localhost:3000

Dockerfile

FROM node:alpine as react-build

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install
RUN npm install --global pm2

COPY . /usr/src/app
RUN npm run build
# EXPOSE 3000
EXPOSE 80

CMD [ "pm2-runtime", "start", "npm", "--", "start" ]

docker-compose.yml

version: '3'
services:
  nextjs: 
    build: ./
  nginx: 
    build: ./nginx
    ports:
      - 80:80

./ nginx / Dockerfile

FROM nginx:alpine

# Remove any existing config files
RUN rm /etc/nginx/conf.d/*

# Copy config files
# *.conf files in "conf.d/" dir get included in main config
COPY ./default.conf /etc/nginx/conf.d/

./ nginx / default.conf

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=STATIC:10m inactive=7d use_temp_path=off;

server {
  listen 80;

  gzip on;
  gzip_proxied any;
  gzip_comp_level 4;
  gzip_types text/css application/javascript image/svg+xml;

  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;

  # BUILT ASSETS (E.G. JS BUNDLES)
  # Browser cache - max cache headers from Next.js as build id in url
  # Server cache - valid forever (cleared after cache "inactive" period)
  location /_next/static {
    #proxy_cache STATIC;
    proxy_pass http://localhost:3000;
  }

  # STATIC ASSETS (E.G. IMAGES)
  # Browser cache - "no-cache" headers from Next.js as no build id in url
  # Server cache - refresh regularly in case of changes
  location /static {
    #proxy_cache STATIC;
    proxy_ignore_headers Cache-Control;
    proxy_cache_valid 60m;
    proxy_pass http://locahost:3000;
  }

  # DYNAMIC ASSETS - NO CACHE
  location / {
    proxy_pass http://locahost:3000;
  }
}

0 个答案:

没有答案