cláusula de localização NGINX apenas para o domínio raiz

1

Como eu só redireciono também sub1.example.com ao atingir $request_filename ~ /sub1 em example.com . Por exemplo, se eu estiver em sub1.example.com , acertar /sub1 não me redirecionará também sub1.example.com , mas sim a página real sub1.example.com/sub1 ?

Minha configuração do NGINX:

server
{
    server_name .example.com;
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
    root /var/www/html/example;
    index index.php;

    location ~ \.php$ {
        if (-f $request_filename) {
            fastcgi_pass    127.0.0.1:9000;
        }
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }

    location / {
        if ($request_filename ~ /sub1) {
            rewrite ^ http://sub1.example.com/? permanent;
        }
        if ($request_filename ~ /sub2) {
            rewrite ^ http://sub2.example.com/? permanent;
        }
        if (!-f $request_filename) {
            rewrite ^(.*)$ /index.php last;
        }
    }
}

Como uso a cláusula location somente para domínio raiz?

Atualizar @ Marcel:

server
{
    server_name .example.com;
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
    root /var/www/html/example;
    index index.php;
    location ~ \.php$ {
        if (-f $request_filename) {
            fastcgi_pass    127.0.0.1:9000;
        }
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }

    location = /sub1 {
       if($host = "example.com") {
           rewrite ^ http://sub1.example.com permanent;
       }
    }
    location = /sub2 {
       if($host = "example.com") {
           rewrite ^ http://sub2.example.com permanent;
       }
    }
    location / {
       if (!-f $request_filename) {
           rewrite ^(.*)$ /index.php last;
       }
    }
}
    
por Dan Kanze 05.04.2013 / 21:13

2 respostas

1

Não use If dentro de Location block. muito melhor para declarar várias seções server {} na configuração com diferentes server_name .

Em vez disso, use if (-f $request_filename) { use try_files directive.

    
por 08.04.2013 / 12:07
0

O Nginx trata as diretivas de localização de uma determinada maneira, dependendo de quais diretivas você possui. Este link indica o comportamento do nginx:

A location can either be defined by a prefix string, or by a regular expression.
Regular expressions are specified by prepending them with the “~*” modifier (for case-insensitive matching), or with the “~” modifier (for case-sensitive matching).

To find a location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the most specific one is searched.
Then regular expressions are checked, in the order of their appearance in a configuration file. A search of regular expressions terminates on the first match, and the corresponding configuration is used.

If no match with a regular expression is found then a configuration of the most specific prefix location is used."

location / é um local curinga que corresponderá a todas as solicitações.

Isto é o que os seus locais devem ser:

location ~ \.php$ {
    if (-f $request_filename) {
        fastcgi_pass    127.0.0.1:9000;
    }
    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include         fastcgi_params;
}
location = /sub1 {
   if($host = "example.com") {
       rewrite ^ http://sub1.example.com permanent;
   }
}
location = /sub2 {
   if($host = "example.com") {
       rewrite ^ http://sub2.example.com permanent;
   }
}
location / {
   if (!-f $request_filename) {
       rewrite ^(.*)$ /index.php last;
   }
}
    
por 05.04.2013 / 21:49

Tags