nginx负载均衡--nginx容器部署

第一步:我们需要下载nginx的镜像:

由于docker上边会提供三种nginx镜像:
 

Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版

Stable version:最新稳定版,生产环境上建议使用的版本

Legacy versions:遗留的老版本的稳定版

所以前往docker官网查看提供的最新的稳定版:nginx镜像 如图

nginx负载均衡--nginx容器部署_第1张图片

我这里下载的是stable-perl版本,根据自己需要下载就行了.每个版本后边都要拉取的命令

第二步:创建镜像

      1.创建一各配置都是默认的nginx 容器:

         

docker run --name nginx -d -p 8090:80 nginx:stable-perl

         命令解释:

          docker run --name 容器名称 -d -p 宿主机端口:容器端口 镜像名称:版本号

          -d 是开启守护进程,这样你的容器才能在后台运行, -p 后边是端口映射

 

    2.如果这么简单多没意思是吧,这么做创建容器简单了但是我们后续使用nginx部署项目的时候就会非常麻烦,需要将项目文件copy到容器,已经nginx的配置修改也很麻烦.我们用nginx是为了使用方便,所以接下来我们部署一个配置文件在宿主机就能修改的方法.

     a.首先nginx有四个目录(conf,conf.d,html,logs),如果有同学在服务器上部署过nginx 的知道这四个目录都在/data下.网上也有许多将宿主机的挂载目录放在这的.但是我使用的mac在系统更新到10.15之后就会出现问题,/data目录没了,虽然网上有许多可以解决的方法.但是我任务没必要,因为目录挂载对于宿主机来说没有那么苛刻.

     于是我随便选了个目录创建了这四个目录(但是自己一定得记着,不然以后需要修改找不到文件岂不是尴尬)

我放在/Users/yang/Develop/dockerFiles/nginx/下了  你们自己随意

sudo mkdir -p {conf,conf.d,html,logs}

 

一次性在nginx创建conf,conf.d,html,logs 四个文件夹,sudo也可以不要

 

    b.现在文件目录有了接着将nginx 对应的配置文件创建出来

      然后在conf下创建nginx.conf文件

sudo vim 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;
}

b.在conf.d/下创建default.conf

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

 c.在html/下创建index.html(用作默认的页面)

sudo vim index.html

文件内容(这个html页面是借用一个老哥的,他博客链接找不到了,如果有幸被老哥看到,联系我把你链接补上):



    
    系统时间


 

配置文件都创建好了,万事俱备只欠东风.

第三步:执行命令创建容器

docker run --name nginx81 -d -p 80:80 -v /Users/yang/Develop/dockerFiles/nginx/html:/usr/share/nginx/html -v /Users/yang/Develop/dockerFiles/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /Users/yang/Develop/dockerFiles/nginx/logs:/var/log/nginx -v /Users/yang/Develop/dockerFiles/nginx/conf.d:/etc/nginx/conf.d -d nginx:stable-perl

其中大量的-v后边是两个路径,前边的就是我们在宿主机创建的路径后边是nginx 的配置路径,前边的根据自己的情况,后边是固定的

注意:

最好映射端口保持一致,不然nginx的跳转端口问题,不改的话问题就是:

访问宿主机对应端口正常但是一旦发生跳转就会请求nginx对应的端口,

因为nginx跳转的端口和宿主机端口不一致就会造成跳转后宿主机根本就不能接到请求.

此时不出意外就已经成功了.

到目前为止第一种和第二种没什么区别,而且还多了目录的挂载.接下来就是正正体现价值的地方.

这时候我启动了两个项目,分别监听8001和8002端口,如果是第一种修改配置文件就得进入容器才行.

而现在呢?

修改宿主机挂载目录conf中的nginx.conf文件:

添加

 upstream upstream_name{
        server 192.168.202.60:8001;
        server 192.168.202.60:8002;
    }

    server {
        listen       8081;
        server_name  localhost;

        location / {
            proxy_pass http://upstream_name;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

最终效果:

 

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;

upstream upstream_name{

server 192.168.202.60:8001;

server 192.168.202.60:8002;

}

 

server {

listen 8081;

server_name localhost;

 

location / {

proxy_pass http://upstream_name;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

#gzip on;

 

include /etc/nginx/conf.d/*.conf;

}

红色部分就是添加的部分和代码框一样的,只是都贴出来明显.

upstream upstream_name的内容就是分发的服务地址,至于权重就是在server ip:端口 后加 weight=n n是正整数,默认是1 。 权重和n成正比

server是nginx 的服务配置,包括监听的端口和一些转发策略,这些网上都可以查到,我学艺不精就不耽误大家时间了

 

注意

和上次的注意呼应的:

配置nginx监听的端口,如果这里和容器的端口不一致将导致宿主机和容器端口是通的但是容器内部的nginx监听没起作用一样失败

然后重启,至于权重就是在server ip:端口 后加 weight=n n是正整数,默认是1 。 权重和n成正比

全部完成 重启容器 就ok了

 

 

 

 

 

 

 

你可能感兴趣的:(docker,nginx)