linux学习Dockerphp

使用docker安装Nginx实现负载均衡

2019-01-03  本文已影响148人  iXiAo9

事先准备

两台apache镜像
一台nginx镜像

先运行web服务器

image.png
docker run --name web1 -d -p 81:80 httpd
将web1的80端口映射为实体机的81端口
docker run --name web2 -d -p 82:80 httpd
将web2的80端口映射为实体机的82端口
docker run --name nginx-proxy -d -p 80:80 nginx
image.png

我们需要修改nginx的配置文件,但是首先找到配置文件地址
我们进入docker

image.png
我喜欢用CONTAINER ID
docker exec -it 508bd07cc627 /bin/bash
注意空格和命令的写法
image.png
敲完命令后环境变量会改变为bash
很多linux的命令无法使用,如ifcnofig vim

nginx 配置文件

nginx的配置文件一般存放在/usr/local/nginx/conf
或者
/etc/nginx


nginx 配置文件

配置文件名为nginx.conf

更改配置文件

未改之前

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   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   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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

更改之后

    #gzip  on;
    #更改这里################
    upstream  webs {
    server 172.18.27.141:81;
    server 172.18.27.141:82;
}   

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            #更改这里################
        proxy_pass   http://webs;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #

更改配置文件后重启nginx镜像
docker restart 508bd07cc627
如果启动失败或启动无反应,则说明你的配置文件更改错误。

上一篇下一篇

猜你喜欢

热点阅读