CodeIgniter, NGINX inside folder not working - php

I have a stock CI application that runs on NGINX. There is no rewriting (I tried for 16 hours and couldn't not get it to work. It's either 500, 404, 403, or input file not specified), php files are passed to php-fpm.
Here's my default file under available-sites (Ubuntu 12.04, tried everything on CentOS and nothing worked):
server {
listen 80;
root /usr/share/nginx/www/;
index index.php index.html index.htm;
server_name fish-in-a-bowl.net;
location / {
try_files $uri $uri/ /index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www/cloud;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
And config.php:
$config['base_url'] = 'http://fish-in-a-bowl.net/zz/';
$config['index_page'] = 'index.php';
//$config['index_page'] = '';
//$config['uri_protocol'] = 'PATH_INFO';
//$config['uri_protocol'] = 'QUERY_STRING';
//$config['uri_protocol'] = 'REQUEST_URI';
//$config['uri_protocol'] = 'DOCUMENT_URI';
//$config['uri_protocol'] = 'ORIG_PATH_INFO';
$config['uri_protocol'] = 'AUTO';
I've tried all the options available, but none worked. This node is on Digital Ocean, they have tutorials for setting up CI with Apache but not NGINX.
The default controller works, but when accessed via http://fish-in-a-bowl.net/zz/index.php/welcome I get No input file specified.
Help? This is SO strange.
SCREENSHOT >

In your nginx service config change:
location / {
try_files $uri $uri/ /index.html;
}
To
location / {
try_files $uri $uri/ /index.php;
}
In your CI config.php set:
$config['index_page'] = '';
$config['uri_protocol'] = "REQUEST_URI";

try this in your nginx setting
# enforce NO www
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;
}

Try the following, works a dream for me. Don't forget to change the fastcgi_pass, and backup your current config before you try it.
server
{
listen Server IP:80;
server_name domain.name;
access_log /var/log/nginx/access.log;
root /path/to/www;
index index.php index.html index.htm;
# enforce www (exclude certain subdomains)
# if ($host !~* ^(www|subdomain))
# {
# rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;
# }
# enforce NO www
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;
}
# catch all
error_page 404 /index.php;
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /path/to/www/$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}
}

Just add a new location block for CI
location ~ /zz(?<myuri>.*) {
root /path/to/root/zz;
try_files $myuri $myuri/ /index.php$myuri$is_args$query_string;
}
That should do it all, test it and tell me how it goes.

try this
location ~ \.php($|/.+) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}

location /atri/ {
alias /var/www/html/atri/;
try_files $uri $uri/ /atri/index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}

Related

Nginx vhost prestashop on sub url

I have 2 projects on same domain -- domain.ltd
On direct domain, i have a symfony project -- this part is OK.
But for my sake, i try to let the old prestashop site on sub url, like domain.ltd/old
So my vhost is :
# Redirect all HTTP traffic
server {
listen 80;
server_name www.domain.ltd domain.ltd;
root /home/user/projects/old.domain.ltd/;
location = /{
# On redirige toutes les requĂȘtes vers HTTPS
rewrite ^ https://www.domain.ltd$request_uri? permanent;
}
location /old {
root /home/user/projects/old.domain.ltd/;
index index.php;
rewrite ^/old/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
rewrite ^/old/([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2$3.jpg last;
rewrite ^/old/([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3$4.jpg last;
rewrite ^/old/([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4$5.jpg last;
rewrite ^/old/([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg last;
rewrite ^/old/([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg last;
rewrite ^/old/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/old/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/old/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg last;
rewrite ^/old/c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2$3.jpg last;
rewrite ^/old/c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
rewrite ^/old/images_ie/?([^/]+)\.(jpe?g|png|gif)$ /js/jquery/plugins/fancybox/images/$1.$2 last;
try_files $uri $uri/ /index.php$is_args$args;
error_page 404 /old/index.php?controller=404;
location ~ ^/old/\.php$ {
try_files $uri =404;
root /home/user/projects/old.domain.ltd/;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm-user.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
access_log /var/log/nginx/domain.ltd.old.access.log;
error_log /var/log/nginx/domain.ltd.old.error.log;
}
access_log /var/log/nginx/domain.ltd.old.access.log;
error_log /var/log/nginx/domain.ltd.old.error.log;
}
# HTTPS request on symfony
server {
listen 443 ssl;
server_name www.domain.ltd;
# this part works fine with my symfony project
}
I don't understand why my config is not working. When i go on back-office page, my web browser download the file of index.php.
If anyone have an idea ??

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';
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
{
}
}
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.

Nginx rewrite rule for CodeIgniter

Here is the rule in English:
Any HTTP request other than those for index.php, assets folder, files folder and robots.txt is treated as a request for your index.php file.
I have an .htaccess file that works correctly on Apache server:
RewriteCond $1 !^(index\.php|assets|files|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Some correct results for this rule:
example.com = example.com/index.php
example.com/index.php/welcome = example.com/welcome
example.com/assets/css/main.css != example.com/index.php/assets/css/main.css
I tried some tools to convert from htaccess rule to nginx rule but all were incorrect.
Via http://winginx.com/htaccess (missing the exception rules for assets folder...):
location / { rewrite ^(.*)$ /index.php/$1 break; }
Via http://www.anilcetin.com/convert-apache-htaccess-to-nginx/ (error in $1 value):
if ($1 !~ "^(index.php|assets|files|robots.txt)"){
set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
rewrite ^/(.*)$ /index.php/$1 last;
}
How can I fix this? It's really hard to debug the rule.
Here is my nginx config so far:
server {
listen 80;
server_name www.example.com example.com;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
You can add this to your config:
location ~* ^/(assets|files|robots\.txt) { }
This will work correctly with your location / rule.
Your config also need to add root document and default index file.
...
root /ftp/wardrobe;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
location ~* ^/(assets|files|robots\.txt) { }
...
You want to do this instead:
if ($request_uri !~ ^/(index\.php|assets|files|robots\.txt)) {
rewrite ^/(.*)$ /index.php/$1 last;
}
$request_uri is for the original URI request by the client. If you want the URI request AFTER other Nginx rewrite rules have processed it then you would use $uri. However, for what you are trying to do the prior would be the one you would want.
Also, you need to escape special regular expression characters like . by using a backslash.
Please try this. It works for me.
server {
server_name domain.tld;
root /var/www/codeignitor;
index index.html 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$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Page shown properly, but Nginx logs shows "File not found"

I'm using Nginx and Codeigniter alongside php5-fpm. Everything "seems" to work just fine, pages are shown and everything looks great. Ofcource this isn't the reason I'm asking this question here, I actually have a problem.
The problem I'm facing is that 404 error is thrown with page, even though the page is rendered properly, I'm stilling getting 404's(in the logs).
The reason why I'm getting 404's (after looking at Nginx's error logs), is that Nginx can't open the file I'm requesting, because Nginx tries to open the PHP files directly, instead of referring to the controller/method and unfortunately Codeigniter works that way.
For example:
requesting http://website.com/<controller>/<function>/<other_params> results in 404 in Nginx logs, the reason behind this is that open() can't open the specified directory because it doesn't exists, instead of referring to the controller/method.
Some important logs:
Nginx error log:
[error] 4172#0: *482 open() "/var/www/<domain>/public/site/section/main" failed
(2: No such file or directory), client: <client_ip>, server: <domain>, request: "GET
/site/section/main HTTP/1.1", host: "<domain>"
as I said before, Nginx is trying to access the file directly, instead of making Codeigniter deal with it.
my sites-enabled/ci configurations:
server
{
server_name <domain> *.<domain>;
access_log /var/www/<domain>/access.log;
error_log /var/www/<domain>/error.log;
root /var/www/<domain>/public;
index index.php index.html index.htm;
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
# enforce www (exclude certain subdomains)
# if ($host !~* ^(www|subdomain))
# {
# rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;
# }
# enforce NO www
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 ~* ^(/site(/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|application))
{
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;
#}
# catch all
error_page 404 /index.php;
# use fastcgi for all php files
location ~ \.php($|/)
{
#if (!-e $request_filename) {
# return 404;
# }
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass php5-fpm-sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# location / {
# try_files $uri $uri/ #codeigniter;
#}
# location #codeigniter {
# rewrite ^(.*) /index.php?$1 last;
# }
# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}
}
So, what's the reason behind this misunderstanding between Nginx and Codeigniter?
Thanks in advance.
It seems like there's no rule in your config that tells nginx to try sending requests to php when the file is not found. There's a commented out block with try_files and #codeigniter that almost look like it. In theory this is what you want nginx do:
Check if the url exists, if does serve it.
Send everything else to codeigniter and let it sort out.
For this, these two blocks should be enough:
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
# for security, see http://forum.nginx.org/read.php?2,88845,page=3
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
The other if guarded block shouldn't break with these in place.

Setting up Laravel with Nginx

I'm attempting to set up the Laravel PHP Framework to work with Nginx. Here is my directory structure:
/project
/application
/laravel
/public
index.php
/legacy
/index.php
/stylesheets
default.css
Basically what I have is a standard Laravel download w/ a legacy folder thrown in which holds all of the files from my non-MVC project.
I need Nginx to first check if the requested page/file exists inside of legacy, if it does then I want to use that. Otherwise, I want to fall back to Laravel's index.php file which is located in project/public/.
I'm no expert when it comes to Nginx configurations so any help that you can provide would be most appreciated.
server {
server_name .laravel.dev;
root /home/tamer/code/laravel/public;
index index.php index.html;
#browse folders if no index file
autoindex on;
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# enforce NO www
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 ~* ^(/lobby(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# 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;
}
# catch all
error_page 404 /index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/tmp/php.socket;
fastcgi_index index.php;
#include fastcgi_params;
include /home/tamer/code/nginx/fastcgi_params;
}
access_log /home/tamer/code/laravel/storage/logs.access.log;
error_log /home/tamer/code/laravel/storage/logs.error.log;
}

Categories