在apache下僅需要開啓重寫,並在網站根目錄添加重寫配置文件即可。
在nginx中,原理類似,需要對根路徑的訪問按條件進行URL重寫:
server {
listen 80;
server_name www.mysite.com;
root /var/www/www.mysite.com;
index index.html index.php;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
第一個 location 節點用於判斷當前訪問的HTTP資源是否是文件,如不是文件則進行URL重寫。
第二個 location 節點用於處理URL中包含 .php 字樣的請求,內部通過unix socket的文件IO方式實現網絡請求IO,PHP 處理器爲 php5-fpm