kenjis / codeigniter-composer-installer

Installs the offical CodeIgniter 3 with secure folder structure via Composer
MIT License
377 stars 118 forks source link

Nginx configuration #7

Closed samirfor closed 8 years ago

samirfor commented 8 years ago

I can not make it work in nginx.

My tree of files:

$ tree -L 2 /var/www/myproj
.
├── api # codeigniter
│   ├── application
│   ├── bin
│   ├── composer.json
│   ├── composer.lock
│   ├── public
│         ├── .htaccess
│         ├── index.php
│   ├── README.md
│   └── vendor
├── bower.json
├── CHANGELOG.md
├── css
│   └── app.css
├── images
│   ├── favicon-apple-touch-icon-114x114.png
│   ├── favicon-apple-touch-icon-72x72.png
│   ├── favicon-apple-touch-icon.png
│   ├── favicon.png
├── index.html
├── js # angular files
│   ├── app.module.js
│   ├── components
│   ├── configs
│   ├── controllers
│   ├── functions.js
│   └── models
├── lib # bower files
│   ├── angular
│   ├── angular-animate    ...

See my settings:

server {
  listen 80;
  listen [::]:80 ipv6only=on;
  server_name localhost;

  root /var/www/myproj;
  index index.html index.htm index.php;

  location /api/ {
    try_files $uri /api/public/$uri /api/public/$uri/ /api/public/index.php?$request_uri;
  }

  location ~* \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;

    fastcgi_intercept_errors on;
    fastcgi_param  CI_ENV           development;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
    fastcgi_read_timeout 150;
  }
}
fastcgi_param   QUERY_STRING            $query_string;
fastcgi_param   REQUEST_METHOD          $request_method;
fastcgi_param   CONTENT_TYPE            $content_type;
fastcgi_param   CONTENT_LENGTH          $content_length;

fastcgi_param   SCRIPT_FILENAME         $request_filename;
fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
fastcgi_param   REQUEST_URI             $request_uri;
fastcgi_param   DOCUMENT_URI            $document_uri;
fastcgi_param   DOCUMENT_ROOT           $document_root;
fastcgi_param   SERVER_PROTOCOL         $server_protocol;

fastcgi_param   GATEWAY_INTERFACE       CGI/1.1;
fastcgi_param   SERVER_SOFTWARE         nginx/$nginx_version;

fastcgi_param   REMOTE_ADDR             $remote_addr;
fastcgi_param   REMOTE_PORT             $remote_port;
fastcgi_param   SERVER_ADDR             $server_addr;
fastcgi_param   SERVER_PORT             $server_port;
fastcgi_param   SERVER_NAME             $server_name;

fastcgi_param   HTTPS                   $https if_not_empty;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param   REDIRECT_STATUS         200;

I tried some variations on the location /api/ block (rewrite, alias, root, try_files) unsuccessful.

kenjis commented 8 years ago
samirfor commented 8 years ago

Of course. This would work if I my project worked with codeigniter as the main framework. However, I need codeigniter installed in a subfolder and served as /api and angularjs actually is the main framework.

kenjis commented 8 years ago

angularjs actually is the main framework.

It does not matter. In any case, you should put only index.php in your public accessible directory. It is because to avoid direct accesses to the files that does not needed to be accessed. This is a security principle (minimal privilege).

What you need to do is to map http://example.com/api/ to the api/public/ directory in your Nginx config, not to put all files in docroot. See http://nginx.org/en/docs/http/ngx_http_core_module.html#alias

samirfor commented 8 years ago

Yes. You're right. I did work.

See my dirtree:

.
├── bower.json
├── CHANGELOG.md
├── codeigniter
│   ├── application
│   │   ├── cache
│   │   ├── config
│   │   ├── controllers
│   │   ├── core
│   │   ├── helpers
│   │   ├── hooks
│   │   ├── index.html
│   │   ├── language
│   │   ├── libraries
│   │   ├── logs
│   │   ├── models
│   │   ├── third_party
│   │   └── views
│   ├── bin
│   │   ├── check-diff.sh
│   │   ├── install.php
│   │   ├── my-codeigniter.sh
│   │   ├── router.php
│   │   └── server.sh
│   ├── composer.json
│   ├── composer.lock
│   ├── public # document_root
│   │   ├── css # moved
│   │   ├── images # moved
│   │   ├── index.html # moved
│   │   ├── index.php
│   │   ├── js # moved
│   │   ├── lib # moved
│   │   ├── scripts # moved
│   │   └── views # moved
│   ├── README.md
│   └── vendor # composer
│       ├── autoload.php
│       ├── codeigniter
│       ├── composer
│       └── mikey179
└── README.md

my conf:

server {
  listen 80;
  listen [::]:80 ipv6only=on;
  #server_name localhost

  root /var/www/myproj/codeigniter/public;
  index index.html index.php index.htm;

  location / {
    try_files $uri $uri/ /index.php?$request_uri;
  }

  location ~* \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;

    fastcgi_intercept_errors on;
    fastcgi_param  CI_ENV           development;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
    fastcgi_read_timeout 150;
  }
}
kenjis commented 8 years ago

Good to hear!

samirfor commented 8 years ago

Thanks for your superb help.