Docker 容器配置 Nginx 负载均衡示例

# Docker 容器配置 Nginx 负载均衡示例:

# 创建 3 个Nginx容器服务:容器命名分别为:

容器命名 ip分配 端口 目录映射 配置文件 角色
hengda-nginx 172.10.0.2 80 ~/nginx/conf.d:/etc/nginx/conf.d nginx-default.conf 入口
hengda-nginx2 172.10.0.3 80 ~/nginx/conf2.d:/etc/nginx/conf.d nginx-default.conf 负载1
hengda-nginx3 172.10.0.4 80 ~/nginx/conf2.d:/etc/nginx/conf.d nginx-default.conf 负载2

# 配置Nginx负载均衡入口服务:~/nginx/conf.d/nginx-default.conf

upstream upstream_name{
    server 172.10.0.3:80 weight=10;
    #server 172.17.0.4:80 weight=20;
}

server {
    listen       80;
    #server_name  localhost;
    server_name  upstream_name;

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

# 配置Nginx负载均衡服务 负载1:~/nginx/conf2.d/nginx-default.conf

# 站点1

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html/laravel/public;
        index index.php index.html index.htm server.php;


    #开启后忽略入口文件
    if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php$1  last;
        break;
    }

扫描二维码关注公众号,回复: 10748928 查看本文章

    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html/laravel/public;
    }

    location ~ \.php(.*)$ {

# 这里使用 我的php服务容器1
    fastcgi_pass    172.17.1:9001;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/laravel/public/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

#站点2

server {
    listen       80;
    server_name  deepin-blog.com www.deepin-blog.com;

    location / {
        root   /usr/share/nginx/html/www_test/blog/public;
        index index.php index.html index.htm server.php;
    
        #开启后忽略入口文件
    if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php$1  last;
        break;
    }
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html/www_test/blog/public;
    }

    location ~ \.php(.*)$ {

    # 这里使用 我的php服务容器1
    fastcgi_pass    172.17.1:9001;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/www_test/blog/public/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

 

# 配置Nginx负载均衡服务 负载2:~/nginx/conf3.d/nginx-default.conf

# 站点1

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html/laravel/public;
        index index.php index.html index.htm server.php;


    #开启后忽略入口文件
    if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php$1  last;
        break;
    }

    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html/laravel/public;
    }

    location ~ \.php(.*)$ {

# 这里使用我的php容器2作为php解释器
    fastcgi_pass    172.17.1:9002;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/laravel/public/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

#站点2

server {
    listen       80;

# 这里deepin-blog.com是我本地站点绑定的域名, 我已在localhost里做过解析记录到127.0.0.1了
    server_name  deepin-blog.com www.deepin-blog.com;

    location / {
        root   /usr/share/nginx/html/www_test/blog/public;
        index index.php index.html index.htm server.php;
    
        #开启后忽略入口文件
    if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php$1  last;
        break;
    }
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html/www_test/blog/public;
    }

    location ~ \.php(.*)$ {
   # 这里使用我的php容器2作为php解释器
    fastcgi_pass    172.17.1:9002;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/www_test/blog/public/$fastcgi_script_name;
        include        fastcgi_params;
    }
}
 

 

# 启动 这些 nginx 和 php 服务器后,即可访问测试:

1.浏览器访问测试访问站点1:http://localhost

2.浏览器访问测试范文站点2:http://deepin-blog.com

# 其他可参考地址:

1. nginx负载均衡:https : // www . cnblogs . com /lave/p/10477375.html

2. Nginx完成 weight负载均衡:https : // blog . csdn . net /qq_43154385/article/details/102722322

3.Docker Nginx 负载均衡配置:https : // blog . csdn . net /jc0803kevin/article/details/88847159

4.docker network基础:https : // www . cnblogs . com /jsonhc/p/7823286.html

5.在docker中部署nginx集群:https : // blog . csdn . net /qq_42346414/article/details/89386952

发布了118 篇原创文章 · 获赞 17 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/one312/article/details/105062350