IdlleF
IdlleF

Reputation: 88

Nginx + Codeigniter rewrite

after i move my app to Nginx server things just blow up. My site is loaded, but when i try to access some controller return 404 not found. Read alot of articles how to configure nginx.conf but without success.

Here is my nginx.conf

server {
        listen       80;
        server_name  example.com;

        root /var/www/html/travel;
        index index.php;

        # Enable rewrite error log
        error_log /var/log/nginx/travel.error_log debug;
        rewrite_log on;

        # Any HTTP request other than those for assets folder, files folder and robots.txt
        # is treated as a request for your index.php file.

        location / {
            try_files $uri $uri/ /index.php?/$request_uri;
        }
        location ~* ^/(assets|files|robots\.txt) { }

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

Strange thing is that in error.log has no errors.

Here is mine Codeigniter config.php

$config['base_url'] = 'http://example.com';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

Upvotes: 0

Views: 2966

Answers (2)

Klaus
Klaus

Reputation: 2590

Check if this helps. Replace both occurrences of <source_directory>; with your correct value:

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         <source_directory>;
    rewrite_log  on;
    index index.html index.php;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    if ($host ~* ^www\.(.*))
    {
        set $host_without_www $1;
        rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
    }

    # canonicalize codeigniter url end points
    # if your default controller is something other than "welcome" you should change the following
    if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
    {
        rewrite ^(.*)$ / permanent;
    }

    # removes trailing "index" from all controllers
    if ($request_uri ~* index/?$)
    {
        rewrite ^/(.*)/index/?$ /$1 permanent;
    }

    # removes trailing slashes (prevents SEO duplicate content issues)
    if (!-d $request_filename)
    {
        rewrite ^/(.+)/$ /$1 permanent;
    }

    # removes access to "system" folder, also allows a "System.php" controller
    if ($request_uri ~* ^/system)
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }

    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
    if (!-e $request_filename)
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }

    # use fastcgi for all php files
    location ~ \.php$
    {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME <source_directory>$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to apache .htaccess files
    location ~ /\.htaccess
    {
        deny all;
    }

    error_page 404 /404.html;
    location = /40x.html 
    {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html 
    {
    }
}

Upvotes: 2

IdlleF
IdlleF

Reputation: 88

server {
            server_name domain.tld;

            root /var/www/html/travel;
            index index.php;

            # set expiration of assets to MAX for caching
            location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
                    expires max;
                    log_not_found off;
            }

            location / {
                    # Check if a file or directory index file exists, else route it to index.php.
                    try_files $uri $uri/ /index.php;
            }

            location ~* \.php$ {
                    fastcgi_pass 127.0.0.1:9000;
                    include fastcgi.conf;
            }
    }

This is my config with some changes.

Upvotes: 0

Related Questions