当前位置首页 > Nginx知识

tp3.23nginxlnmp填坑

阅读次数:196 次  来源:admin  发布时间:

thinkphp3.23在apache上可以轻松实现4个路由模式

但是在nginx上就出现问题

我们的环境是用lnmp包实现(地址:https://lnmp.org/)

安装完成后,ta的lnmp的nginx是默认在 /usr/local/nginx(linux)

在/usr/local/nginx/conf下,我们发现有个thinkphp.conf文件

location / {
    if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php?s=/$1 last;
        break;
    }
}

如果我们用这个配置站点可以实现:

http://test.cc/home/index/index

但是如果要用:

http://test.cc/index.php/home/index/index

不能成功。

我们可以新建一个tp3.23.conf的配置文件

location / {
    try_files $uri @rewrite;
}

location @rewrite {
    set $static 0;
    if  ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) {
            set $static 1;
    }
    if ($static = 0) {
            rewrite ^/(.*)$ /index.php?s=/$1;
    }
}
location ~ \.php/ {
        if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { }
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_NAME     $1;
        fastcgi_param PATH_INFO       $2;
        fastcgi_param SCRIPT_FILENAME $document_root$1;
}
location ~ \.php$ {
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
}

那vhost如下

server
{
    listen 80;
    server_name test.cc ;
    index index.html index.htm index.php;
    root  /home/wwwroot/tp3.23;

    #error_page   404   /404.html;
    

    #rewrite
    include tp3.23.conf;
    location ~ /\.ht {
            deny  all;
    }
    #rewrite结束

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

    location ~ /\.
    {
        deny all;
    }

}

如上就可以实现

http://test.cc/home/index/index 与 http://test.cc/index.php/home/index/index

参考:

https://lnmp.org/faq/lnmp-vhost-add-howto.html

https://www.cnblogs.com/dee0912/p/5208002.html

http://www.thinkphp.cn/topic/34380.html

上一篇:IIS如何设置多个Access-Control-Allow-Origin
下一篇:新换的FedoraCoreLinux桌面,来秀一秀(多图)