Nginx Setup With Frontend And Backend

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



Hope this helps…

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.

This is my current config:




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.

The issue lays in the nginx config. If I change the try_files line in the /admin location block to this:


try_files   $uri $uri/ /admin/index.php

(removed ?$args), then it works, but I can’t access my GET variables

That’s correct, $args is what provides $_GET. Please post your urlManager rules to see if something can be done there to solve the problem.

Found the solution. I had to use a named location.




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 fastcgi.conf;

            }

    }


    location /admin {

            return 301 /admin/;

    }


    location /admin/ {

            alias /vagrant/backend/www/;

            try_files $uri $uri/ @handler;


            location ~ \.php$ {

                    include fastcgi.conf;

            }

    }


    location @handler {

            rewrite / /admin/index.php?$args;

    }

}



Excellent! I didn’t know that directive so thank you for the tip :)

With rewrite, you do not need to specify $args. Nginx by default attaches arguments to it and if you specify $args, it will have arguments twice.

If you want to drop $args then you can use ?.

thanks