Nginx + CodeIgniter Posting To Controller 500 Error - codeigniter

I previously had this website on an Ubuntu/Apache host which has now been moved to a Debian/Nginx host. When I moved to Nginx it seems the form which used to work no longer works
How the form works is that it's hosted on a Wordpress website which is located at www.mycompany.com. It then posts the data to a controller which is hosted on the same server but using virtual hosts to site1.mycompany.com.
When I post the data, the form just hangs, looking at the console shows
POST https://site1.mycompany.com/request_form/submit 500 (Internal Server Error)
The CodeIgniter controller is called request_form and the submit function starts like this (to give you an idea)
public function submit()
{
$return = array('error' => false, 'msg' => '', 'error_data' => array());
$validation_rules = array();
//$this->form_validation->set_rules('serial_no', 'Outdoor Serial No', 'required');
$this->form_validation->set_rules('enquiry_type', 'Enquiry Type', 'required');
$this->form_validation->set_rules('txt_email', 'Email', 'required');
if(!empty($this->input->post('txt_email')))
{
$this->form_validation->set_rules('txt_email','email','valid_email|callback_email_exist');
}
$this->form_validation->set_rules('txt_phone', 'Phone', 'required');
$this->form_validation->set_rules('txt_address', 'Address', 'required|callback_check_address_for_state');
$this->form_validation->set_message('check_address_for_state','Address is not valid!');
My Nginx Config for site1.mycompany.com is
server {
server_name site1.mycompany.com;
root /var/www/html/site1.mycompany.com;
index index.php index.html index.htm;
autoindex on;
client_max_body_size 50M;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location / {
try_files $uri $uri/ /index.php;
}
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
There are no error logs in either /var/logs/nginx nor under CodeIgniter even when full logging is enabled. I have tried a variety of changes to the Nginx config with no avail.

Just try with these basic settings. (for easy debug just add echo "works"; die; in the home controller and check.)
In Ngnix
server {
server_name site1.mycompany.com;
root /var/www/html/site1.mycompany.com;
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$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
}
In Config.php
$config['base_url'] = "http://site1.mycompany.com/";
$config['index_page'] = "";
$config['uri_protocol'] = "REQUEST_URI";

Related

Nginx configuration with Laravel API + vuejs

I have the application with frontend on vuejs which works through the laravel api on backend. And I use Laravel Passport for authorization.
When I start the application through the integrated laravel web server (artisan serve) on localhost:8000 it works good without any errors.
But when I moved it to my local web server I got some problems.
When I try to do the post request
axios.post('http://project.local/oauth/token', data)
I get the http error 405 (Method Not Allowed).
I excluded all routes from CSRF-token, added CORS headers and anyway it doesnt work. As I said on localhost:8000 it works well.
I didn't find any information about this problem and couldn't do it by myself. Does anyone know anything about it? Thanks in advance.
My nginx config
server {
listen 80;
root /home/me/project/frontend/dist;
index index.html index.php;
server_name project.local;
access_log off;
error_log /home/me/project/backend/error.log notice;
location / {
index index.html;
try_files $uri $uri/ /index.html?$args;
}
location /api/v1 {
root /home/me/project/backend/public;
rewrite ^/api/v1/(.*)$ /$1 break;
try_files $uri $uri/ /index.php?$query_string;
}
location /oauth/token {
root /home/me/project/backend/public;
rewrite ^/oauth/token/(.*)$ /$1 break;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
set $newurl $request_uri;
if ($newurl ~ ^/api/v1(.*)$) {
set $newurl $1;
root /home/me/project/backend/public;
}
if ($newurl ~ ^/oauth/token(.*)$) {
set $newurl $1;
root /home/me/project/backend/public;
}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param REQUEST_URI $newurl;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
Maybe it's a cache! I just solved this problem and it turned out to be a cache cloudflare
cached all files as index.html

404 errors with controller files using CodeIgniter 3 and NGINX

I have a server running NGINX 1.10.0 and installed CodeIgniter 3 in a subdirectory /ci/. The welcome page renders when I go to the subdirectory but any other controller gives a 404 error.
Here is the code in /etc/nginx/sites-available/default configuration file:
server {
server_name mydomain.org;
root /home/username/mydomain/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
expires 15d;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/username/mydomain/ci/index.php;
fastcgi_param REQUEST_URI $request_uri;
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_pass unix:/run/php/php7.0-fpm.sock;;
}
}
I created a Hello World Controller file (Hello.php)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Hello extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index(){
$this->load->view('helloworld');
}
}
In my config.php file, I have the following:
$config['base_url'] = '';
$config['index_page'] = 'index.php';
$config['uri_protocol'] = 'REQUEST_URI';
I have tried the approaches here:
NGINX CodeIgniter Recipe: https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/
CodeIgniter NGINX Rewrite Rules:
http://www.farinspace.com/codeigniter-nginx-rewrite-rules/
Codeigniter nginx 404 error:
Codeigniter nginx 404 error
Most of everything that I've read is based on PHP5 or an older version of CodeIgniter.
Use this config in Nginx (Source):
server {
server_name domain.tld;
root /your/web/root/path
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$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
}
Also you need to make a change in your php-fpm config. Edit this file:
/etc/php/7.0/fpm/pool.d/www.conf
Find this line:
listen = /run/php/php7.0-fpm.sock
Change to this:
listen = 127.0.0.1:9000
This changes should solve your issue.
Here is the final working versions of the default, www.conf and config files. Another issue I found out I was having is that the controller and file name have to be the same capitalization. The controller class Hello needs to have the filename be Hello. Some of my controllers were not the same capitalization, this however was not the sole cause of the 404 as I tried that before fixing the default file.
/etc/nginx/sites-available/default:
server {
server_name example.org;
root /home/username/example/;
index index.php index.html index.htm;
location / {
# Check if a file or directory index file exists, else route it to index.php
try_files $uri $uri/ /index.php;
# Handle PHP files
location ~* \.php$ {
root /home/username/example/ci;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $uri $uri/ ci/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;
}
}
/etc/php/7.0/fpm/pool.d/www.conf:
listen = 127.0.0.1:9000
Finally, in my config file:
$config['base_url'] = 'http://example.org/ci/';
This configuration changes any CI links from
http://example.org/ci/Hello
to
http://example.org/Hello

Laravel nginx default file

I try to configure my laravel installation on a digital ocean instance with nginx running.
The default config looks like this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/mfserver/public;
index index.php index.html index.htm;
server_name IPADDRESS;
location / {
try_files $uri $uri/ /index.php$is_args&args =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/mfserver/public;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php$is_args&args =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;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
}
The problem is, that when I call a route like /v1/aeds I get an error 404. Is the config not setup properly?
The route file:
Route::group(['domain' => 'SERVERIP', 'namespace' => 'API'], function() {
Route::group(['prefix' => 'v1', 'namespace' => 'v1'], function() {
// AED ROUTES
Route::get('/aeds', 'AED\APIAEDController#index');
Route::post('/aeds', 'AED\APIAEDController#store');
Route::get('/aeds/{aeds}', 'AED\APIAEDController#show');
}
Try replacing this:
location / {
try_files $uri $uri/ /index.php$is_args&args =404;
}
With this:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;

nginx / php-fpm virtual host for magento 2

i am struggling to get my virtual host working with magento2.
Can anyone help?
server {
listen 80;
server_name local.magento2.com;
root /home/www/magento2/;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
index index.php;
try_files $uri $uri/ #handler; ## If missing pass the URI to Magento's front handler
expires off; ## Assume all files are cachable
}
location #handler { ## Magento uses a common front handler
rewrite / /index.php;
}
location /pub/static {
rewrite ^/pub/static/(.*)$ /pub/static.php?resource=$1? last;
}
# Foward php request to FastCGI server.
location ~ \.php$ {
#if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
fastcgi_pass php5-fpm-sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
Here is what seems to be an official magento2 nginx configuration.
upstream fpm_backend {
server 127.0.0.1:9000; # backend server:port address
}
server {
listen 80 default;
server_name magento.lan www.magento.lan;
root /var/www/magento2; # document root, path to directory with files
index index.html index.php;
autoindex off; # we don’t want users to see files in directories
location ~ (^/(app/\|includes/\|lib/\|/pkginfo/\|var/\|report/config.xml)\|/\.svn/\|/\.git/\|/.hta.+) {
deny all; #ensure sensitive files are not accessible
}
location / {
try_files $uri $uri/ /index.php?$args; # make index.php handle requests for /
access_log off; # do not log access to static files
expires max; # cache static files aggressively
}
location \~\* \.(jpeg\|jpg\|gif\|png\|css\|js\|ico\|swf)$ {
try_files $uri $uri/ #proxy; # look for static files in root directory and
ask backend if not successful
expires max;
access_log off;
}
location #proxy {
fastcgi_pass fpm_backend; # proxy everything from this location to backend
}
location \~\.php$ {
try_files $uri =404; # if reference to php executable is invalid return 404
expires off; # no need to cache php executable files
fastcgi_read_timeout 600;
fastcgi_pass fpm_backend; # proxy all requests for dynamic content to
# backend configured in upstream.conf
fastcgi_keep_conn on; # use persistent connects to backend
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root${fastcgi_script_name};
fastcgi_param MAGE_RUN_CODE default; # Store code is defined in
#administration > Configuration > Manage Stores
fastcgi_param MAGE_RUN_TYPE store;
}
}

Nested Codeigniter apps not working in NGINX

We've setup 2 CodeIgniter apps which has its own complete code bases on its own directories as in this example. Plus we also have wordpress blog on the same public_html directory.
public_html/HOME_APP
public_html/admin_tool/ADMIN_TOOL_CODES
public_html/blog/WORDPRESS_CODES
HOME_APP codes (CodeIgniter) and our WORDPRESS works fine. But the admin_tool (CodeIgniter) doesn't work. We can access only the http://example.com/admin_tool/index.php but not any inside controller pages. When accessing its show 404 error page. And it looks like the routes are handled via APP1
The nginx rules are as follows. Appreciate if anyone can help us to fix the issue with admin_tool
server{
listen 80;
root /home/ubuntu/websites/example.com/public_html;
index index.html index.htm index.php;
server_name example.com;
access_log /home/ubuntu/websites/example.com/logs/access.log;
error_log /home/ubuntu/websites/example.com/logs/error.log error;
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
index index.php;
# 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_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ^~/admin_tool/ {
root /home/ubuntu/websites/example.com/public_html/admin_tool;
index index.php;
try_files $uri $uri/ /index.php$args;
}
}
server {
listen 443 ssl;
server_name example.com *.example.com;
return 301 http://$server_name$request_uri;
}
The following should do the trick for all of your installations:
# We define the index directory at the outermost level and therefore
# only once for all servers. Also note that we use the PHP file first
# because all main directories are handled by PHP scripts and this will
# give us best performance.
index index.php index.html index.htm;
server {
access_log /home/ubuntu/websites/example.com/logs/access.log;
error_log /home/ubuntu/websites/example.com/logs/error.log error;
# 80 is default!
#listen 80;
root /home/ubuntu/websites/example.com/public_html;
server_name example.com;
location / {
# Don't allow access to the logs directory.
location ~* ^/logs {
return 404;
}
# Don't allow access to PHP files.
location ~* \.php$ {
return 404;
}
# Handle static files.
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
# Directly return if the requested URI is a real file.
try_files $uri $uri/ index.php =404;
}
# Codeigniter and WordPress will always handle everything with their
# index.php script, therefore we only need to catch that case.
location = /index.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;
}
}
server {
listen 443 ssl;
server_name example.com *.example.com;
return 301 http://$server_name$request_uri;
}
I had the same problem while I am using codeigniter in subfolder(dashboard).
All non-wordpress requests are not being entertained but remain on wordpress main page.
I fixed my issue by following lines in my configuration file /etc/nginx/sites-available/{my-site-name}
location /dashboard {
try_files $uri $uri/ /dashboard/index.php;
}
location /index.php {
fastcgi_pass unix:/usr/sbin/php5-fpm;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Resources