Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx yii2 configuration

Tags:

nginx

centos

yii

Hi there!

I'am trying to configure Nginx for 2 yii projects, frontend for users and admin for admins with only one domain (no sub domain). I need to configure it in a way such that mydomain.com should refer to frontend and mydomain.com/admin to admin. The problem is I'am being able to configure only one of them at a time, meaning I can use frontend or admin not both of them.

What I have tried

front.conf

server {
        listen 80;
        server_name api.maim.experiments.uz;
        return 301 https://$server_name$request_uri;
}


server {
    charset utf-8;
    client_max_body_size 128M;

    listen 443 ssl;

    ssl_certificate_key privkey.pem;
    ssl_certificate     fullchain.pem;

    ssl_protocols TLSv1.2;

    set $host_path "/home/itschool/inha_dev/frontend";   

    server_name  api.maim.experiments.uz;
    root        $host_path/web;

    set $yii_bootstrap "index.php";

    access_log  /var/log/nginx/itschool-access.log;
    error_log   /var/log/nginx/itschool-error.log;

    location / {
        index index.html $yii_bootstrap;
        try_files $uri $uri/ /index.php;
    }

    location ~ ^/(protected|framework|themes/\w+/views) {
        deny  all;
    }

    location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        try_files $uri =404;
    }

    location ~ \.php$ {

        set $fsn /index.php;
           if (-f $document_root$fastcgi_script_name){
               set $fsn $fastcgi_script_name;
        }

        fastcgi_pass 127.0.0.1:9002;

        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fsn;
    }

    location ~ /\.(ht|svn|git) {
        deny all;
    }

    location ~* /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
}

back.conf

server {
        listen 80;
        server_name api.maim.experiments.uz;
        return 301 https://$server_name$request_uri;
}


server {
    charset utf-8;
    client_max_body_size 128M;

    listen 443 ssl;

    ssl_certificate_key privkey.pem;
    ssl_certificate     fullchain.pem;

    ssl_protocols TLSv1.2;

    set $host_path "/home/itschool/inha_dev/backend";   

    server_name  api.maim.experiments.uz;
    root        $host_path/web;

    set $yii_bootstrap "index.php";

    access_log  /var/log/nginx/itschool-access.log;
    error_log   /var/log/nginx/itschool-error.log;


    location ^~ /admin {
        alias /home/itschool/inha_dev/backend/web;

        if (!-e $request_filename) { rewrite ^ /admin/index.php last; }

        location ~ \.php$ {
            if (!-f $request_filename) { return 404; }

            include        fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME $request_filename;
            fastcgi_pass   127.0.0.1:9002;
        }
    }

    location ~ /\.(ht|svn|git) {
        deny all;
    }

    location ~* /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
}

I found some questions with answers but they didn't work for me, please help.

like image 545
Mukhammadsher Avatar asked Dec 07 '25 08:12

Mukhammadsher


1 Answers

I have recently use similar configuration to support web application / mobile application and admin panel on single domain

I hope this could help you out. Below is the configuration

server {
        listen 80;

        set $root /var/www/html/application;

        #here we go
        #if backend not found in url then set root url
        if ($uri !~ "^(.*)/(backend)(.*)") {
            set $root /var/www/html/application/frontend/web;
        }

        # when request is coming from mobile then display mobile site
        # you don't need this one, I just written in order to explain the mobile application navigation.
        if ($http_user_agent ~* "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos") {
            set $root /var/www/html/application/mobile/web;
        }

        root $root;

        index index.php index.html index.htm index.nginx-debian.html;
        server_name your_domain;

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }

        location / {
            index  index.html index.php;
            if (!-e $request_filename){
                rewrite ^/(.*) /index.php?r=$1 last;
            }
        }

        location ~ /\.ht {
                deny all;
        }
}

Also have a look in official document of Yii2 to setup yii2-app-advanced on single domain (Apache, Nginx). CLICK HERE

One more thing that you need to know is if you want to change backend/web to admin then you also have to made some changes in Yii2 application.

like image 130
Aabir Hussain Avatar answered Dec 09 '25 14:12

Aabir Hussain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!