Yii Framework Forum: Nginx Setup With Frontend And Backend - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Nginx Setup With Frontend And Backend

#1 User is offline   Kjell Einar Skutle 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 30-November 11
  • Location:Norway

Posted 24 April 2014 - 12:59 PM

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!
0

#2 User is offline   JFReyes 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 517
  • Joined: 28-October 09
  • Location:Puerto Rico

Posted 24 April 2014 - 02:10 PM

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...
José
0

#3 User is offline   Kjell Einar Skutle 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 30-November 11
  • Location:Norway

Posted 24 April 2014 - 02:18 PM

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...
0

#4 User is offline   JFReyes 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 517
  • Joined: 28-October 09
  • Location:Puerto Rico

Posted 24 April 2014 - 03:45 PM

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.
José
0

#5 User is offline   Kjell Einar Skutle 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 30-November 11
  • Location:Norway

Posted 25 April 2014 - 01:15 AM

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...
0

#6 User is offline   JFReyes 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 517
  • Joined: 28-October 09
  • Location:Puerto Rico

Posted 25 April 2014 - 07:26 AM

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.
José
0

#7 User is offline   Kjell Einar Skutle 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 30-November 11
  • Location:Norway

Posted 26 April 2014 - 08:20 AM

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
0

#8 User is offline   JFReyes 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 517
  • Joined: 28-October 09
  • Location:Puerto Rico

Posted 26 April 2014 - 10:12 AM

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.
José
0

#9 User is offline   Kjell Einar Skutle 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 30-November 11
  • Location:Norway

Posted 26 April 2014 - 10:34 AM

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;
    }
}

0

#10 User is offline   JFReyes 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 517
  • Joined: 28-October 09
  • Location:Puerto Rico

Posted 27 April 2014 - 05:27 AM

Excellent! I didn't know that directive so thank you for the tip :)
José
0

#11 User is offline   Sohail Ahmed 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 15-July 14

Posted 15 July 2014 - 01:27 PM

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 ?.
0

#12 User is offline   reza saeed zadeh 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 14-January 18

Posted 14 January 2018 - 08:16 AM

thanks
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users