方法以下:php
1. 在須要使用.htaccess文件的目錄下新建一個.htaccess文件,
css
vim /var/www/html/.htaccesshtml
2. 在裏面輸入規則,我這裏輸入Discuz的僞靜態規則:nginx
# nginx rewrite rule
rewrite /!.(js|gif|jpg|png|css)$ /index.php;
# end nginx rewrite ruleweb
wq保存退出。apache
3. 修改nginx配置文件:vim
vim /etc/nginx/nginx.conf分佈式
4. 在須要添加僞靜態的虛擬主機的server{}中引入.htaccess文件,如圖所示:server
server { listen 8081; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; root D:/www; include D:/www/.htaccess; index index.php,index.phtml,index.html index.htm; if (!-e $request_filename){ rewrite ^/(.*)$ /index.php last; } } location ~ \.ht { deny all; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root D:/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
include /var/www/html/.htaccess;(把這個改爲你.htaccess文件的具體位置)htm
wq保存退出。
5. 從新加載nginx配置文件:
/etc/init.d/nginx reload
從新打開網頁看看,若是僞靜態正常就證實你的rewrite rule語法是正確的。
正常,完畢!
補充:偶在網上發現了個能夠在線將Apache Rewrite僞靜態規則自動轉換爲Nginx Rewrite網頁。你們能夠試試看。
http://www.anilcetin.com/convert-apache-htaccess-to-nginx/
此地址裏面的內容包含能夠完成上面說的略作修改的功能。就是把.htaccess中的規則自動轉換成nginx下面可用的規則。
總結:.htaccess文件原本是apache專用的分佈式配置文件,提供了針對每一個目錄改變配置的方法,即在一個特定的目錄中放置一個包含指令的文件,其中的指令做用於此目錄及其全部子目錄。其實修改一下,nginx也可以使用.htaccess文件實現多種功能。實現僞靜態只是.htaccess的其中一個用途,.htaccess還能夠作不少的用途,如過濾訪問IP,設置web目錄訪問權限、密碼等。