简体   繁体   中英

nginx rewrite rules for Codeigniter are not working correctly

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.

try this

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/$1 break;
        }
    }
location ~* ^/(assets|files|robots\.txt) { }
  try_files $uri $uri/ /index.php/$request_uri; … location ~ \\.php$ { 

The issue you're having is that you're using 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 handler doesn't expect for there to anything at all within the $uri after the .php part.

The solution may be to replace $ with (/|$) , for example, as in, location ~ \\.php(/|$) .

Using .htaccess isn't much secure, prefer to set in the sites-enable or conf.d directory in a .conf file. I recommend you to put like this:

    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;
    }

Your very first location block is defined like this:

location ~/ {

So that's a regex match for requests containing / . You'll probably get a lot of those...

Nginx location processing evaluates all prefix specified locations first and stores the best match, then evaluates regex in order of appearance in your config file. If it finds a regex match then it immediately stops looking any further and selects that location block to process the request.

Your first location regex will match every request you get, so you could basically delete the rest of your config and it would make no difference, every request will end up in your first block.

Your first block has no fast_cgi directive to pass php processing to php fpm, so Nginx finds the file and just servers it as text.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM