在 Rancher 中通过 http://服务代号.命名空间:端口 内网调用各个容器的实现

1、新的部署规划,期望服务端的容器之间相互调用不再基于域名,而是基于服务名。如图1

图1

2、现有的规划。一个服务的容器中包含 3 个应用,其端口皆配置为 80 ,基于域名区分。调用:https://ccpapi.dev.chinamcloud.cn 。如图2

图2

https://ccpauth.dev.chinamcloud.cn
https://ccpapi.dev.chinamcloud.cn
https://ccprpc.dev.chinamcloud.cn

3、现有的规划。一个服务的容器中包含 3 个应用,其端口皆配置为 80 ,基于域名区分。调用:https://ccpauth.dev.chinamcloud.cn 。如图3

图3

4、现有的规划。容器中的 Nginx 配置文件内容如下

 
server {
    listen 80; ## listen for ipv4
    server_name ccpauth.dev.chinamcloud.cn;
    charset utf-8;

    root /mcloud/www/channel-pub-api/frontend/web;
    index index.php;

    location / {
        # 如果找不到真实存在的文件,把请求分发至 index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # uncomment to avoid processing of calls to non-existing static files by Yii
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    #    try_files $uri =404;
    #}
    #error_page 404 /404.html;

    # deny accessing php files for the /assets directory
    location ~ ^/assets/.*\.php$ {
        deny all;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~* /\. {
        deny all;
    }
}

server {
    listen 80; ## listen for ipv4
    server_name ccprpc.dev.chinamcloud.cn;
    charset utf-8;

    root /mcloud/www/channel-pub-api/rpc/web;
    index index.php;

    location / {
        # 如果找不到真实存在的文件,把请求分发至 index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # uncomment to avoid processing of calls to non-existing static files by Yii
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    #    try_files $uri =404;
    #}
    #error_page 404 /404.html;

    # deny accessing php files for the /assets directory
    location ~ ^/assets/.*\.php$ {
        deny all;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~* /\. {
        deny all;
    }
}

server {
    listen 80; ## listen for ipv4
    server_name ccpapi.dev.chinamcloud.cn localhost;
    charset utf-8;

    root /mcloud/www/channel-pub-api;
    index index.php;

    location / {
        root /mcloud/www/channel-pub-api/api/web;
        try_files $uri $uri/ /api/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /customize {
        alias /mcloud/www/channel-pub-api/customize/web/;
        location = /customize {
            try_files $uri /customize/customize/web/index.php$is_args$args;
        }
        try_files $uri $uri/ /customize/customize/web/index.php$is_args$args;
        location ~ ^/customize/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /baijia {
        alias /mcloud/www/channel-pub-api/baijia/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /baijia/ {
        #    return 301 /baijia;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /baijia {
            # if your location is "/baijia", try use "/baijia/baijia/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /baijia/baijia/web/index.php$is_args$args;
        }

        # if your location is "/baijia", try use "/baijia/baijia/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /baijia/baijia/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/baijia/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/baijia/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /douyin {
            alias /mcloud/www/channel-pub-api/douyin/web/;

            # redirect to the URL without a trailing slash (uncomment if necessary)
            #location = /douyin/ {
            #    return 301 /douyin;
            #}

            # prevent the directory redirect to the URL with a trailing slash
            location = /douyin {
                # if your location is "/douyin", try use "/douyin/douyin/web/index.php$is_args$args"
                # bug ticket: https://trac.nginx.org/nginx/ticket/97
                try_files $uri /douyin/douyin/web/index.php$is_args$args;
            }

            # if your location is "/douyin", try use "/douyin/douyin/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri $uri/ /douyin/douyin/web/index.php$is_args$args;

            # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
            #location ~ ^/douyin/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
            #    log_not_found off;
            #    access_log off;
            #    try_files $uri =404;
            #}

            location ~ ^/douyin/assets/.+\.php(/|$) {
                deny all;
            }
        }

    location /netease {
        alias /mcloud/www/channel-pub-api/netease/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /netease/ {
        #    return 301 /netease;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /netease {
            # if your location is "/netease", try use "/netease/netease/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /netease/netease/web/index.php$is_args$args;
        }

        # if your location is "/netease", try use "/netease/netease/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /netease/netease/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/netease/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/netease/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /qq {
        alias /mcloud/www/channel-pub-api/qq/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /qq/ {
        #    return 301 /qq;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /qq {
            # if your location is "/qq", try use "/qq/qq/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /qq/qq/web/index.php$is_args$args;
        }

        # if your location is "/qq", try use "/qq/qq/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /qq/qq/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/qq/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/qq/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /vtt {
        alias /mcloud/www/channel-pub-api/vtt/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /vtt/ {
        #    return 301 /vtt;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /vtt {
            # if your location is "/vtt", try use "/vtt/vtt/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /vtt/vtt/web/index.php$is_args$args;
        }

        # if your location is "/vtt", try use "/vtt/vtt/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /vtt/vtt/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/vtt/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/vtt/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /weibo {
        alias /mcloud/www/channel-pub-api/weibo/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /weibo/ {
        #    return 301 /weibo;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /weibo {
            # if your location is "/weibo", try use "/weibo/weibo/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /weibo/weibo/web/index.php$is_args$args;
        }

        # if your location is "/weibo", try use "/weibo/weibo/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /weibo/weibo/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/weibo/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/weibo/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /wx {
        alias /mcloud/www/channel-pub-api/wx/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /wx/ {
        #    return 301 /wx;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /wx {
            # if your location is "/wx", try use "/wx/wx/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /wx/wx/web/index.php$is_args$args;
        }

        # if your location is "/wx", try use "/wx/wx/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /wx/wx/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/wx/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/wx/assets/.+\.php(/|$) {
            deny all;
        }
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
        location ~ ^/.+\.php(/|$) {
                rewrite (?!^/((api|customize|baijia|customize|douyin|netease|qq|vtt|weibo|wx)/web|baijia|customize|douyin|netease|qq|vtt|weibo|wx))^ /api/web$uri break;
                rewrite (?!^/baijia/web)^/baijia(/.+)$ /baijia/web$1 break;
                rewrite (?!^/customize/web)^/customize(/.+)$ /customize/web$1 break;
                rewrite (?!^/douyin/web)^/douyin(/.+)$ /douyin/web$1 break;
                rewrite (?!^/netease/web)^/netease(/.+)$ /netease/web$1 break;
                rewrite (?!^/qq/web)^/qq(/.+)$ /qq/web$1 break;
                rewrite (?!^/vtt/web)^/vtt(/.+)$ /vtt/web$1 break;
                rewrite (?!^/weibo/web)^/weibo(/.+)$ /weibo/web$1 break;
                rewrite (?!^/wx/web)^/wx(/.+)$ /wx/web$1 break;

                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_pass 127.0.0.1:9000;
                #fastcgi_pass unix:/var/run/php5-fpm.sock;
                try_files $fastcgi_script_name =404;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\. {
        deny all;
    }
}

5、现有的规划。查看负载均衡的规则,皆是自定义域名。如图4

图4

6、新的规划。一个服务的容器中包含 3 个应用,ccpauth 需要支持外网域名,无需要支持内网调用。ccpapi 与 ccprpc 需要支持内网服务名调用,无需要支持外网调用。其端口分别配置为 80、82、83 ,基于端口区分。

https://ccpauth.dev.chinamcloud.cn
http://ccpapi.ccp:82
http://ccpapi.ccp:83

7、新的规划。容器中的 Nginx 配置文件内容如下

server {
    listen 80; ## listen for ipv4
    server_name ccpauth.dev.chinamcloud.cn;
    charset utf-8;

    root /mcloud/www/channel-pub-api/frontend/web;
    index index.php;

    location / {
        # 如果找不到真实存在的文件,把请求分发至 index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # uncomment to avoid processing of calls to non-existing static files by Yii
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    #    try_files $uri =404;
    #}
    #error_page 404 /404.html;

    # deny accessing php files for the /assets directory
    location ~ ^/assets/.*\.php$ {
        deny all;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~* /\. {
        deny all;
    }
}

server {
    listen 83; ## listen for ipv4
    server_name ccpapi.ccp;
    charset utf-8;

    root /mcloud/www/channel-pub-api/rpc/web;
    index index.php;

    location / {
        # 如果找不到真实存在的文件,把请求分发至 index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # uncomment to avoid processing of calls to non-existing static files by Yii
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    #    try_files $uri =404;
    #}
    #error_page 404 /404.html;

    # deny accessing php files for the /assets directory
    location ~ ^/assets/.*\.php$ {
        deny all;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~* /\. {
        deny all;
    }
}

server {
    listen 82; ## listen for ipv4
    server_name ccpapi.ccp localhost;
    charset utf-8;

    root /mcloud/www/channel-pub-api;
    index index.php;

    location / {
        root /mcloud/www/channel-pub-api/api/web;
        try_files $uri $uri/ /api/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /customize {
        alias /mcloud/www/channel-pub-api/customize/web/;
        location = /customize {
            try_files $uri /customize/customize/web/index.php$is_args$args;
        }
        try_files $uri $uri/ /customize/customize/web/index.php$is_args$args;
        location ~ ^/customize/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /baijia {
        alias /mcloud/www/channel-pub-api/baijia/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /baijia/ {
        #    return 301 /baijia;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /baijia {
            # if your location is "/baijia", try use "/baijia/baijia/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /baijia/baijia/web/index.php$is_args$args;
        }

        # if your location is "/baijia", try use "/baijia/baijia/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /baijia/baijia/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/baijia/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/baijia/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /douyin {
            alias /mcloud/www/channel-pub-api/douyin/web/;

            # redirect to the URL without a trailing slash (uncomment if necessary)
            #location = /douyin/ {
            #    return 301 /douyin;
            #}

            # prevent the directory redirect to the URL with a trailing slash
            location = /douyin {
                # if your location is "/douyin", try use "/douyin/douyin/web/index.php$is_args$args"
                # bug ticket: https://trac.nginx.org/nginx/ticket/97
                try_files $uri /douyin/douyin/web/index.php$is_args$args;
            }

            # if your location is "/douyin", try use "/douyin/douyin/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri $uri/ /douyin/douyin/web/index.php$is_args$args;

            # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
            #location ~ ^/douyin/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
            #    log_not_found off;
            #    access_log off;
            #    try_files $uri =404;
            #}

            location ~ ^/douyin/assets/.+\.php(/|$) {
                deny all;
            }
        }

    location /netease {
        alias /mcloud/www/channel-pub-api/netease/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /netease/ {
        #    return 301 /netease;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /netease {
            # if your location is "/netease", try use "/netease/netease/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /netease/netease/web/index.php$is_args$args;
        }

        # if your location is "/netease", try use "/netease/netease/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /netease/netease/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/netease/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/netease/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /qq {
        alias /mcloud/www/channel-pub-api/qq/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /qq/ {
        #    return 301 /qq;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /qq {
            # if your location is "/qq", try use "/qq/qq/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /qq/qq/web/index.php$is_args$args;
        }

        # if your location is "/qq", try use "/qq/qq/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /qq/qq/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/qq/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/qq/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /vtt {
        alias /mcloud/www/channel-pub-api/vtt/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /vtt/ {
        #    return 301 /vtt;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /vtt {
            # if your location is "/vtt", try use "/vtt/vtt/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /vtt/vtt/web/index.php$is_args$args;
        }

        # if your location is "/vtt", try use "/vtt/vtt/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /vtt/vtt/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/vtt/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/vtt/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /weibo {
        alias /mcloud/www/channel-pub-api/weibo/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /weibo/ {
        #    return 301 /weibo;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /weibo {
            # if your location is "/weibo", try use "/weibo/weibo/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /weibo/weibo/web/index.php$is_args$args;
        }

        # if your location is "/weibo", try use "/weibo/weibo/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /weibo/weibo/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/weibo/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/weibo/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /wx {
        alias /mcloud/www/channel-pub-api/wx/web/;

        # redirect to the URL without a trailing slash (uncomment if necessary)
        #location = /wx/ {
        #    return 301 /wx;
        #}

        # prevent the directory redirect to the URL with a trailing slash
        location = /wx {
            # if your location is "/wx", try use "/wx/wx/web/index.php$is_args$args"
            # bug ticket: https://trac.nginx.org/nginx/ticket/97
            try_files $uri /wx/wx/web/index.php$is_args$args;
        }

        # if your location is "/wx", try use "/wx/wx/web/index.php$is_args$args"
        # bug ticket: https://trac.nginx.org/nginx/ticket/97
        try_files $uri $uri/ /wx/wx/web/index.php$is_args$args;

        # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
        #location ~ ^/wx/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ {
        #    log_not_found off;
        #    access_log off;
        #    try_files $uri =404;
        #}

        location ~ ^/wx/assets/.+\.php(/|$) {
            deny all;
        }
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
        location ~ ^/.+\.php(/|$) {
                rewrite (?!^/((api|customize|baijia|customize|douyin|netease|qq|vtt|weibo|wx)/web|baijia|customize|douyin|netease|qq|vtt|weibo|wx))^ /api/web$uri break;
                rewrite (?!^/baijia/web)^/baijia(/.+)$ /baijia/web$1 break;
                rewrite (?!^/customize/web)^/customize(/.+)$ /customize/web$1 break;
                rewrite (?!^/douyin/web)^/douyin(/.+)$ /douyin/web$1 break;
                rewrite (?!^/netease/web)^/netease(/.+)$ /netease/web$1 break;
                rewrite (?!^/qq/web)^/qq(/.+)$ /qq/web$1 break;
                rewrite (?!^/vtt/web)^/vtt(/.+)$ /vtt/web$1 break;
                rewrite (?!^/weibo/web)^/weibo(/.+)$ /weibo/web$1 break;
                rewrite (?!^/wx/web)^/wx(/.+)$ /wx/web$1 break;

                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_pass 127.0.0.1:9000;
                #fastcgi_pass unix:/var/run/php5-fpm.sock;
                try_files $fastcgi_script_name =404;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\. {
        deny all;
    }
}

8、新的规划。查看负载均衡的规则,仅保留 ccpauth.dev.chinamcloud.cn。如图5

图5

9、新的规划。在自身容器内部调用 3 个应用的地址,调用成功。

[root@ccpapi-78d6f74688-rfjhf /]# cd /mcloud/www/channel-pub-api/
[root@ccpapi-78d6f74688-rfjhf channel-pub-api]# cd api/web/
[root@ccpapi-78d6f74688-rfjhf web]# vi api.txt
[root@ccpapi-78d6f74688-rfjhf web]# cd ..
[root@ccpapi-78d6f74688-rfjhf api]# cd ..
[root@ccpapi-78d6f74688-rfjhf channel-pub-api]# cd qq/web/
[root@ccpapi-78d6f74688-rfjhf web]# vi qq.txt
[root@ccpapi-78d6f74688-rfjhf web]# cd rpc/web
bash: cd: rpc/web: No such file or directory
[root@ccpapi-78d6f74688-rfjhf web]# cd ..
[root@ccpapi-78d6f74688-rfjhf qq]# cd ..
[root@ccpapi-78d6f74688-rfjhf channel-pub-api]# cd rpc/web/
[root@ccpapi-78d6f74688-rfjhf web]# vi rpc.txt
[root@ccpapi-78d6f74688-rfjhf web]# cd ..
[root@ccpapi-78d6f74688-rfjhf rpc]# cd ..
[root@ccpapi-78d6f74688-rfjhf channel-pub-api]# cd frontend/web/
[root@ccpapi-78d6f74688-rfjhf web]# vi auth.txt
[root@ccpapi-78d6f74688-rfjhf web]# curl http://ccpapi.ccp:82/api.txt
api
[root@ccpapi-78d6f74688-rfjhf web]# curl http://ccpapi.ccp:82/qq/qq.txt
qq
[root@ccpapi-78d6f74688-rfjhf web]# curl http://ccpapi.ccp:83/rpc.txt
rpc
[root@ccpapi-78d6f74688-rfjhf web]# curl https://ccpauth.dev.chinamcloud.cn/auth.txt
auth
[root@ccpapi-78d6f74688-rfjhf web]# 

10、新的规划。在其他容器内部调用 3 个应用的地址,调用成功。如图6

图6

[root@ccpvtt-54dc6fb788-t9ldd /]# curl http://ccpapi.ccp:82/api.txt
api
[root@ccpvtt-54dc6fb788-t9ldd /]# curl http://ccpapi.ccp:82/qq/qq.txt
qq
[root@ccpvtt-54dc6fb788-t9ldd /]# curl http://ccpapi.ccp:83/rpc.txt
rpc
[root@ccpvtt-54dc6fb788-t9ldd /]# curl https://ccpauth.dev.chinamcloud.cn/auth.txt
auth
[root@ccpvtt-54dc6fb788-t9ldd /]# 

11、新的规划。在外网中依次调用。查看结果。符合预期。

https://ccpauth.dev.chinamcloud.cn/auth.txt 响应成功,auth
https://ccpapi.dev.chinamcloud.cn/api.txt 响应 404,404 Not Found
https://ccprpc.dev.chinamcloud.cn/rpc.txt 响应 404,404 Not Found
http://ccpapi.ccp:82/api.txt 无响应,Failed to load response data
http://ccpapi.ccp:83/rpc.txt 无响应,Failed to load response data

12、由于在开发环境中,可能其他服务的本地环境需要调用 ccpapi 与 ccprpc。因此,需要 ccpapi 与 ccprpc 支持外网调用。编辑负载均衡的规则,ccpapi.dev.chinamcloud.cn:82、ccprpc.dev.chinamcloud.cn:83。如图7

图7

13、新的规划。在外网中依次调用。查看结果。符合预期。

https://ccpauth.dev.chinamcloud.cn/auth.txt 响应成功,auth
https://ccpapi.dev.chinamcloud.cn/api.txt 响应成功,api
https://ccprpc.dev.chinamcloud.cn/rpc.txt 响应成功,rpc
http://ccpapi.ccp:82/api.txt 无响应,Failed to load response data
http://ccpapi.ccp:83/rpc.txt 无响应,Failed to load response data
永夜