------------------------------------------------------------------------- 单站点 ---------------------------------------------------------------------------
server {
listen 80;
server_name thinkphp.lo;
root /var/www;
index index.html index.htm index.php;
error_page 404 /404.html;
location = /404.html {
return 404 'Sorry, File not Found!';
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html; # windows用户替换这个目录
}
location ~ \.php/ {
if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { }
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_NAME $1;
fastcgi_param PATH_INFO $2;
fastcgi_param SCRIPT_FILENAME $document_root$1;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
------------------------------------------------------------------------- 多站点 ---------------------------------------------------------------------------
/etc/nginx/conf.d/下创建 xxxx.conf(站点配置文件名随意,扩展名必须为conf)
server {
listen 80; # 监听端口可变
server_name www.siteA.com siteA.com; # 站点域名
root /home/user/www/blog; # 站点根目录
index index.html index.htm index.php; # 默认导航页
include /home/user/www/blog/.htaccess; # 放置伪静态
#识别图片等
location ~ .*\.(jpg|gif|png|js|css)$ {
root /var/www/html/champion;
}
# PHP配置
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
}
多个站点则需要多个xxxx.conf文件配置,格式如上
------------------------------------------------------------------------- 伪静态 ---------------------------------------------------------------------------
在nginx的配置中直接include .htaccess文件就好
include /站点目录/.htaccess;
APACHE下伪静态规则:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
转换成Nginx后:
# nginx rewrite rule
if (!-d $request_filename){
set $rule_0 1$rule_0;
}
if (!-f $request_filename){
set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
rewrite /admin.php/(.*)$ /admin.php?/$1 last;
rewrite ^/(.*)$ /index.php/$1 last;
}
# end nginx rewrite rule
然后将转换好的规则替换.htaccess文件内容,将.htaccess文件导入my.conf,my.conf内容如下:
location / {
include /home/wwwroot/dijin.com/web/.htaccess;
}
--至此完成伪静态转移
方法二、如果不想保留.htaccess文件,则前面的步骤照常,最后一步替换的时候直接将转换好的内容放入my.conf文件中,如下:
location / {
if (!-d $request_filename){
set $rule_0 1$rule_0;
}
if (!-f $request_filename){
set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
rewrite /admin.php/(.*)$ /admin.php?/$1 last;
rewrite ^/(.*)$ /index.php/$1 last;
}
}
--至此完成