之前一直使用apache 服務器,現有一項目想轉到Nginx 服務器運行。。
發現Apache 的撰寫功能和 Nginx的不一樣。無法通用.hataccess 文件
查閱網上資料,nginx 配置轉寫功能 是直接在配置文件中 配置的:
虛擬服務器設置:
server { listen 81; server_name localhost:81; root "D:/phpStudy/WWW/yii2/anran/backend/web"; location / { index index.html index.php;#默認主頁 autoindex on;#是否允許訪問目錄 include D:/phpStudy/WWW/yii2/anran/backend/web/.htaccess; #關鍵htaccess 導入 } #php 解析器 location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } }
其中
include D:/phpStudy/WWW/yii2/anran/backend/web/.htaccess; #關鍵htaccess 導入
是由apache 的.htaccess 轉化過來的 網址:http://winginx.com/en/htaccess
原.htaccess
Options +FollowSymLinks IndexIgnore */* RewriteEngine on # if a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward it to index.php RewriteRule . index.php
轉換后.htaccess (就是nginx導入的配置)
if (!-e $request_filename){ rewrite ^(.*)$ /index.php; }