PHP MVC Framework 搭配 Nginx + PHP-FPM 設定檔

回覆文章
yehlu
Site Admin
文章: 3244
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

PHP MVC Framework 搭配 Nginx + PHP-FPM 設定檔

文章 yehlu »

http://blog.wu-boy.com/2012/10/php-mvc- ... x-php-fpm/

相信大家都知道 Nginx 搭配 PHP-FPM 用起來效能還不錯,這次來筆記如何設定 Nginx 去除 PHP MVC Framework 討厭的 index.php 字串,不管是 Laravel 或 CodeIgniter 教學文件都是在 Apache 設定 .htaccess 來達成 Cleaner URL,Apache 最大好處支援 .htaccess,但是 Nginx 也有強大的效能,此篇紀錄如何設定 Nginx 達成 mod_rewrite 效果。 首先來看看 apache .htaccess 是如何設定:
1
2
3
4
5
6
7
8
<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 改成底下
1
2
3
4
5
6
7
Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php [L]
接著 Nginx 是如何設定呢?打開虛擬主機設定檔 /etc/nginx/sites-available/xxxx,將底下設定寫入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
}
}
基本上按照上面的設定,你就可以成功移除 index.php 字眼,底下來解釋此設定檔
1
2
3
4
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
此設定會將 /controller/index 轉成 /controller,因為每一個 Controller 預設的 method 就是 index,所以我想也不用特別顯示在 URL 上面。permanent 所代表就是 301 轉向。
1
2
3
4
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
此設定會將 URL 最後的 Slash 給拿掉,防止 SEO 取得重複資訊。
1
2
3
4
5
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
此設定是禁止存取 system 目錄,此目錄是 CodeIgniter 核心目錄,那 Laravel 沒有此問題,因為你的虛擬主機一定會指向 public 目錄,所以也無法存取上層 Laravel 核心目錄
1
2
3
4
5
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
此設定來到 URL 最後判斷,如果 URL 連結並非是實體檔案,則全部導向 /index.php?/$1,也就完成了 Cleaner URL 動作,最下面設定了 PHP-FPM Socket Server,這邊就不多說了,設定檔給大家參考,如果有任何問題請留言。
回覆文章

回到「nginx」