1:拉取nginx镜像
docker pull nginx
2:启动nginx
docker run --name nginx -p 80:80 -d nginx (前面80端口主机映射到后面容器的80端口)
3:在本地找一个文件夹创建3个文件(conf、html、logs)我的在E:\nginx下创建
4: 将容器中的配置复制到创建的文件夹中
docker cp nginx:/etc/nginx/nginx.conf E:\nginx\conf\nginx.conf
docker cp nginx:/etc/nginx/conf.d E:\nginx\conf\conf.d
5:停止并移除容器
docker stop nginx
docker rm nginx
6:启动nginx并挂载文件
docker run -d --name nginx -p 8000:80 -v
E:\nginx\conf\nginx.conf:/etc/nginx/nginx.conf -v
E:\nginx/logs:/var/log/nginx -v
E:\nginx/html:/usr/share/nginx/html -v
E:\nginx/conf:/etc/nginx/conf.d --privileged=true nginx
6:将打包好的dist文件放在上面自己创建的html文件夹,不要dist外层,复制里面的所有文件放在html(也可以带上dist但是nginx配置路径等会要多加一层dist)
7:修改配置自己创建的conf文件下的两个default.conf,nginx.conf文件如下(修改地方标红了)
第一个标红:nginx负载均衡调用的ip端口,可以为两个,比如可以
192.168.1.22:8080
192.168.1.22:8088
代表负载均衡,可以加权重,一般是一个
第二个标红:
listen:是监听的端口,第6步8000:80就是监听的8000端口主机端口
root是容器中html文件的位置,如果没删除dist那么就在第6步挂载html的一行代码,改为E:\nginx\html\dist:/usr/share/nginx/html -v
index 是打包好的里面的index页面,也可以自己建一个html后缀的文件测试,不放dist包
user root;
worker_processes 1;error_log /var/log/nginx/error.log warn;
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 gateway {
server 192.168.1.55:80;
}
server {
listen 8000;
server_name web;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}location ^~/api {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://gateway;
}
}
}