使用Nginx和Docker部署Next.ts项目
背景
-
前段时间在字节的课程上接触了Next.js框架,觉得很好用,另一方面也想锻炼一下TypeScript能力,为暑期在字节实习做准备
我称之为 Next.ts -
一直想做一个属于我的网站,这些天受到一些东西的熏染,决定开动了
Getting started
首先初始化我们的项目,需要加入ts支持
create-next-app [name] --example with-typescript
然后部署到我的服务器上
但是这还没完,这个项目跑在3000端口,而我们平常访问的网站都是访问默认端口80,于是我想到了使用 Nginx反向代理-大概的原理就是Nginx监听80端口客户端发来的请求,然后作为代理发送给3000端口
Nginx配置
本部分参考【文章】
具体的做法就是使用Docker构建专属于本项目的配置文件 default.conf
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=STATIC:10m inactive=7d use_temp_path=off;
upstream nextjs_upstream {
server nextjs:3000;
}
server {
listen 80 default_server;
server_name _;
server_tokens off;
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;
location / {
proxy_pass http://nextjs_upstream;
}
}
Dockerfile如下(其实后来想想,只有一台服务器,而且只运行一个服务,实在没有必要在Docker里面跑nginx...)
# Base on offical NGINX Alpine image
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/
# Expose the listening port
EXPOSE 80
# Launch NGINX
CMD [ "nginx", "-g", "daemon off;" ]
Next Docker 配置
FROM node:14
# Set working directory
WORKDIR /usr/app
# Copy package.json and yarn.lock before other files
COPY ./package.json ./
COPY ./yarn.lock ./
# Install dependencies
RUN yarn
# Copy all files
COPY ./ ./
# Build app
RUN yarn build
# Expose the listening port
EXPOSE 3000
# Run container as non-root (unprivileged) user
# The node user is provided in the Node.js Alpine base image
USER node
# Run npm start script when container starts
CMD [ "yarn", "start" ]
Deploy
# cd work_dir
# build
docker build -t [next-image] .
docker build -t [nginx-image] ./nginx/
docker network create [network]
docker run -d --network [network] --name [next-name] [next-image]
docker run -d --network [network] --link [next-name]:nextjs -p 80:80 --name [nginx-name] [nginx-image]
升级到Https
下载SSL证书并部署,参考文章
在这个过程中遇到了一些问题,即报错 wrong version number
以为是http的版本,把1.1升级到2,问题还是没有解决
最后发现是由于前端项目使用的http协议,需要把 proxy_pass改成http协议的😓
更新证书
更新完证书后需要重载一下新的证书文件,使用如下指令即可
nginx -s reload