没有不值得去解决的问题,也没有不值得去学习的技术!

Deploy Two Domains in One Git Repository and One Container With Rancher

In Rancher port mapping, public host port 8661 is mapped to private container port 80, and a new public host port 8662 is mapped to private container port 81.

1. The channel publishing API now includes the following applications: QQ Account, WeChat Official Account, Weibo, Console Command, Cross-Channel, and Authorization. Among them, QQ Account, WeChat Official Account, Weibo, and Cross-Channel need to be deployed under Domain A, with inbound traffic restricted to the intranet only. Authorization needs to be deployed under Domain B, with inbound traffic supporting external networks, as shown in Figure 1.

The channel publishing API now includes the following applications: QQ Account, WeChat Official Account, Weibo, Console Command, Cross-Channel, and Authorization. Among them, QQ Account, WeChat Official Account, Weibo, and Cross-Channel need to be deployed under Domain A, with inbound traffic restricted to the intranet only. Authorization needs to be deployed under Domain B, with inbound traffic supporting external networks.
Figure 1

2. \build\c_files\etc\nginx\conf.d\channel-pub-api.conf already supports deployment under Domain A, with inbound traffic restricted to the intranet only.

Nginx

server {
    listen 80; ## listen for ipv4
    server_name CHANNEL_PUB_API_CFG_NGINX_SERVER_NAME;
    charset utf-8;

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

    location / {
        root /sobey/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 /qq {
        alias /sobey/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 /weibo {
            alias /sobey/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 /sobey/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|qq|weibo|wx)/web|qq|weibo|wx))^ /api/web$uri break;
        rewrite (?!^/qq/web)^/qq(/.+)$ /qq/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;
    }
}

3. In Rancher port mapping, public host port 8661 is mapped to private container port 80, as shown in Figure 2.

In Rancher port mapping, public host port 8661 is mapped to private container port 80.
Figure 2

4. The access details for Domain A are as follows:

Plaintext
QQ Account: https://wjdev2.chinamcloud.com:8661/v1/qq/
WeChat Official Account: https://wjdev2.chinamcloud.com:8661/v1/wx/
Weibo: https://wjdev2.chinamcloud.com:8661/v1/weibo/
Cross-Channel: https://wjdev2.chinamcloud.com:8661/v1/

5. Edit \build\c_files\etc\nginx\conf.d\channel-pub-api.conf to support deploying Authorization under Domain B, with inbound traffic supporting external networks.

Nginx

server {
    listen CHANNEL_PUB_API_CFG_NGINX_AUTH_LISTEN; ## listen for ipv4
    server_name CHANNEL_PUB_API_CFG_NGINX_AUTH_SERVER_NAME;
    charset utf-8;

    root /sobey/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 CHANNEL_PUB_API_CFG_NGINX_API_LISTEN; ## listen for ipv4
    server_name CHANNEL_PUB_API_CFG_NGINX_API_SERVER_NAME;
    charset utf-8;

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

    location / {
        root /sobey/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 /qq {
        alias /sobey/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 /weibo {
            alias /sobey/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 /sobey/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|qq|weibo|wx)/web|qq|weibo|wx))^ /api/web$uri break;
		rewrite (?!^/qq/web)^/qq(/.+)$ /qq/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;
    }
}

6. In Rancher port mapping, public host port 8661 is mapped to private container port 80, and a new public host port 8662 is mapped to private container port 81, as shown in Figure 3.

In Rancher port mapping, public host port 8661 is mapped to private container port 80, and a new public host port 8662 is mapped to private container port 81.
Figure 3

Add environment variables:

Plaintext
	CHANNEL_PUB_API_CFG_NGINX_API_SERVER_NAME=wjdev2.chinamcloud.com # Nginx server name (API domain; inbound traffic recommended to be restricted to intranet only)
	CHANNEL_PUB_API_CFG_NGINX_API_LISTEN=80 # Nginx server listening port (API domain; inbound traffic recommended to be restricted to intranet only)
	CHANNEL_PUB_API_CFG_NGINX_AUTH_SERVER_NAME=wjdev2.chinamcloud.com # Nginx server name (Authorization domain; inbound traffic recommended to support external networks)
	CHANNEL_PUB_API_CFG_NGINX_AUTH_LISTEN=81 # Nginx server listening port (Authorization domain; inbound traffic recommended to support external networks)

7. Check /etc/nginx/conf.d/channel-pub-api.conf; the environment variables have been replaced.

Nginx

server {
    listen 81; ## listen for ipv4
    server_name wjdev2.chinamcloud.com;
    charset utf-8;

    root /sobey/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 wjdev2.chinamcloud.com;
    charset utf-8;

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

    location / {
        root /sobey/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 /qq {
        alias /sobey/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 /weibo {
            alias /sobey/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 /sobey/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|qq|weibo|wx)/web|qq|weibo|wx))^ /api/web$uri break;
                rewrite (?!^/qq/web)^/qq(/.+)$ /qq/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. The access details for Domains A and B are as follows, and access is working normally, as shown in Figure 4.

The access details for Domains A and B are as follows, and access is working normally.
Figure 4
Plaintext
QQ Account: https://wjdev2.chinamcloud.com:8661/v1/qq/
WeChat Official Account: https://wjdev2.chinamcloud.com:8661/v1/wx/
Weibo: https://wjdev2.chinamcloud.com:8661/v1/weibo/
Cross-Channel: https://wjdev2.chinamcloud.com:8661/v1/
Authorization: https://wjdev2.chinamcloud.com:8662/

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系