nginx在docker上安装配置
参考:
- docker nginx : [https://hub.docker.com//nginx](https://hub.docker.com//nginx)
- Docker nginx 反向代理设置:https://gythialy.github.io/Docker-nginx-reverse-proxy/
- Virtual Hosts on nginx:https://gist.github.com/soheilhy/8b94347ff8336d971ad0
- Nginx: How do I forward a http request to another port?:https://serverfault.com/questions/536576/nginx-how-do-i-forward-a-http-request-to-another-port
- How to redirect address.com/foo/bar to address.com:port/bar with nginx?:https://stackoverflow.com/questions/4449788/how-to-redirect-address-com-foo-bar-to-address-comport-bar-with-nginx
- Forwarding port 80 to 8080 using NGINX:https://stackoverflow.com/questions/24861311/forwarding-port-80-to-8080-using-nginx
- Nginx配置upstream实现负载均衡:https://www.cnblogs.com/wzjhoutai/p/6932007.html
- Nginx 负载均衡演示之 upstream 参数 & location 参数:https://blog.csdn.net/caijunsen/article/details/83002219
- nginx - location配置详解:https://juejin.im/entry/5b10a08ae51d4506ca62b5ec
- nginx rewrite的作用:https://zhuanlan.zhihu.com/p/36148885
- Nginx Full Example Configuration:https://www.nginx.com/resources/wiki/start/topics/examples/full/
- Nginx之proxy_redirect详解:https://blog.csdn.net/u010391029/article/details/50395680
- Nginx反向代理中使用proxy_redirect重定向url:https://cloud.tencent.com/developer/article/1027558
- 使用 Certbot 为网站设置永久免费的 HTTPS 证书:https://jimmysong.io/posts/free-certificates-with-certbot/
首先确保 my-nginx-network 网络提前创建好
docker network create -d bridge mynginx_network
准备nginx docker项目
创建项目并拷贝文件
mkdir mynginx
cd mynginx
docker run --rm -d --name temp-nginx -p 80:80 nginx
docker container ls --all
# 拷贝文件
docker cp f7968bc4ff4f:/usr/share/nginx ./www
docker cp f7968bc4ff4f:/etc/nginx/conf.d ./conf/conf.d
docker cp f7968bc4ff4f:/etc/nginx/nginx.conf ./conf/nginx.conf
docker cp f7968bc4ff4f:/var/log/nginx ./log
创建README.md,添加.dockerignore文件
*.dockerapp
*.tar.gz
_build/
bin
README.md
运行
docker run --rm -d -p 80:80 \
--name mynginx \
--network mynginx_network \
-v $PWD/www:/usr/share/nginx \
-v $PWD/conf/conf.d:/etc/nginx/conf.d \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
nginx
使用Dockerfile
创建Dockerfile
FROM nginx:latest
# RUN echo '<h1>Hello, This is Mynginx in Docker!</h1>' > /usr/share/nginx/html/index.html
COPY ./conf/nginx.conf /etc/nginx/nginx.conf
COPY ./conf/conf.d /etc/nginx/conf.d
COPY ./log /var/log/nginx
# COPY ./nginx /usr/share/nginx
# WORKDIR /root
EXPOSE 80
构建镜像
docker build -t mynginx:v1.0 .
运行
docker run --rm -d -p 80:80 \
--name mynginx \
--network mynginx_network \
-v $PWD/www:/usr/share/nginx \
-v $PWD/conf/conf.d:/etc/nginx/conf.d \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/log:/var/log/nginx \
mynginx:v1.0
使用docker-compose
创建docker-compose.yml
version: '3.5'
services:
nginx:
build: .
# command: nginx -g 'daemon off;'
# image: nginx:lastest
image: mynginx:v1.0 # 指定(生成的)镜像名
container_name: mynginx # 指定(生成的)容器名
volumes:
- ./log:/var/log/nginx
- ./conf.d:/etc/nginx/conf.d
- ./nginx.conf:/etc/nginx/nginx.conf
- ./www:/usr/share/nginx
ports:
- 80:80
networks:
- nginx_network
# 引用已存在的网络
networks:
nginx_network:
external:
name: mynginx_network
# mynginx_network 在命令行里提前建好
# docker network create -d bridge mynginx_network
# # 创建新的网络
# networks:
# nginx_network:
# name: mynginx_network
# driver: bridge
docker-compose service的启动、运行及删除
docker-compose up -d //创建并后台启动
docker-compose stop //停止
docker-compose start //启动
docker-compose ps //查看
docker-compose down --rmi all --remove-orphans //删除服务、并删除生成的镜像和容器
应用配置
ghost
docker run --rm --name my-ghost \
--network mynginx_network \
-e url=http://localhost \
--expose 2368 \
-p 3002:2368 ghost
jekyll
export JEKYLL_VERSION=3.8
docker run --rm --name my-jekyll \
--network mynginx_network \
--volume="$PWD:/srv/jekyll" \
--volume="$PWD/vendor/bundle:/usr/local/bundle" \
-p 4000:4000 \
--expose 4000 \
-it jekyll/jekyll:$JEKYLL_VERSION \
jekyll serve --watch --drafts --host my-jekyll
docker run --rm --name my-jekyll \
--network mynginx_network \
--volume="$PWD:/srv/jekyll" \
--volume="$PWD/vendor/bundle:/usr/local/bundle" \
-p 4000:4000 \
-it jekyll/jekyll:$JEKYLL_VERSION \
jekyll serve --watch --drafts
nginx
docker run -p 80:80 \
--name mynginx \
--restart=always \
--network mynginx_network \
-v $PWD/conf/conf.d:/etc/nginx/conf.d \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/log:/var/log/nginx \
-v $PWD/www:/usr/share/nginx \
-v $PWD/../myjekyll/_site:/usr/share/nginx/blog \
nginx
布署nginx
docker run --rm -p 80:80 \
--name mynginx \
--network mynginx_network \
--hostname=wodedata.com \
--add-host=app.wodedata.com:127.0.0.1 \
-v $PWD/conf/conf.d:/etc/nginx/conf.d \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/log:/var/log/nginx \
-v $PWD/www:/usr/share/nginx \
-v $PWD/../jekyll/_site:/usr/share/nginx/jekyll \
nginx
注:--hostname=HOSTNAME 设定容器的主机名,它会被写到容器内的 /etc/hostname 和 /etc/hosts