Nginx config for Yii2 advanced project
Author: Rafal MarguzewiczPublished:
Categories: Yii2
Tags: nginx
To work properly Yii2 framework with NGINX, have to do:
Do poprawnej pracy frameworka Yii z serwerem HTTP Nginx, należy:
- Install framework Yii2 template advanced
- Configuration NIGNX in config file
- Configuration Yii2 in backend config file
Configuration NIGNX
Envirement list:
path to frontend folder: /home/user/www/folder-app/frontend/
path to backend folder /home/user/www/folder-app/backend/
Url frontend: http://yii.dev
Url backend: http://yii.dev/admin
server {
server_name yii.dev;
root /home/user/www/folder-app;
access_log off;
charset utf-8;
location / {
root /home/user/www/folder-app/frontend/web;
try_files $uri /index.php?$args;
}
location ~* \.php$ {
try_files $uri /frontend/web$uri =404;
include fastcgi.conf;
}
location ~* \.(css|js|jpg|jpeg|png|gif|bmp|ico|mov|swf|pdf|zip|rar)$ {
access_log off;
log_not_found off;
try_files $uri /frontend/web$uri =404;
}
location /admin {
alias /home/user/www/folder-app/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;
}
# avoid processing of calls to non-existing static files by Yii (uncomment if necessary)
location ~* ^/admin/(.+\.(css|js|jpg|jpeg|png|gif|bmp|ico|mov|swf|pdf|zip|rar))$ {
try_files $uri /backend/web/$1?$args;
}
}
}
Config Yii2 backend file
return [
'homeUrl' => '/admin/',
'components' => [
'request' => [
'baseUrl' => '/admin',
],
]
]
If this localhost, don’t forget add yii.dev to hosts
sudo nano /etc/hosts
Example /etc/hosts on localhost
127.0.0.1 localhost yii.dev pceuropa.dev wordpress.dev
127.0.1.1 m