相信大家都知道 Nginx 搭配 PHP-FPM 用起来效能还不错,这次来笔记如何设定 Nginx 去除 PHP MVC Framework 讨厌的 index.php 字符串,不管是 Laravel 或 CodeIgniter 教学文件都是在 Apache 设定 .htaccess 来达成 Cleaner URL,Apache 最大好处支持 .htaccess,但是 Nginx 也有强大的效能,此篇纪录如何设定 Nginx 达成 mod_rewrite 效果。
首先来看看 apache .htaccess 是如何设定:
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]</IfModule>
上面的意思就是代表如果该 URL 是不存在的档案或者是目录就全部导向 index.php,如果在 Ubuntu 底下可能会产生 Loop,请把 .htaccess 改成底下
Options +FollowSymLinksRewriteEngine onRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . index.php [L]
接着 Nginx 是如何设定呢?打开html' target='_blank'>虚拟主机设定文件 /etc/nginx/sites-available/xxxx,将底下设定写入
server { listen 80; server_name laravel.wuboy.twbbs.org; root /usr/home/git/laravel/public; access_log /var/log/nginx/laravel_access.log; error_log /var/log/nginx/laravel_error.log; location / { index index.php index.html index.htm; } if ($request_uri ~* index/?$) { rewrite ^/(.*)/index/?$ /$1 permanent; } # removes trailing slashes (prevents SEO duplicate content issues) if (!-d $request_filename) { rewrite ^/(.+)/$ /$1 permanent; } # removes access to "system" folder, also allows a "System.php" controller if ($request_uri ~* ^/system) { rewrite ^/(.*)$ /index.php?/$1 last; break; } # unless the request is for a valid file (image, js, css, etc.), send to bootstrap if (!-e $request_filename) { rewrite ^/(.*)$ /index.php?/$1 last; break; } # catch all error_page 404 /index.php; # use fastcgi for all php files location ~ /.php$ { fastcgi_pass unix:/tmp/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include /usr/local/etc/nginx/fastcgi_params; fastcgi_param HTTPS off; } # deny access to apache .htaccess files location ~ //.ht { deny all; }}
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
新闻热点
疑难解答
图片精选