虚拟机容器部署---nginx
虚拟机容器部署------nginx
环境 ubuntu 20.0 nginx1.21.5
1.拉取nginx镜像
docker pull nginx:latest
2.创建nginx配置文件
# 此处目录为虚拟机内目录,以实际部署的为准
# 1. 创建挂载目录
mkdir -p /home/webServer/nginx/conf
mkdir -p /home/webServer/nginx/log
mkdir -p /home/webServer/nginx/html
# 2. 生成容器
docker run --name nginx -p 8888:80 -d nginx:latest
# 3.将容器nginx.conf文件复制到宿主机
docker cp nginx:/etc/nginx/nginx.conf /home/webServer/nginx/conf/nginx.conf
# 4.将容器conf.d文件夹下内容复制到宿主机
docker cp nginx:/etc/nginx/conf.d /home/webServer/nginx/conf/conf.d
# 5.将容器中的html文件夹复制到宿主机
docker cp nginx:/usr/share/nginx/html /home/webServer/nginx/html
3.创建容器并运行
docker stop nginx
docker rm nginx
docker-compose内容新增nginx部分
nginx:
image: nginx:latest
restart: always
container_name: nginx
ports:
- 8888:80
volumes:
- "/home/webServer/nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
- "/home/webServer/nginx/conf/conf.d:/etc/nginx/conf.d"
- "/home/webServer/nginx/log:/var/log/nginx"
- "/home/webServer/nginx/html:/usr/share/nginx/html"
networks:
- tinyscan
# 执行命令,生成nginx容器
docker-compose up -d
4.修改配置文件
# user nginx;
user root;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
# include /etc/nginx/conf.d/*.conf;
upstream web_api {
#server 172.19.0.4:8000;
# 此处为容器IP:uwsgi端口 或 容器名称:uwsgi端口!!!!
server web_api:8000;
}
server {
listen 80;
server_name localhost;
charset UTF-8;
access_log /var/log/nginx/ty_api_access.log;
error_log /var/log/nginx/ty_api_error.log;
client_max_body_size 75M;
# 根目录直接访问主页
location / {
root /usr/share/nginx/html/;
index index.php index.html index.htm;
}
#---------------以下为我项目中所配置的location与部署无关------------------------------
# 静态文件访问地址
location /static/ {
autoindex on;
alias /usr/share/nginx/html/;
}
# 我的django项目中所有路由只有v2开头和验证码captcha开头两种情况
location ~ ^(/v2/|/captcha/) {
uwsgi_pass web_api;
include /etc/nginx/uwsgi_params;
uwsgi_read_timeout 300;
}
}
}
5.重启容器使配置文件生效
docker restart nginx
即可访问成功