M
M
Maxim Lagoysky2018-09-11 15:51:37
Nginx
Maxim Lagoysky, 2018-09-11 15:51:37

How to properly configure nginx for yii2?

Good afternoon everyone, help me figure out why the backend does not work under the site.ru/admin link.
When I go to the link 193.200.74.52/admin, I catch a 404 error and the project continues to look in frontend/web
I'm just learning to work with nginx, so don't throw stones. Here is the nginx config

server {

    #client_max_body_size 200M;

    listen 80 default_server;
    listen [::]:80 default_server;

    server_name 193.200.74.52;
    root /var/www/notice.com/frontend/web;
   
    index index.php;

    charset utf-8;
    
    location / {
        root /var/www/notice.com/frontend/web;
        try_files $uri $uri/ /index.php$is_args$args;
        
        rewrite ^/(.*)/$ /$1 permanent;
        
        location ~ ^/assets/.+\.php(/|$) {
            deny all;
        }
    }
    
    location /admin {
        root /var/www/notice.com/backend/web;
        try_files $uri $uri/ /index.php$is_args$args;
        
        rewrite ^/(.*)/$ /$1 permanent;
        
        location ~ ^/assets/.+\.php(/|$) {
            deny all;
        }
    }
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }
    
    #location ~ /\.ht {
    #    deny all;
    #}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mysterion, 2018-09-11
@lagoy

server {
    listen 80 default_server;
    server_name notice.com;

    set $base_root /var/www/notice.com;
    root $base_root;

    charset UTF-8;
    index index.php index.html;

    location / {
        root $base_root/frontend/web;
        try_files $uri $uri/ /frontend/web/index.php$is_args$args;

        location ~ ^/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location /admin {
        alias $base_root/backend/web/;

        try_files $uri /backend/web/index.php$is_args$args;

        location ~ ^/admin/assets/.+\.php(/|$) {
            deny all;
        }
    }

    location ~ ^/.+\.php(/|$) {
        rewrite (?!^/((frontend|backend)/web|admin))^ /frontend/web$uri break;
        rewrite (?!^/backend/web)^/admin(/.+)$ /backend/web$1 break;

        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location ~ /\. {
        deny all;
    }
}

D
Dmitry, 2018-09-11
@slo_nik

Good afternoon.
Here you will find an example of setting up Nginx

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question