1

I'm trying to build proper server configuration for nginx serving Yii2 advanced template where backend hosted in a subfolder inside same domain name as frontend.

In this case path_to_yii2 contains whole Yii2 application template and we have these requirements:

path_to_yii2/frontend/web -> should be webroot of / location

path_to_yii2/backend/web -> should be webroot of /backend location

Static content in both folders should be properly served. PHP files should work in both cases.

I wrote and tested such configuration:

server {
   listen        80;
   server_name  localhost;

   root <path_to_yii2_application>;

    location ~* ^/backend/(.+)$ {
        try_files  /backend/web/$1 /backend/web/$1/ /backend/index.php?$args;
        index /backend/$1/index.php; # not working in case of exact /backend/ request

        location ~* ^/backend/(.+\.php)$ {
            #fastcgi_pass   127.0.0.1:9000;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include        fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME $document_root/backend/web/$1;
        }

    }

    location / {
        try_files  /frontend/web/$uri /index.php?$args;
        index /$uri/index.php; # not working at all, but / location is served by php even without this line
    }

    location ~ \.php$ {
        #fastcgi_pass   127.0.0.1:9000;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root/frontend/web/$fastcgi_script_name;
    }
}

And I have some unresolved problems with such configuration. I tested six different options:

FRONTEND:

  1. Static content under / location should be served directly from frontend/web subfolder of yii2 application folder.
  2. Nonexistent content here should be redirected to frontend/web/index.php?$args and served using ~ .php$ location with fastcgi.
  3. Directories under / location should return indexes, if needed served with ~ .php$ location and fastcgi.

BACKEND:

  1. Static content under /backend location should be served directly from backend/web subfolder of yii2 application folder.
  2. Nonexistent content here should be redirected to backend/web/index.php?$argsand served using ~ .php$ location with fastcgi.
  3. Directories under /backend location should return their indexes, if needed served with ~ .php$ location and fastcgi.

I have troubles with directories and their indexes (3 and 6).

First of all, directories for frontend section not working at all, seems that index /$uri/index.php; is wrong for some reason.

Secondly, directories for backend working except exact /backend/ url. Nginx doesn't serve index directive in =/backend/ case.

As a temporarily workaround for backend I added few lines for this exact url:

location = /backend {
    return 301 https://$server_name/backend/index.php;
}

location = /backend/ {
    return 301 https://$server_name/backend/index.php;
}

How to fix these indexes correctly and what I'm doing wrong?

P.S. There are some similar questions, like Migrating Yii2 from Apache to Nginx - failed on backend app (doesn't provide correct answer, recommends using subdomain) and Nginnx config for Yii 2 Advanced App Template (suggested to move backend content inside yii2 application to frontend folder). I believe that nginx configuration is a proper way of congiguring yii2-application template. There is also https://github.com/mickgeek/yii2-advanced-one-domain-config repositary which not works in nginx > 1.8.1.

Interesting that apache just needs a symbolic link to make this work. Nginx before 1.8.1 too.

Yii2 application template can be git cloned from here: https://github.com/yiisoft/yii2-app-advanced.git

Community
  • 1
  • 1
V G
  • 1,225
  • 9
  • 13
  • Uh... that's a lot of requirements there... I think it's better to break this down into multiple pieces. – Gynteniuxas Sep 16 '16 at 18:27
  • Edvin, everything working except indexes. I think that some non-evident correction will fix that issue. – V G Sep 18 '16 at 09:47

0 Answers0