云服务安装docker+nginx,并将配置挂载到宿主机中

一键自动安装
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
 
启动docker
sudo systemctl start docker
开机自启
sudo systemctl enable docker
搜索nginx
docker search nginx
安装nginx(最新)
docker pull nginx:latest
查看docker 镜像
docker images
运行docker
 
参数说明:
  • --name nginx-test:容器名称。
  • -p 8080:80: 端口进行映射,将本地 8080 端口映射到容器内部的 80 端口。
  • -d nginx: 设置容器在在后台一直运行。
docker run --name nginx-test -p 8080:80 -d nginx
查看容器状态
docker stats docker ps
进入docker 镜像
docker exec -it 镜像ID /bin/bash
生成宿主文件对应文件夹  -p :递归创建目录
mkdir -p /data/nginx/{conf,conf.d,html,logs}
创建对应文件
 /data/nginx/conf/nginx.conf
点击查看代码
user  nginx;
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;
}
/data/nginx/conf.d/default.conf
点击查看代码
server {  
    listen       80;  
    server_name  localhost;  
  
    #charset koi8-r;  
    #access_log  /var/log/nginx/log/host.access.log  main;  
  
    location / {  
        #root   /data/nginx/html;  
        root   /usr/share/nginx/html;  
        index  index.html index.htm;  
        #autoindex  on;  
        #try_files $uri /index/index/page.html;  
        #try_files $uri /index/map/page.html;  
    }  
  
    #error_page  404              /404.html;  
  
    # redirect server error pages to the static page /50x.html  
    #  
    error_page   500 502 503 504  /50x.html;  
    location = /50x.html {  
        root   /usr/share/nginx/html;  
    }  
  
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
    #  
    #location ~ \.php$ {  
    #    proxy_pass   http://127.0.0.1;  
    #}  
  
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
    #  
    #location ~ \.php$ {  
    #    root           html;  
    #    fastcgi_pass   127.0.0.1:9000;  
    #    fastcgi_index  index.php;  
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
    #    include        fastcgi_params;  
    #}  
  
    # deny access to .htaccess files, if Apache's document root  
    # concurs with nginx's one  
    #  
    #location ~ /\.ht {  
    #    deny  all;  
    #}  
}
/data/nginx/html/
将打包的前端包解压到这边
也可以复制文件
docker cp image_id:usr/share/nginx/conf.d/default.conf /data/nginx/conf.d/default.conf
...
将配置挂载到宿主机子中
docker run --name nginx_jd_proxy -d -p 8081:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/log:/var/log/nginx -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf.d:/etc/nginx/conf.d nginx
查看是否启动成功
docker ps
查看日志
docker logs image_id
停止并删除
docker stop image_id dockre rm image_id
页面访问即可
xx.xx.xx.xx:8081/index.html // 注意云服务器的安全组开放
 
如果前后端不在一个服务器,前端代码打包路径应为localhost,然后nginx代理到对应后端域名做双重代理
 

posted @ 2021-11-25 11:12  攻玉  阅读(320)  评论(0)    收藏  举报