美文网首页DockerIT技术篇
【3.5】服务器安装 Docker中安装与配置 Nginx

【3.5】服务器安装 Docker中安装与配置 Nginx

作者: 王滕辉 | 来源:发表于2021-08-19 08:47 被阅读0次

    搭建nginx

    采用docker 快速搭建方式

    docker run --name nginx1 -d nginx
    mkdir /home/java/nginx1/conf
    docker cp nginx1:"/etc/nginx/conf.d/default.conf" "/home/java/nginx1/conf/default.conf"
    docker cp nginx1:/etc/nginx/nginx.conf /home/java/nginx1/nginx.conf
    docker cp nginx1:/var/log/nginx /home/java/nginx1/
    mv /home/java/nginx1/nginx /home/java/nginx1/logs 
    docker cp nginx1:/usr/share/nginx/html /home/java/nginx1/
    docker rm -f nginx1
    chmod 777 -Rf /home/java/nginx1
    
    ## nginx
    docker run --name nginx \
    -p 9090:9090 -p 8848:8848  -p 9002:9002 \
    --restart=always --network my-net \
    -v /home/java/nginx1/html:/usr/share/nginx/html \
    -v /home/java/nginx1/nginx.conf:/etc/nginx/nginx.conf \
    -v /home/java/nginx1/conf/default.conf:/etc/nginx/conf.d/default.conf \
    -v /home/java/nginx1/logs:/var/log/nginx  nginx
    
    ## mysql
    docker run -p 3306:3306 --name mysql \
    -v /home/mysql/conf:/etc/mysql \
    -v /home/mysql/logs:/var/log/mysql \
    -v /home/mysql/data:/var/lib/mysql \
    -v /home/mysql/mysql-files:/var/lib/mysql-files \
    -e MYSQL_ROOT_PASSWORD=root -d mysql
    
    ## nacos
    docker run -d -p 8848:8848 --name nacos  \
    --env MODE=standalone \
    --env SPRING_DATASOURCE_PLATFORM=mysql \
    --env MYSQL_SERVICE_HOST=192.168.137.100 \
    --env MYSQL_SERVICE_PORT=3306 \
    --env MYSQL_SERVICE_DB_NAME=nacos \
    --env MYSQL_SERVICE_USER=root \
    --env MYSQL_SERVICE_PASSWORD=root \
    nacos/nacos-server:latest
    
    

    其中 端口 9090 是 echarts-phantomjs 反向代理用的端口
    8848 为nacos集群环境的端口 9002就是 yum 局域网环境准备的端口

    vi /home/java/nginx1/conf/default.conf

    # -----------------------------------------------------------
    #  echarts - phantomjs
    # -----------------------------------------------------------
    upstream prox.phantomjs{
        server 192.168.0.103:9091 weight=2;
        server 192.168.0.103:9092 weight=2;
        server 192.168.0.103:9093 weight=2;
        server 192.168.0.103:9094 weight=2;
        server 192.168.0.103:9095 weight=2;
        #server localhost:9527;
        #server localhost:8003 backup;
        #server localhost:8004 max_fails=3 fail_timeout=20s;
    }
    
    server {
        listen       9090;
        listen  [::]:9090;
        server_name  localhost;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    
        location /phantomjs {
            proxy_pass http://prox.phantomjs;
            proxy_set_header Referer $http_referer;
        }
        #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;
        #}
    }
    
    # -----------------------------------------------------------
    #  nacos clus cluster
    # -----------------------------------------------------------
    upstream prox.nacos{
        server 192.168.0.105:8948 weight=2;
        server 192.168.0.103:8949 weight=2;
        server 192.168.0.103:8950 weight=2;
        
    }
    
    server {
        listen       8848;
        server_name  localhost;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
    
        location / {
            proxy_pass http://prox.nacos;
            proxy_set_header Referer $http_referer;
        }
    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
    }
    
    # -----------------------------------------------------------
    #  yum 
    # -----------------------------------------------------------
    
    server {
        listen       9002;
        server_name  localhost;
    
        root         /usr/share/nginx/html/repl/;
        autoindex on;# 显示目录
        autoindex_exact_size on;# 显示文件大小
        autoindex_localtime on;# 显示文件时间
    
    }
    
    
    

    重启容器

    docker restart nginx1
    

    打开浏览器 http://192.168.0.103:9002/


    image.png

    点波关注 系统搭建(docker)

    相关文章

      网友评论

        本文标题:【3.5】服务器安装 Docker中安装与配置 Nginx

        本文链接:https://www.haomeiwen.com/subject/ywgqbltx.html