Codeigniter setup with nginx for hmvc stucture - php

Can anyone done with Codeigniter setup with nginx for hmvc stucture?
Please help me on this, I try to setup up codeigniter HMVC structure on nginx. But fails to many times. Please suggest some wayt to configure it.
I am using php7.0-fpm.
My nginx config file is
server {
listen 80;
root /var/www/html/salsetrack;
index index.html index.htm index.php;
server_name local.sales-track.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
#Include Nginx’s fastcgi configuration
include /etc/nginx/fastcgi.conf;
#Look for the FastCGI Process Manager at this location
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location #handler {
rewrite / /index.php;
}}

You can refer https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/ for nginx configuration for codeigniter.

Related

Nginx try_files prompt for download PHP

I'm setting up an Nginx + PHP web server on AWS Linux 2. It is a fresh install with nginx and PHP7.4 installed. Below is the virtual host config file in nginx.
I need to redirect all the traffic to index.php because it is a Single Page App.
When I go to www.xxx.com/index.php, the PHP page renders fine (so PHP is definitely running).
When I go to www.xxx.com/login/, the browser prompts for download of the index.php file instead of executing it.
Can anyone please help? (I've tried to clear my browser cache).
/etc/nginx/conf.d/myapp.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/vhosts/app.userback.io/frontend/;
index index.php index.html index.htm;
server_name www.myapp.com;
location / {
try_files $uri $uri/ /index.php =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass php-fpm;
}
}
Can you try:
location ~ \.php$ {
try_files $uri /404.html;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;// change it if not valid
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I've found the answer here: NGINX try_files does not pass to PHP.
It turns out it is the order of the location plus removing the 404 to make the redirect internal.

How to add rewrite on existing Nginx config file

I have an existing Nginx config file that I've configured to work with a docker container that runs php. Below is the nginx config file:
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
Now I wanted to test a certain project in the nginx configuration, the project requires that I use its own configuration of nginx
# nginx configuration
autoindex off;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?path=$1;
}
}
I am getting difficulties on adding the new configurations onto the already existing Nginx file that connects to the PHP container. I need some help on how I can add the new configurations without compromising the old Nginx file because the routes wont be redirected to the php container if I use the Nginx file that comes with the project.

Laravel's NotFoundHttpException won't dissapear in Nginx

I'm using Nginx with Laravel and I keep getting a NotFoundHttpException error.
Nginx won't log anything. I've also tried to add die('Test'); inside my public/index.php file, but that won't be dumped as well.
My Nginx server block looks like this:
server {
listen 0.0.0.0:80;
server_name test.docker.dev;
root /usr/share/nginx/html/docs/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
access_log /var/log/nginx/default_access.log;
error_log /var/log/nginx/default_error.log;
}

Executing php scripts under a nginx configuration

I have the following nginx server block in its configuration
server {
listen 80;
server_name mydomain.com;
location /deploy {
alias /home/somedir/deploy/;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I want to execute a php file at the url mydomain.com/deploy/erp/index.php or mydomain.com/deploy/erp
The above thing downloads the php file instead of executing it. I googled and found solutions that asks you to define a www directory or something. I just need to execute the file in a specific directory at specific url. What are my options?
You haven't told nginx what to do with .php files. Assuming you're using php-fpm, you'll need something like this:
server {
listen 80;
server_name mydomain.com;
root /home/somedir;
location / {
try_files $uri $uri/ =404;
}
location /deploy {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
}
}

NGINX shows script code instead of execute

I have a troble with NGINX that makes me insane. I have Ubuntu 12.04.3 LTS on VirtualBox. I installed NGINX-MYSQL-PHP following these instructions:
install php5-fpm;
set cgi.fix_pathinfo=0 in /etc/php5/fpm/php.ini;
set listen = var/run/php5-fpm.sock in /etc/php5/fpm/pool.d/www.conf
Then I set in /etc/nginx/sites-available/default:
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Both localhost and info.php are executed properly. Now i want to set up a local website and try to make some redirect. So I created a new folder in /usr/share/nginx/www called mywebsite, put a index.php inside with a echo instruction, a php script i make redirect to, added a new config file in sites-available and a link to that in sites-enabled, and disable application/octet-stream in nginx.conf in order to avoid to downloading the script file. So i set my new config file as following:
server {
listen 80;
root /usr/share/nginx/www/mysite;
index index.php index.html index.htm;
server_name www.mysite.com;
location / {
try_files $uri $uri/ /index.html;
if (!-e $request_filename){
rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/$ /senseddata.php break;
rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/lasthour/$ /senseddata.php break;
rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/daily/$ /senseddata.php break;
}
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.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;
try_files $uri =404;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
So what happened after all? Index.php is executed properly. Rewrite is done well but the script is not executed. I only see the entire code i wrote displayed in the browser. What's the problem? Thanks in advance.
Also check the opening tags in your PHP-files. If you use <? instead of <?php and the short tag is not available, the PHP-code will not be parsed and you will see the source.
Try add fastcgi_split_path_info ^(.+\.php)(/.+)$; to location ~ \.php$ and restart nginx.
Example from my config
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Categories