아파치

아파치

이 htaccess 규칙을 nginx로 변환하려고 했습니다.

아파치

# Necessary to prevent problems when using a controller named "index" and having a root index.php
# more here: http://httpd.apache.org/docs/2.2/content-negotiation.html
Options -MultiViews

# Activates URL rewriting (like myproject.com/controller/action/1/2/3)
RewriteEngine On

# Disallows others to look directly into /public/ folder
Options -Indexes

# When using the script within a sub-folder, put this path here, like /mysubfolder/
# If your app is in the root of your web folder, then leave it commented out
RewriteBase /php-mvc/

# General rewrite rules
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

nginx

server {
    listen  80;
    listen  [::]:80 default_server ipv6only=on;

    root /home/goggelz/www;
    server_name localhost;
    rewrite_log on;

    location / {
        index index.php;
    }

    location /php-mvc {
        if (!-d $request_filename){
            set $rule_0 1$rule_0;
        }
        if (!-f $request_filename){
            set $rule_0 2$rule_0;
        }
        if (!-e $request_filename){
            set $rule_0 3$rule_0;
        }
        if ($rule_0 = "321"){
            rewrite^/(.+)$ /php-mvc/index.php?url=$1 last; 
        }

    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;

    }
}

문제는 /home/goggelz/www/php-mvc 폴더에서만 재작성을 사용하고 싶고 또 다른 문제는 다음과 같습니다. localhost를 열려고 하면 index.php 파일이 다운로드되지만 시도할 때 127.0.0.1에 연결하면 index.php가 실행됩니다. 어떻게 그 문제를 해결할 수 있나요?

답변1

nginx 지시문을 사용하는 버전입니다 try_files.

server {
    listen  80;
    listen  [::]:80 default_server ipv6only=on;

    root /home/goggelz/www;
    server_name localhost;
    rewrite_log on;

    index index.php;

    location /php-mvc {
        try_files $uri $uri/ @mvcrewrite;
    }

    location @mvcrewrite {
        rewrite ^ /php-mvc/index.php?url=$request_uri last; 
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

여기서는 try_files요청한 URI가 파일인지 디렉터리인지 먼저 확인하는 데 사용됩니다. 존재하는 경우 전송됩니다. 존재하지 않는 경우 요청은 블록으로 전달되며 @mvcrewrite, 여기서 요청은 index.php에 URI를 전달하도록 다시 작성됩니다.

이렇게 하면 localhost를 열 때 발생하는 문제가 해결될 수도 있습니다.

관련 정보