Use docker containers to deploy front-end projects on nginx and solve cross-domain problems
Ready to work
1. The front-end project is packaged, decompressed in / home and stored in / home / dist
2. The back-end project runs
Host operation
The new data volume is named nginx
docker volume create nginx
docker volume ls
docker volume inspect nginx
Initialize a container called nginx, -d runs in the background, -p port mapping, port 8089 of the docker host maps to port 80 of the container
-v parameter maps the static files under dist to the html folder of nginx, and the configuration file maps to The corresponding default configuration file maps the host folder (/ var / lib / docker / volumes / nginx / _data) bound to the data volume nginx
to the / etc / nginx folder of the nginx container
docker run --name nginx -d -p 8089:80 -v /home/dist:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v nginx:/etc/nginx nginx
docker ps view running containers
Go to the folder where the host is mounted
cd /var/lib/docker/volumes/nginx
cd _date /
Copy the static folder to the mounted folder
cp / home / dist.
Go to the folder where the configuration file is stored and edit the configuration file
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;
server{
listen 80;
server_name ip;
location / {
proxy_pass http://ip:8081/api/; #ip为你的后端ip地址
}
location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff|html)$ {
root /etc/nginx/dist;
expires 30d;
}
}
}
Enter the folder of the nginx container
/data/nginx/conf
1, docker exec -it nginx (container name) / bin / bash
2、cd /etc/nginx
3. ls can view the static files in the host folder just copied
Restart nginx container to update configuration
docker ps
docker restart XXX