I have a project with a frontend and a backend, which runs on nginx. The frontend is found at example.localhost, and is working as expected. I would like to have the backend at example.localhost/admin, but I just don’t get it working. I have been struggling some time with different locations setups, but nothing works. Have anybody set this up before, and is willing to share some tips? It would certainly be greatly appreciated!
I have a similar project but instead of a subfolder for the backend I used a subdomain. Perhaps the following can give you some ideas:
Inside /etc/nginx/conf.d…
myfront.conf:
server {
listen 1.2.3.4:80;
set $host_path "/home/myproject/frontend";
server_name myproject.com *.myproject.com;
root $host_path/www; # web accesible folder is www under frontend
set $yii_bootstrap "index.php";
charset utf-8;
location / {
index index.html $yii_bootstrap;
try_files $uri $uri/ /index.php;
}
... rest of config
myback.conf:
server {
listen 1.2.3.4:80;
set $host_path "/home/myproject/backend";
server_name admin.myproject.com;
root $host_path/www; # web accesible folder is www under backend
set $yii_bootstrap "index.php";
charset utf-8;
location / {
index index.html $yii_bootstrap;
try_files $uri $uri/ /$yii_bootstrap?$args;
}
... rest of config
Thanks for the reply. Unfortunately I cannot use subdomains. This is because I also have an api location which is to be named example.localhost/api. If I put this in a subdomain I get into trouble when making ajax requests from example.localhost…
You could use api as a subdomain but if that’s not feasible look into mapping your different locations (/ for frontend, /admin for backend, /api for api) to a specific folder alias. Post your .conf to get a clearer view.
server {
listen 80;
server_name example.localhost;
root /vagrant/frontend/www;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}
location /admin {
alias /vagrant/backend/www/;
try_files $uri $uri/ /admin/index.php?$args;
location ~ \.php$ {
include /etc/nginx/fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}
}
}
The url “example.localhost/admin” actually gets processed, but as soon as I add something like “example.localhost/admin/site/index”, it looks like the /admin location doesn’t match, cause it is routed to frontend…
From the URL example.localhost/admin/site/index it seems you’re trying to hide index.php (as mentioned here for Apache). If so, take a look at the urlManager rules in your Yii config/main.php, perhaps they need customization to handle your routes properly.