Nginx, prioritize folders instead of "Location /"

Asked

Viewed 352 times

2

I use PHP with the Phalcon framework in my projects and this is the structure.

inserir a descrição da imagem aqui

My Nginx is like this:

server {
    listen   80;

    server_name 123.123.123.123;
    root /var/www/meusite.com.br;

    location @site{
        rewrite ^/public(.+)$ /public/index.php?_url=$1 last;
    }

    location / {
        index index.php;
        if ($uri !~ ^/public) {
            rewrite ^(.*)$ /public$1;
        }
        try_files $uri $uri/ @site;

        location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
            root /var/www/meusite.com.br/public;
        }
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

}

It is working fine, only now when an internal folder is requested, I need Nginx to ignore all "Location /" and execute the same commands of "Location /" but inside the requested folder. For example /v2/.

How do I do that? I’ve tried it in many ways. I’ve already Google it. Anyway, my biggest problem is because Phalcon uses a different folder structure than usual.

1 answer

0


Nginx works top to bottom configuration order, that is to summarize you have to put the Location of the folder you want to access above Location / or you inserted a rewrite inside the Location ordme from top to bottom.

Example:

server {
listen   80;

server_name 123.123.123.123;
root /var/www/meusite.com.br;

location @site{
    rewrite ^/public(.+)$ /public/index.php?_url=$1 last;
}
location /pastaserchecadaprimeiro {
  root /var/www/html/pastaserchecadaprimeiro;
}
location / {
    index index.php;
    if ($uri !~ ^/public) {
        rewrite ^(.*)$ /public$1;
    }
    try_files $uri $uri/ @site;

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root /var/www/meusite.com.br/public;
    }
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include fastcgi_params;
}

location ~ /\.ht {
    deny all;
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.