简体   繁体   中英

How to enable URL rewrite in Ubuntu, nGinx and codeigniter?

I've setup ubuntu, php5.6, nGinx on a server and I've deployed few projects under my website configured. One project I need to deploy under a directory is developed in codeigniter and I've configured the nGinx configuration as follows:

 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 unix:/run/php/php5.6-fpm.sock;
                include fastcgi.conf;
                fastcgi_split_path_info ^(.+\.php)(.*)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }

The URL don't say 404 now but it is showing the home page of my site after URL rewrite. How can I fix this?

If I understood your question correctly.

One approach is to run PHP globally across the root of your web directory. Once you have it running you can then specify what files each location will accept. So if you need to isolate PHP to just the sub-directory then you can change the files allowed for the directory.

index index.html index.htm

An example Nginx config with a sub-directory:

server {
   listen   80;
   server_name mysite.com www.mysite.com;

   root   /path/to/your/website.com/;

   index index.html index.htm

   error_log log/error.log;

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

   # main codeigniter rewrite rule or sub-directory

   location /codeigniter {
       alias /path/to/your/website.com/codeigniter/;
       try_files $uri $uri/ /index.php;
   }

   # php parsing 
   location ~ .php$ {
        root           /path/to/your/website.com/;
        try_files $uri =404;
        fastcgi_pass   unix:/tmp/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 256 4k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
    }
}

I'm not a Codeigniter user, so I can't share a config for the PHP. But the location will need to be specified for a sub-directory. This Stack post will show you how to do this here .

Another approach would be to isolate PHP to the sub-directory. Similar to the above, only change this line

# php parsing 
location ~ /codeigniter/.+\.php$

This post has a full example for this approach .

Actually, I've fixed the issue. The problem was to change the fastcgi_param and it worked fine. Thanks @edlee

location /6 {
                alias /var/www/html/6/;
                try_files $uri $uri/ /6/index.php;
        }



        location ~* \.php$ {
                fastcgi_pass unix:/run/php/php5.6-fpm.sock;
                root /var/www/html/;
                try_files $uri =404;
                fastcgi_index  index.php;
                fastcgi_param   SCRIPT_FILENAME $request_filename;
                include fastcgi_params;
                fastcgi_buffer_size 128k;
                fastcgi_buffers 256 4k;
                fastcgi_busy_buffers_size 256k;
                fastcgi_temp_file_write_size 256k;
        }

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