现有Apache .htaccess到Nginx配置

时间:2017-09-15 10:49:53

标签: php apache .htaccess nginx

我想我已经失去了一点点。

我尝试了一些解决方案,但我没有得到一个部分。一切都必须由index.php处理,我无法在nginx中使用它。我一直在学习Nginx很棒,但我真的很感激某些人的快速帮助。

我的.htaccess:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_route_=$1 [L,QSA]

我的Nginx配置是:

server {
        listen 80;
        listen [::]:80;

        root /folder;
        index index.php index.html index.htm index.nginx-debian.html;

        server_name hostname.com;

        location / {
        try_files $uri $uri/ =404;
            }

    location ~ \.php$ {
        try_files = $uri @missing;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
        location @missing {
           rewrite ^ $scheme://$host/index.php permanent;
}
}

有人能指出我正确的方向吗?感谢

1 个答案:

答案 0 :(得分:0)

任何不以.php结尾的URI都由location /块处理。如果要实现Apache .htaccess规则,那就应该这样做。有关详情,请参阅this document

例如:

location / {
    try_files $uri $uri/ /index.php?_route_=$uri;
}

location ~ \.php$区块中,您应该防范passing uncontrolled requests to PHP,例如:

location ~ \.php$ {
    try_files $uri =404;
    ...
}