首页 > 网站 > WEB服务 > 正文

PHP MVC Framework搭配Nginx+PHP-FPM设定档

2020-03-22 20:23:19
字体:
来源:转载
供稿:网友
  • 相信大家都知道 Nginx 搭配 PHP-FPM 用起来效能还不错,这次来笔记如何设定 Nginx 去除 PHP MVC Framework 讨厌的 index.php 字符串,不管是 Laravel 或 CodeIgniter 教学文件都是在 Apache 设定 .htaccess 来达成 Cleaner URL,Apache 最大好处支持 .htaccess,但是 Nginx 也有强大的效能,此篇纪录如何设定 Nginx 达成 mod_rewrite 效果。

    首先来看看 apache .htaccess 是如何设定:

    <IfModule mod_rewrite.c>     RewriteEngine on     RewriteCond %{REQUEST_FILENAME} !-f     RewriteCond %{REQUEST_FILENAME} !-d     RewriteRule ^(.*)$ index.php/$1 [L]</IfModule>

    上面的意思就是代表如果该 URL 是不存在的档案或者是目录就全部导向 index.php,如果在 Ubuntu 底下可能会产生 Loop,请把 .htaccess 改成底下

    Options +FollowSymLinksRewriteEngine onRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . index.php [L]

    接着 Nginx 是如何设定呢?打开html' target='_blank'>虚拟主机设定文件 /etc/nginx/sites-available/xxxx,将底下设定写入

    server {    listen       80;    server_name  laravel.wuboy.twbbs.org;    root         /usr/home/git/laravel/public;    access_log /var/log/nginx/laravel_access.log;    error_log /var/log/nginx/laravel_error.log;    location / {        index  index.php index.html index.htm;    }    if ($request_uri ~* index/?$)    {        rewrite ^/(.*)/index/?$ /$1 permanent;    }    # removes trailing slashes (prevents SEO duplicate content issues)    if (!-d $request_filename)    {        rewrite ^/(.+)/$ /$1 permanent;    }    # removes access to "system" folder, also allows a "System.php" controller    if ($request_uri ~* ^/system)    {        rewrite ^/(.*)$ /index.php?/$1 last;        break;    }    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap    if (!-e $request_filename)    {        rewrite ^/(.*)$ /index.php?/$1 last;        break;    }    # catch all    error_page 404 /index.php;    # use fastcgi for all php files    location ~ /.php$    {        fastcgi_pass unix:/tmp/php-fpm.sock;        fastcgi_index index.php;        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;        include /usr/local/etc/nginx/fastcgi_params;        fastcgi_param HTTPS off;    }    # deny access to apache .htaccess files    location ~ //.ht    {        deny all;    }}

    基本上按照上面的设定,你就可以成功移除index.php 字眼,底下来解释此设定档 www.it165.net

    if ($request_uri ~* index/?$)
    {
    rewrite ^/(.*)/index/?$ /$1 permanent;
    }

    此设定会将/controller/index 转成/controller,因为每一个Controller 预设的method 就是index,所以我想也不用特别显示在URL 上面。permanent 所代表就是301 转向。

    if (!-d $request_filename)
    {
    rewrite ^/(.+)/$ /$1 permanent;
    }

    此设定会将URL 最后的Slash 给拿掉,防止SEO 取得重复信息。

    if ($request_uri ~* ^/system)
    {
    rewrite ^/(.*)$ /index.php?/$1 last;
    break;
    }

    此设定是禁止存取system 目录,此目录是CodeIgniter 核心目录,那Laravel 没有此问题,因为你的虚拟主机一定会指向public 目录,所以也无法存取上层Laravel 核心目录

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

    此设定来到URL 最后判断,如果URL 连结并非是实体档案,则全部导向/index.php?/$1,也就完成了Cleaner URL 动作,最下面设定了PHP-FPM Socket Server,这边就不多说了,设定档给大家参考,如果有任何问题请留言。

    PHP编程

    郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表