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

搭建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; # 这里采用upstream 的配置轮训达到负载效果
        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

下面为nginx服务提供ssl 支持

首先需要准备key和 crt
我这里linux上生成一个测试的server.crt和key


image.png

然后修改配置文件 ssl版本
vim /home/java/management/conf/default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    rewrite ^(.*)$ https://$host:444 permanent; # 重要 80 重定性到443

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #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;
    #}
}

# 定义集群 负载均衡
# 通过配置多个server,weight是权重,权重越大,被访问的记录越大
# fail_timeout  与max_fails结合使用
# max_fails 设置在fail_timeout参数设置的时间内最大失败次数,如果在这个时间内,所有针对该服务器的请求都失败了,那么认为该服务器会被认为是停机了
# fail_time 服务器会被认为停机的时间长度,默认为10s。
# down  标记服务器永久停机了。
# backup    标记该服务器为备用服务器。当主服务器停止时,请求会被发送到它这里。
# 默认轮寻 ,    
#ip_hash #就近原则
#least_conn 最小连接
upstream prox.manage{
    server 192.168.0.103:9527 weight=2;
    #server localhost:9527;
    #server localhost:8003 backup;
    #server localhost:8004 max_fails=3 fail_timeout=20s;
}


# HTTPS server
#
server {
    listen       444 ssl ; # ssl 协议
    server_name  localhost;

    root /usr/share/nginx/html; # 网站源文件地址
    index zc-management-app/index.html zc-management-app/index.htm; # 首页

    ssl_certificate      /etc/nginx/conf.d/server.crt; # crt文件
    ssl_certificate_key  /etc/nginx/conf.d/server_nopass.key;  # key 文件

    #ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; # 固定不要变动
    ssl_prefer_server_ciphers on;
    
    location /security/img/ {
        proxy_pass http://prox.manage/security/img/;
        #proxy_set_header Host $host:8001;
        proxy_set_header Referer $http_referer;
    }

    location ~ ^/(security|data|manage|order|payment|zensyo/write|zensyo|taskcenter|api)/ {
        proxy_pass http://prox.manage;
        #proxy_set_header Host $host:8001;
        proxy_set_header Referer $http_referer;
    }
    

}




重新运行docker

docker rm -f nginx-manager

docker run --name nginx-manager -p 444:444 -p 8081:80   \
-v /home/java/management/html:/usr/share/nginx/html \
-v /home/java/management/nginx.conf:/etc/nginx/nginx.conf \
-v /home/java/management/conf/default.conf:/etc/nginx/conf.d/default.conf \
-v /home/java/management/conf/server.crt:/etc/nginx/conf.d/server.crt \
-v /home/java/management/conf/server_nopass.key:/etc/nginx/conf.d/server_nopass.key \
-v /home/java/management/logs:/var/log/nginx  nginx

当你打开浏览器 输入 ip:8081 他会自动跳转到 https://ip:444

image.png

点波关注 系统搭建(docker)

你可能感兴趣的:(【4.5】服务器安装 Docker中安装与配置 Nginx)