简体   繁体   中英

Setup SlimPHP .htaccess in Nginx

I'm trying to setup my PHP application in my Raspberry Pi. I'm using a Nginx server to host the application. The SlimPHP requires a .htaccess configuration to redirect all the requests to the index.php file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

I've converted the htaccess using the Winginx tool and edited the /etc/nginx/nginx.conf file adding the following source, but Nginx reported a configuration error:

# nginx configuration
location / {
    if (!-e $request_filename){
        rewrite ^(.*)$ /index.php break;
    }
}

Restarting nginx: nginx: [emerg] unknown directive "location" in /etc/nginx/nginx.conf:98 nginx: configuration file

/etc/nginx/nginx.conf test failed

How should I proceed?

UPDATE

I 've edited the nginx.conf file as recommended but the Nginx still doesn't redirecting the URL. It shows a File not found answer. Should I edit the nginx.conf file or create any other configuration file?

In general, you want something like this. You can toggle the SLIM_MODE to development as needed.

[Edit: Oops, posted an old version, updated to remove the if.]

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

location ~ \.php$ {
    try_files $uri =404; # not needed if you have cgi.fix_pathinfo=false in php.ini
    include fastcgi_params;
    fastcgi_param SLIM_MODE production;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM