Quantcast
Channel: Live News for Yii Framework
Viewing all articles
Browse latest Browse all 3361

[Wiki] yii2-app-advanced + Nginx on single domain

$
0
0

Introduction

If you develop rather complex applications with Yii2, you might already be familiar with yii2-app-advanced application template. Well, the template is awesome and provides high flexibility regarding code/configuration sharing among parts of an application.

What is not obvious for this template, is how to configure a web server so that both, frontend and backend will be accessible on the same domain, like following:

  • http://example.com/ - frontend
  • http://example.com/backend/ - backend

Solution

To achieve our goal, we need to slightly modify backend configuration, as well as to alter Nginx config.

Backend configuration

We need to "teach" the app components request and urlManager about our intended URL structure.

Modify file backend/config/main.php:

....
    'components' => [
        ....
        'request'=>[
            'baseUrl'=>'/backend',
        ],
        'urlManager'=>[
            'scriptUrl'=>'/backend/index.php',
        ],
    ],

Nginx configuration

Here is Nginx config, built on top of the official recommended configuration:

server {
    set $project_root /path/to/example.com;
    set $fcgi_server 127.0.0.1:9000;
    #set $fcgi_server unix:/var/run/php-fpm/example.socket;
 
    charset utf-8;
    client_max_body_size 128M;
 
    listen 80; 
 
    server_name example.com;
    root $project_root/frontend/web;
    index index.php;
 
    access_log  /var/log/nginx/example.access.log combined;
    error_log  /var/log/nginx/example.error.log warn;
 
    location ~ ^/backend/web {
        root $project_root;
 
        location ~ /\.(ht|svn|git) {
            deny all;
        }
 
        location ~ \.php$ {
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_pass $fcgi_server;
        }   
    }   
 
    location ~ ^/backend {
        rewrite ^/backend(.*)$ /backend/web$1 last;
    }   
 
    location / { 
        try_files $uri $uri/ /index.php$is_args$args;
    }   
 
    location ~ /\.(ht|svn|git) {
        deny all;
    }
 
    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass $fcgi_server;
    }   
}

Viewing all articles
Browse latest Browse all 3361

Trending Articles