2016-10-28 2 views
12

저와 같은 문제를 가진 많은 사람들을 찾았지만 적절한 해결책을 찾을 수 없었습니다.NGINX가 나를 대신하여 PHP 파일을 제공합니다.

방글라데시와 농가를 통해 NGINX 서버를 실행 중입니다. 생산 종료에 나는 아파치를 사용하고, 따라서 나는 htaccess로 있습니다

RewriteEngine On 
RewriteCond %{REQUEST_URI} !(^cms-system/public|^assets) 
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteRule ^(.*)$ cms-system/public/$1 [L] 

내가 CMS에 내 index.php를로 (CMS 시스템/공공 및 자산 제외) 모든 URL을 다시 작성이 htaccess로를 사용을 -system/public.

이 도구로이 htaccess를 nginx 설정으로 변환하려고 시도했습니다. https://winginx.com/en/htaccess 잘 작동하지 않았습니다. 몇 가지 조정을했습니다. 예외 규칙은 작동하지만 index.php가 다시 작동하도록 할 수는 없습니다. 누군가 나를 도울 수 있을까요? 이 모든입니다

server { 
    listen 80; 
    listen 443 ssl http2; 
    server_name company.dev; 
    root "/home/vagrant/company/"; 

    index index.html index.htm index.php; 

    charset utf-8; 

    location ~ ^/cms-system/public/(.*) { 

    } 

    location ~ ^/assets/(.*) { 

    } 

    location/{ 
     if ($request_uri !~ "-f"){ 
      rewrite ^(.*)$ /cms-system/public/$1 break; 
     } 
    } 

# original location rule 
# location/{ 
#  try_files $uri $uri/ /index.php?$query_string; 
# } 

    location = /favicon.ico { access_log off; log_not_found off; } 
    location = /robots.txt { access_log off; log_not_found off; } 

    access_log off; 
    error_log /var/log/nginx/company.dev-error.log error; 

    sendfile off; 

    client_max_body_size 100m; 

    location ~ \.php$ { 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 
     fastcgi_index index.php; 
     include fastcgi_params; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 

     fastcgi_intercept_errors off; 
     fastcgi_buffer_size 16k; 
     fastcgi_buffers 4 16k; 
     fastcgi_connect_timeout 300; 
     fastcgi_send_timeout 300; 
     fastcgi_read_timeout 300; 
    } 

    location ~ /\.ht { 
     deny all; 
    } 

    ssl_certificate  /etc/nginx/ssl/company.dev.crt; 
    ssl_certificate_key /etc/nginx/ssl/company.dev.key; 
} 
+3

PHP7은'/ var/run/php/php7.0-fpm.sock' 파일을 만들고 있습니까? 기본 PHP-FPM 풀은 해당 소켓 파일을 사용해야합니다. –

+0

안녕하세요. 대답은 정말 끔찍하기 때문에 나를 도와 드리겠습니다. 다음 진술로 수정합니까? 1)'/ images/logo.png'와'/images/logo.png'을 찾을 수 없지만, '/cms-system/public/images/logo.png'이 존재한다면이 파일을 보내야합니다. 2) 찾을 수없는 모든 요청 (/ assets/* 제외)은 '/cms-system/public/index.php'를 통해 제공됩니다. 또한, 4)'/ cms-system/public/index.php' 또는'/ cms-system/public/images/logo.png'와 같은 직접 요청에 대한 지원이 필요합니까? 그리고 5)'/ some.php/custom/path /'와 같은 요청이 필요합니까? –

답변

6

당신이 필요합니다

location/{ 
try_files $uri @rewrite; 
} 

location @rewrite { 
if ($uri !~* "^/(cms-system\/public|assets)$") {set $block "A";} 
if (!-e $request_filename) {set $block "${block}B";} 
if ($block = "AB") {rewrite ^(.*)$ /cms-system/public/$1 last;} 
} 

대신 :

RewriteEngine On 
RewriteCond %{REQUEST_URI} !(^cms-system/public|^assets) 
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteRule ^(.*)$ cms-system/public/$1 [L] 

이 보안상의 이유로 권장 :

location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ { 
deny all; 
} 

읽기도 :
porting-standard-apaches-mod_rewrite-rules-to-nginx
How do I convert mod_rewrite (QSA option) to Nginx equivalent?

+3

감사합니다! 그것은 지금 –

+3

수정 작동합니다. 그것은 결국 작동하지 않습니다 ... 캐싱 날 바보있어. 나는 자산과 valemus/public 디렉토리에 직접 접근 할 수 있지만, 최상위 디렉토리가 아닌 전체 경로를 통해서만 가능합니다. 기본 URL은 403이고 다른 모든 URL은 500이됩니다. –

+3

@MartijnImhoff nginx는 해당 폴더에 대한 액세스 권한을 갖고 있습니까? –

관련 문제