M
M
Mark2018-04-04 09:26:00
Nginx
Mark, 2018-04-04 09:26:00

How to configure a redirect to another part of an advanced Yii2 application in nginx?

Hello. There is an Advanced Yii2 application that has: backend, frontend, api.
Under the backend, frontend, it was possible to create redirects, but something doesn’t work under the API.
The API follows the link: site.zone/api/web, I would like to achieve a link like site.zone/api
Working configurations for frontend/backend:

location / {
            root $base_root/frontend/web;
            try_files $uri $uri/ /frontend/web/index.php$is_args$args;
 
            location ~ ^/assets/.+\.php(/|$) {
                deny all;
            }
        }
 
    location /admin {
        alias $base_root/backend/web;
        try_files  $uri /backend/web/index.php?$args;
 
        location = /admin/ {
           return  301 /admin;
        }
 
        location ~* ^/admin/(.+\.php)$ {
            try_files  $uri /backend/web/$1?$args;
        }
 
    }
 
        location ~ [^/]\.php(/|$) {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            if (!-f $document_root$fastcgi_script_name) {
                return  404;
            }
        rewrite (?!^/((frontend|backend)/web|admin))^ /frontend/web$uri break;
        rewrite (?!^/backend/web)^/admin(/.+)$ /backend/web$1 break;
 
            fastcgi_pass    127.0.0.1:9001;
            fastcgi_index   index.php;
            include         /etc/nginx/fastcgi_params;
        }
 
        location ~ /\. {
            deny all;
        }

Here I tried this option for the API, but there is a crash: "Access denied" throughout the application (added (32-35 lines, 44 lines to the previous config):
location / {
            root $base_root/frontend/web;
            try_files $uri $uri/ /frontend/web/index.php$is_args$args;
 
            location ~ ^/assets/.+\.php(/|$) {
                deny all;
            }
        }
 
    location /admin {
        alias $base_root/backend/web;
        try_files  $uri /backend/web/index.php?$args;
 
        location = /admin/ {
           return  301 /admin;
        }
 
        location ~* ^/admin/(.+\.php)$ {
            try_files  $uri /backend/web/$1?$args;
        }
 
    }
 
        location /api {
            alias $base_root/api/web;
            try_files  $uri /api/web/index.php?$args;
 
            location = /api/ {
               return  301 /api;
            }
 
            location ~* ^/api/(.+\.php)$ {
                try_files  $uri /api/web/$1?$args;
            }
        }
 
        location ~ [^/]\.php(/|$) {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            if (!-f $document_root$fastcgi_script_name) {
                return  404;
            }
        rewrite (?!^/((frontend|backend)/web|admin))^ /frontend/web$uri break;
        rewrite (?!^/backend/web)^/admin(/.+)$ /backend/web$1 break;
        rewrite (?!^/api) /api/web$1 break;
 
            fastcgi_pass    127.0.0.1:9001;
            fastcgi_index   index.php;
            include         /etc/nginx/fastcgi_params;
        }
 
        location ~ /\. {
            deny all;
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-04-04
@slo_nik

Good morning.
Perhaps you have a problem with the rule.
An example from .htaccess, but maybe it will lead you to the right solution. Accordingly, you will also need to change the paths to the configuration and vendor files in api/web/index.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question