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

Deploying CakePHP 2.x: How to Adjust the Base URL from / to /creditshopback

No 404 error exists in the response. https://console.xxx.cn/creditshopback/app/webroot/css/common.css . It responds with 200.

1. The current BASE URL is /, https://creditshopback.xxx.cn/ . It responds with 200. See Figure 1.

The current BASE URL is /, https://creditshopback.xxx.cn/ . It responds with 200.
Figure 1

2. Edit the program file to output the current version number: 2.6.4. See Figure 2.

Edit the program file to output the current version number: 2.6.4.
Figure 2
PHP

echo Configure::version();
exit;

3. Now the expected URL is adjusted to: https://console.xxx.cn/creditshopback . Refer to the development installation guide: https://book.cakephp.org/2/zh/installation.html . See Figure 3.

Now the expected URL is adjusted to: https://console.xxx.cn/creditshopback . Refer to the development installation guide: https://book.cakephp.org/2/zh/installation.html .
Figure 3

4. View the current Nginx configuration.

Nginx

server {
    listen 82;
    charset utf-8;
    
    root /mcloud/creditshop_back/app/webroot;
    index index.php;

    location / {
        # modsecurity on;
        # modsecurity_rules_file /etc/nginx/modsec_includes_lite.conf;
        if (!-e $request_filename) {
            rewrite ^/(.+)$ /index.php?url=$1 last;
            break;
        }
    }

    location /creditf {
        # modsecurity on;
        # modsecurity_rules_file /etc/nginx/modsec_includes_lite.conf;
        alias /webtv/wangjie/creditshop_back/h5jifen/files;
        index index.html;
    }

    location /upload {
        alias /webtv/wangjie/creditshop_back/upload;
        index index.html;
    }

    # 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  /mcloud/creditshop_back/app/webroot$fastcgi_script_name;
        include        fastcgi_params;
    }
    #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;
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

5. Move the directory: /mcloud/creditshop_back/* to /mcloud/creditshop_back/creditshopback/*. See Figure 4.

Move the directory: /mcloud/creditshop_back/* to /mcloud/creditshop_back/creditshopback/*.
Figure 4

6. Edit the Nginx configuration.

Nginx

server {
    listen 82;
    charset utf-8;

    root /mcloud/creditshop_back/;
    index index.php;

    location / {
        # modsecurity on;
        # modsecurity_rules_file /etc/nginx/modsec_includes_lite.conf;
        if (!-e $request_filename) {
            rewrite ^/(.+)$ /index.php?url=$1 last;
            break;
        }
    }

    location /creditshopback/creditf {
        # modsecurity on;
        # modsecurity_rules_file /etc/nginx/modsec_includes_lite.conf;
        alias /webtv/wangjie/creditshop_back/h5jifen/files;
        index index.html;
    }

    location /creditshopback/upload {
        alias /webtv/wangjie/creditshop_back/upload;
        index index.html;
    }

    # 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  /mcloud/creditshop_back/$fastcgi_script_name;
        include        fastcgi_params;
    }
    #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;
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

7. A 404 error exists in the response. The URL is: https://console.xxx.cn/creditshopback/css/common.css . It needs to be adjusted to: https://console.xxx.cn/creditshopback/app/webroot/css/common.css . See Figure 5.

A 404 error exists in the response. The URL is: https://console.xxx.cn/creditshopback/css/common.css . It needs to be adjusted to: https://console.xxx.cn/creditshopback/app/webroot/css/common.css .
Figure 5

8. In the directory: /app/View, bulk replace echo $this->Html->css(‘ with echo $this->Html->css(‘/app/webroot/css/. Strings like echo $this->Html->css(‘cake.generic’); are not replaced. See Figure 6.

In the directory: /app/View, bulk replace echo $this->Html->css(' with echo $this->Html->css('/app/webroot/css/. Strings like echo $this->Html->css('cake.generic'); are not replaced.
Figure 6

9. In the directory: /app/View, bulk replace echo $this->Html->script(‘ with echo $this->Html->script(‘/app/webroot/js/. Strings like echo $this->Html->css(‘cake.generic’); are not replaced.

10. No 404 error exists in the response. https://console.xxx.cn/creditshopback/app/webroot/css/common.css . It responds with 200. See Figure 7.

No 404 error exists in the response. https://console.xxx.cn/creditshopback/app/webroot/css/common.css . It responds with 200.
Figure 7

11. Open the URL: https://console.xxx.cn/creditshopback/products/AddNewProduct?tenantid=2a20ea857030b75d3efa343cd625ee03 . It responds with 404.

12. Reference URL: https://www.shuijingwanwq.com/2018/08/16/2836/ . Edit the Nginx configuration.

Nginx

server {
    listen 82;
    charset utf-8;

    root /mcloud/creditshop_back;
    index index.php;

    location /creditshopback {
        alias /mcloud/creditshop_back/creditshopback/app/webroot/;
        location = /creditshopback {
            try_files $uri /creditshopback/creditshopback/app/webroot/index.php$is_args$args;
        }
        try_files $uri $uri/ /creditshopback/creditshopback/app/webroot/index.php$is_args$args;
    }

    location /creditshopback/creditf {
        # modsecurity on;
        # modsecurity_rules_file /etc/nginx/modsec_includes_lite.conf;
        alias /webtv/wangjie/creditshop_back/h5jifen/files;
        index index.html;
    }

    location /creditshopback/upload {
        alias /webtv/wangjie/creditshop_back/upload;
        index index.html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ ^/.+\.php(/|$) {
        rewrite (?!^/creditshopback/app/webroot)^/creditshopback(/.+)$ /creditshopback/app/webroot$1 break;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        try_files      $fastcgi_script_name =404;
    }
    #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;
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

13. Open the URL: https://console.xxx.cn/creditshopback/products/AddNewProduct?tenantid=2a20ea857030b75d3efa343cd625ee03 . It responds with 200.

14. Revert steps 8 and 9. The final result is that all responses return 200, as expected.

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

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

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

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

关于我  ·  GitHub  ·  邮件联系