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 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线