Router not working (Heroku + Nginx + PHP Phalcon) - heroku

I don't get it why the mvc routes are not working.
When I access the home page, all the css and js are loaded.
"GET /css/main.css HTTP/1.1" 304
But when I access any other controller, I got:
method=GET path="/transaction/add"
[error] open() "/app/public/transaction/add" failed (2: No such file or directory)
"GET /transaction/add HTTP/1.1" 404
Here's my procfile
--Procfile--
web: vendor/bin/heroku-php-nginx public/
location / {
# try to serve file directly, fallback to rewrite
try_files $uri #rewriteapp;
}
location #rewriteapp {
# rewrite all to app.php
rewrite ^(.*)$ /index.php/$1 last;
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
try_files #heroku-fcgi #heroku-fcgi;
internal;
}
I'm really lost here.

This should work:
location / {
# try to serve file directly, fallback to rewrite
try_files $uri #rewriteapp;
}
location #rewriteapp {
# rewrite all to app.php
rewrite ^(.*)$ /index.php/$1 last;
}
location ~ ^/index\.php(/|$) {
try_files #heroku-fcgi #heroku-fcgi;
internal;
}

Try to follow the instructions in the documentation: http://docs.phalconphp.com/fr/latest/reference/nginx.html
server {
listen 80;
server_name localhost.dev;
index index.php index.html index.htm;
set $root_path '/var/www/phalcon/public';
root $root_path;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index /index.php;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
location ~ /\.ht {
deny all;
}
}

Related

Laravel + nginX + Windows 10 + api routing issue (404 Error)

I am trying to make a very simple API request with above settings. It is working perfectly if I serve it with artisan serve command. But If I make the request with nginx, it gives 404 error. Thats why I guess the issue is with the nginX. my nginX conf is as follows.
server {
listen 80 ;
listen [::]:80 ;
root D:/sites/demo/public;
index index.php index.html index.htm ;
server_name laravel.local;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri /index.php = 404;
fastcgi_pass 127.0.0.1:9123;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}

Laravel + NGINX CSS/JS 404 not found

Laravel 5.5 fresh
nginx version: nginx/1.10.3 (Ubuntu)
When I go to: http://<my-ip>/crm/ all is working great, I get the Laravel welcome page, all js and css are loading correctly.
When I go to http://<my-ip>/crm/register - I get 404 for css and js.
This is my conf:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name _;
rewrite ^/((?!en)[a-z]*)/home$ /index.php?lang=$1&$args last;
rewrite ^/((?!en)[a-z]*)/sitemap.xml$ /sitemap.xml last;
rewrite /pigeon/(.*)$ /pigeon/index.php?/ last;
rewrite /crm/(.*)$ /crm/index.php?/ last;
if (!-f $request_filename){
set $rule_52 1$rule_52;
}
if ($rule_52 = "1"){
rewrite ^/(.*)(?<!php)$ /$1.php last;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /pigeon {
alias /var/www/html/pigeon/public;
try_files $uri $uri/ #pigeon;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
location /crm {
alias /var/www/html/crm/public;
try_files $uri $uri/ /index.php?$query_string;
location /crm.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
How do I fix this conf file to catch css and js files correctly?
Thanks
I had a similar issue. It seems allright what you have there in the server block.Although you might be better off if you add a proper server name.
Check this question and see if it helps. It has to do with the symlink to var/www/html for your crm, if you used one.
Laravel 8 + nginx - app.css and app.js resources from public/ not loading - 404 not found
And if not you could still use the online CDN's from bootstrap.Also in the question.

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

nginx one domain and multiple projects setting alias

I want to use one domain to map different project.
mydomain.com.tw => /var/www/project1/public
mydomain.com.tw/test => /var/www/project2
but mydomain.com.tw/project2/detail always direct to mydomain.com.tw project route.
project1 => laravel
project2 => vue and using vue-route
listen 80;
listen [::]:80;
server_name mydomain.dev;
index index.php index.html index.htm;
root /var/www/project1/public;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /lottery {
alias /var/www/project2/dist;
try_files $uri $uri/ #rewrites;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
I solved this question.
Solution:
https://gist.github.com/Maras0830/dc6f627eba005bdfc6b741f7f2ea3178

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