【问题标题】:How to convert .htacess(apache) to Nginx如何将 .htaccess(apache) 转换为 Nginx
【发布时间】:2020-09-22 04:46:27
【问题描述】:

我尝试在site 上进行转换,但我没有成功。 我需要为 Nginx 代码转换 .htacess 代码。 我的 .htacess 代码:

<IfModule mod_rewrite.c>
    RewriteEngine   On
    RewriteCond     %{REQUEST_FILENAME} !-f
    RewriteCond     %{REQUEST_FILENAME} !-d

    # Rewrite all other URLs to index.php/URL
    RewriteRule     ^(.*)$  index.php?url=$1    [PT,L,QSA]

    ErrorDocument 400 error.html?err=404
    ErrorDocument 401 error.html?err=401
    ErrorDocument 403 error.html?err=403
    ErrorDocument 404 error.html?err=404
    ErrorDocument 500 error.html?err=500
    ErrorDocument 503 error.html?err=503
</IfModule>

我的 nginx 代码是:

server {
        listen 80;
        server_name mysite.mysite.com.*;
        index index.php;
        root /home/mysite1/public_html/mysite/webapp/;
        client_max_body_size 5M;


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

        location / {                    
                             try_files $uri $uri/ index.php?url=$uri&$query_string;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php-fpm/www.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                include /etc/nginx/fastcgi.conf;

       }
}

我的php代码是:

<?php
define('APP_PATH', ROOT . DS . 'app' . DS);
    define('CFG_PATH', ROOT . DS . 'config' . DS);
    define('LIB_PATH', __DIR__ . DS . 'lib' . DS);
    define('RES_PATH', ROOT . DS . 'resources' . DS);
    define('TMP_PATH', ROOT . DS . 'tmp' . DS);
    define('VND_PATH', ROOT . DS . 'vendor' . DS);

    $url = isset($_GET['url']) ? $_GET['url'] : '';

    require_once(VND_PATH . 'autoload.php');
    require_once(CFG_PATH . 'config.php');
    require_once(CFG_PATH . 'routing.php');
    require_once(__DIR__ . DS . 'doctrine.php');
    require_once(__DIR__ . DS . 'autoloader.php');

    Cache::createDirs();

    session_start();

    $dfgfw = new DFGFW();

    $dfgfw->gzipOutput() || ob_start("ob_gzhandler");
    $dfgfw->setReporting();
    $dfgfw->removeMagicQuotes();
    $dfgfw->exec();

访问该网站的作品。 mysite.mysite.com

路线系统不工作。 mysite.mysite.com/history

我的 Nginx 出了什么问题,如何使用 Codeigniter 内部路由系统?

【问题讨论】:

  • 完全摆脱if 块,您的location 块使用try 命令执行此操作,该命令说“尝试将此请求作为文件,然后是目录,然后传递它到index.php"

标签: php apache .htaccess codeigniter nginx


【解决方案1】:

试试this converter。这将产生正确的try_files 指令(NGINX 前端模式 URL 重写的基石):

server {
    server_name example.com;
    location / {
        try_files $uri $uri/ /index.php?url=$uri$is_args$args;
    }
}

【讨论】:

  • 现在我可以在$url = isset($_GET['url']) ? $_GET['url'] : ''; 上获取参数,但我在 mysite.mysite.com/history 中得到状态码 404。
  • 您仍然需要location ~ \.php$ { ... 将 PHP 请求传递给 PHP-FPM。
  • 是的,我在这里做:location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php-fpm/www.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; include /etc/nginx/fastcgi.conf;我有一个新问题?
猜你喜欢
  • 2015-05-21
  • 2011-11-25
  • 2021-11-26
  • 2020-12-23
  • 2021-06-30
  • 1970-01-01
  • 2012-02-25
  • 1970-01-01
  • 2016-08-31
相关资源
最近更新 更多