0

I've configured a Yii2 advanced template site (advanced meaning 2 sites: first is frontend site and the second is the admin area)

My frontend (Nginx's root) is at /public_html and my admin is at /public_html/admin.

Almost everything works with the exception of two endpoints that were developed in a "different way".

I think this is were my problem resides: Usually any request will point to www.mysite.com/<controller_name>/index and the index.php file will take the request, and let PHP decide what to do given the application routes.

Unfortunately I have two endpoints (one in frontend and another in admin) that are www.mysite.com/<controller_name>/action (mind index was replaced by action). So now Nginx is routing that request to index but should be pointing it to action. Or at least this is how I see it. Amiright?

Here's what I mean:

regular frontend routes

  • Request URL: www.mysite.com/index
  • all route to public_html/index.php but served by Nginx location /

regular admin routes

  • Request URL: www.mysite.com/admin/index
  • all route to public_html/admin/index.php but served by Nginx location /admin

non-regular frontend route:

  • Request URL: www.mysite.com/not_index <== I think this is my problem
  • route to public_html/index.php still served by Nginx location / <== I think this is my problem

non-regular backend route:

  • Request URL: www.mysite.com/admin/not_index <== I think this is my problem
  • route to public_html/admin//index.php still served by Nginx location /admin <== I think this is my problem

"Obviously" my two non-regular routes are resolving to 404 not found. And I'm not able to find a solution for this. I've been at it for several days now...

Here's my nginx.conf:

server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    root /var/www/mysite/public_html;
    index index.php index.html index.htm;
    server_name localhost;

    location /admin {
        root /var/www/mysite/public_html/admin;
        index index.php;
        try_files $uri  /admin/index.php$is_args$args ;

        location = /admin/ {
            return  301 /admin;
        }

        location = /admin/frontend-users/search-influencers {
            try_files  /admin/$uri /admin/$uri.php;
        }
    }


    location / {
        root /var/www/mysite/public_html;
        try_files $uri /index.php$is_args$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* \.(css|map|mp3|mp4|js|jpg|jpeg|png|gif|bmp|ico|mov|swf|pdf|zip|rar)$ {
        access_log  off;
        log_not_found  on;
        try_files  $uri /public_html/$uri =404;
    }
}

What am I doing wrong?

Luís Correia
  • 448
  • 4
  • 9
  • Read your question several times and can't understand what you are trying to explain. What is *non-regular* route, how is it different from *regular*, and how should it be served? – Ivan Shatsky Oct 07 '20 at 01:14
  • Read this https://stackoverflow.com/questions/5238377/nginx-location-priority – shingo Oct 07 '20 at 06:19
  • `root /var/www/mysite/public_html/admin;` looks wrong, you should delete all of the `root` directives **except** the one in the `server` block. – Richard Smith Oct 07 '20 at 07:47
  • 1
    maybe you need like this [https://stackoverflow.com/questions/38975970/local-nginx-custom-url](https://stackoverflow.com/questions/38975970/local-nginx-custom-url) – SXN Oct 07 '20 at 09:31
  • @IvanShatsky: I've updated my description. Is it clearer now? Thanks you – Luís Correia Oct 07 '20 at 10:08
  • Do you mean that `/admin/frontend-users/search-influencers` is an example of such a route? What PHP script should serve this request instead of `/admin/index.php`? – Ivan Shatsky Oct 07 '20 at 10:17
  • @IvanShatsky route /admin/frontend-users/*search-influencers* should serve https://example.com/admin/frontend-users/search-influencers – Luís Correia Oct 07 '20 at 14:02
  • Did you understand my question? Should request `https://example.com/admin/frontend-users/search-influencers` go to `/var/www/mysite/public_html/admin/index.php` PHP script or to some other PHP script? – Ivan Shatsky Oct 07 '20 at 14:34
  • What are you trying to achieve with `try_files /admin/$uri /admin/$uri.php;` directive? Process the request with `/var/www/mysite/public_html/admin/frontend-users/search-influencers.php` file? – Ivan Shatsky Oct 07 '20 at 14:39
  • @IvanShatsky request https://example.com/admin/users -> /var/www/mysite/public_html/admin/users/index -> location /admin | request https://example.com/admin/groups -> /var/www/mysite/public_html/admin/groups/index | ... | but request https://example.com/admin/frontend-users/search -> /var/www/mysite/public_html/admin/frontend-users/search -> location /admin | and I think the /search is the problem cuz Nginx expects /index – Luís Correia Oct 07 '20 at 15:11
  • @IvanShatsky /var/www/mysite/public_html/admin/frontend-users/search-influencers.php I am trying to override the location /admin because the aforementioned problem – Luís Correia Oct 07 '20 at 15:16
  • @IvanShatsky I've add more details to your answer, but StackOverflow doesnt let me mention you there – Luís Correia Oct 09 '20 at 11:08
  • @IvanShatsky updated – Luís Correia Oct 09 '20 at 14:00
  • Sorry, I'm completely lost with your explanations. You can define almost every processing rule you could imagine with nginx config, rewrite any URI to whatever you want, add/remove query arguments, process different requests with different PHP files etc, but I'm really can't figure what nginx rules do you want. Initially I thought you want some requests to be processed by some PHP script other than `index.php`. From all our conversation I can't even figure if it is true of not. Giving up. Sorry. Looks like my answer is useless so I remove it. – Ivan Shatsky Oct 10 '20 at 20:19

0 Answers0