2017-10-05 5 views
0

나는 이런의 nginx 서버 구성이 있습니다일치하는 위치를 Nginx 서버 블록의 변수로 사용하는 방법은 무엇입니까?

server { 
    listen 80; 
    listen [::]:80; 
    root /var/www/html; 
    index index.php index.html index.htm; 
    server_name example.com www.example.com; 

    location /project1/public { 
    try_files $uri $uri/ /project1/public/index.php?$query_string; 
    } 

    location /project2/public { 
    try_files $uri $uri/ /project2/public/index.php?$query_string; 
    } 

    location /project3/public { 
    try_files $uri $uri/ /project3/public/index.php?$query_string; 
    } 

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

을하고 그것을 작동합니다. 하지만 regex (코드에 따라)를 사용하여 동적으로 처리하려고하면 URL을 브라우저에 표시하는 대신 다운로드합니다.

server { 
    listen 80; 
    listen [::]:80; 
    root /var/www/html; 
    index index.php index.html index.htm; 
    server_name example.com www.example.com; 

    location ~ ^/([^/]+)/public { 
    try_files $uri $uri/ /$1/public/index.php?$query_string; 
    } 

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

어떤 생각?

답변

1

위치 블록의 순서를 변경해보십시오.

  1. 의 Nginx는 기존의 위치 블록 (같은 /project1/public) 발견하면, 그것은하지 않습니다 : 당신이 Nginx Docs에 위치 블록의 순서/우선 순위를 선택하면

    사용자의 설정에 중요한 두 가지 규칙이 있습니다 검색을 중지하고 정규 표현식과 더 이상 기존의 블록이 먼저 일치하게 될

    -> 첫 번째 설정에 그래서, Nginx에 처음 php-regex 위치를 발사 한 후이 try_files

  2. 실행
  3. 정규 표현식은 구성 파일에 나타나는 순서대로 검사됩니다. 정규 표현식 검색은 첫 번째 일치시 종료되고 해당 구성이 사용됩니다.

    -> 두 번째 설정에서, 당신의 php-regextry_files부터 사용되지 않습니다 발견되는 첫 번째