0

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?

vishgarg
  • 445
  • 1
  • 9
  • 24
  • Have you configured your socket? – edlee Nov 28 '16 at 17:00
  • I'm not sure I understand your question. Have you configured your socket correctly, can you reach a PHP info page? What is not working the sub-directory or the Codeignite site? – edlee Nov 28 '16 at 17:34

2 Answers2

2

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.

Community
  • 1
  • 1
edlee
  • 665
  • 1
  • 7
  • 20
  • Thanks @edlee Actually, the site is already working fine but when not the codeigniter site. When I'm logging in the website shows the root page of domain. eg. example.com/123/ after login the url becomes example.com/123/login/dashboard/ but it shows the content of example.com/ – vishgarg Nov 29 '16 at 12:07
  • I tried your configuration but I'm still getting the same content. – vishgarg Nov 29 '16 at 12:07
  • N.P. Just to clarify - so after login it is redirecting you to the root of your web directory? Therefore, you would like example.com/ to be example.com/123/ ? – edlee Nov 29 '16 at 12:24
  • Thanks brother for helping me :) Yes, I want the /123 directory to be accessed as example.com/123/ after login it automatically shows the root directory contents and not the directory /123/ content – vishgarg Nov 29 '16 at 15:51
  • I've got it working. :) Thanks @edlee Brother for your incredible help and actually I tried your solution to make it working. :) – vishgarg Nov 29 '16 at 16:21
0

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;
        }
vishgarg
  • 445
  • 1
  • 9
  • 24