douhong9210 2018-07-30 12:02
浏览 162
已采纳

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

I am trying to use nginx server in MAMP instead of apache server for Codeigniter for the first time. I converted my apache htaccess rewrite rules to nginx rewrite rules. But it is showing index.php file code from my file not my website. Here is my apache htaccess rules,

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

And here is my nginx configuration file,

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?/$1 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;
    #}
}
}

I am not understanding where am I missing. My rules are working on apache server. But on nginx server, it shows only index.php file raw code. Thanks in advance.

  • 写回答

5条回答 默认 最新

  • dr5779 2019-01-06 19:45
    关注

    Sorry for the late answer. It was actually very silly mistake. My controller page name was in small character. This is why it was not working. My configuration is okay. The first letter of the controller page should be in capital character. For example, my controller name is Home. So my php file name must be Home.php not home.php. It will work on local ngincx server but not in live server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog