Codeigniter 的 nginx 重写规则无法正常工作

nginx rewrite rules for Codeigniter are not working correctly

我第一次尝试在 MAMP 中使用 nginx 服务器而不是 Codeigniter 的 apache 服务器。我将我的 apache htaccess 重写规则转换为 nginx 重写规则。但它显示的是我文件中的 index.php 文件代码,而不是我的网站。这是我的 apache htaccess 规则,

DirectoryIndex index.php
RewriteEngine on
RewriteCond  !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/ [L,QSA]

这是我的 nginx 配置文件,

http {
include                  mime.types;
default_type             text/html;
gzip                     on;
gzip_types               text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;

sendfile                 on;

server {
    listen               80 default_server;

    # MAMP DOCUMENT_ROOT !! Don't remove this line !!
    root "C:/MAMP/htdocs/aia_inventory/";

    access_log  C:/MAMP/logs/nginx_access.log;

    error_log  C:/MAMP/logs/nginx_error.log;

    index index.html index.htm index.php;
    #autoindex on;
    #server_name localhost;
    location ~/ {
        #root C:/MAMP/htdocs/aia_inventory/;
        #index index.html index.php;
        try_files $uri $uri/ /index.php/$request_uri;

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

    location ~* ^/(assets|files|robots\.txt) { }

    location ~* /MAMP(.*)$ {
    root             C:/MAMP/bin;
        index            index.php;

        location ~ \.php$ {
            try_files        $uri =404;
            fastcgi_pass     127.0.0.1:9100;
            fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include          fastcgi_params;
        }
    }

    location ~* /phpMyAdmin(.*)$ {
    root             C:/MAMP/bin;
        index            index.php;

        location ~ \.php$ {
            try_files        $uri =404;
            fastcgi_pass     127.0.0.1:9100;
            fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include          fastcgi_params;
        }
    }

    location ~* /phpLiteAdmin(.*)$ {
    root             C:/MAMP/bin;
        index            phpliteadmin.php index.php;

        location ~ \.php$ {
            try_files        $uri =404;
            fastcgi_pass     127.0.0.1:9100;
            fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include          fastcgi_params;
        }
    }

    location ~* /SQLiteManager(.*)$ {
    root             C:/MAMP/bin;
        index            index.php;

        location ~ \.php$ {
            try_files        $uri =404;
            fastcgi_pass     127.0.0.1:9100;
            fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include          fastcgi_params;
        }
    }

    #location /icons {
    #   alias /Applications/MAMP/Library/icons;
    #   autoindex on;
    #}

    #location /favicon.ico {
    #   alias /Applications/MAMP/bin/favicon.ico;
    #    # log_not_found off;
    #    # access_log off;
    #}

    location ~ \.php$ {
        try_files        $uri =404;
        fastcgi_pass     127.0.0.1:9100;
        fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include          fastcgi_params;
    }

    #location ~ /\. {
    #   deny all;
    #}

    # location ~* \.(gif|jpg|png|pdf)$ {
    #   expires          30d;
    # }

    # location = /robots.txt {
    #   allow all;
    #   log_not_found off;
    #   access_log off;
    # }

    # location ~* \.(txt|log)$ {
    #   allow 127.0.0.1;
    #   deny all;
    # }

    # location ~ \..*/.*\.php$ {
    #   return 403;
    # }

    #location /nginx_status {
    #   stub_status      on;
    #   access_log       off;
    #   allow            127.0.0.1;
    #   deny             all;
    #}
}
}

我不明白我错过了什么。我的规则适用于 apache 服务器。但是在 nginx 服务器上,它只显示 index.php 文件原始代码。提前致谢。

试试这个

location ~ / {
        root C:/MAMP/htdocs/inventory/;
        index index.html index.php;
        try_files $uri $uri/ /index.php/$request_uri;

        if (!-e $request_filename){
             rewrite ^(.*)$ /index.php/ break;
        }
    }
location ~* ^/(assets|files|robots\.txt) { }
    try_files $uri $uri/ /index.php/$request_uri;
  …
  location ~ \.php$ {

您遇到的问题是您使用的 http://nginx.org/r/try_files to redirect the requests to a URL that starts with /index.php//, whereas your .php$ http://nginx.org/r/location 处理程序并不期望在 .php 部分之后的 $uri 中有任何内容。

解决方案可能是将 $ 替换为 (/|$),例如 location ~ \.php(/|$).

使用 .htaccess 不太安全,最好在 sites-enable 或 conf.d 目录中的 .conf 文件中设置。我建议你这样写:

    sendfile on;
    send_timeout 300s;
    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.php?$args;
            # try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;

            # With php7.0-cgi alone: // in your case is 9100
            fastcgi_pass 127.0.0.1: 9100;
            # With php7.0-fpm:
            # fastcgi_pass unix:/run/php/php7.1-fpm.sock;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
            deny all;
    }

您的第一个位置块定义如下:

location ~/ {

所以这是包含 / 的请求的正则表达式匹配。你可能会得到很多......

Nginx 位置处理首先评估所有前缀指定的位置并存储最佳匹配,然后按照配置文件中出现的顺序评估正则表达式。如果找到正则表达式匹配项,它会立即停止进一步查找并选择该位置块来处理请求。

您的第一个位置正则表达式将匹配您收到的每个请求,因此您基本上可以删除其余配置,这没有任何区别,每个请求都将在您的第一个块中结束。

您的第一个块没有 fast_cgi 指令将 php 处理传递给 php fpm,因此 Nginx 找到该文件并将其作为文本提供。

抱歉回答晚了。这实际上是一个非常愚蠢的错误。我的控制器页面名称是小字符。这就是它不起作用的原因。我的配置没问题。控制器页面的第一个字母应该是大写字符。例如,我的控制器名称是 Home。所以我的 php 文件名必须是 Home.php 而不是 home.php。它可以在本地 ngincx 服务器上运行,但不能在实时服务器上运行。