Phalcon and nginx - framework run only indexController - php

I am using Phalcon and Nginx, and i have a problem.
When I go to http://myapp.dev/segmentation Phalcon should run SegmentationController and its indexAction() method. But instead, Phalcon is running IndexController(default controller for "/"). I think problem is with Nginx configuration, because all works fine under Apache.
Here is my Nginx site configuration:
server {
listen 80;
server_name myapp.dev;
index index.php index.html index.htm;
set $root_path '/var/www/myapp/public';
root $root_path;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri $uri/ =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
location ~ /\.ht {
deny all;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri $uri/ =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
}

USe this config
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
Working Config

Its the problem with location directive
Use this configuration.
location / {
root $root_path;
index index.php index.html index.htm;
# if file exists return it right away
if (-f $request_filename) {
break;
}
# otherwise rewrite it
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?_url=/$1 last;
break;
}
Hope this helps!
Happy Coding !!!

Related

I make Nginx keep routing for one php script but I got 404 Not Found error

I have my PHP project that handles all the request in index.php file. So, I install nginx on my ubuntu 22.04 and my configuration is:
server {
listen 80;
server_name crm.localtest.me;
root /home/truong/Documents/github/web_projects/crm_system/public;
error_log /home/truong/Documents/github/web_projects/crm_system/public/error_log.html;
location /images {
try_files $uri $uri/ =404;
}
location /js {
try_files $uri $uri/ =404;
}
location /css {
try_files $uri $uri/ =404;
}
location / {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_param SCRIPT_NAME index.php;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
# location ~ \.php$ {
# include snippets/fastcgi-php.conf;
# fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
# }
location ~ /\.ht {
deny all;
}
}
I can browse my home page when access crm.localtest.me but other uri such as crm.localtest.me/auth/login turn to 404 Not Found.
I tried to use
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
but it still not works.

Laravel 5.2 not working in PHP 5.6 with Nginx server

I am currently migrating my website from Apache to nginx, but my .htaccess file is not working. How can I use .htaccess in my nginx server?
My nginx config file:
server {
#listen 8000;
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
#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 index.nginx-debian.html;
server_name localhost;
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/var/run/php5-fpm.sock; # or 127.0.0.1:9000
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
location ~ \.php$ {
#try_files $uri =404;
try_files $uri /index.php =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;
}
location ~ /\.ht {
deny all;
}
}
What I tried:
sudo chown -R www-data:www-data *
sudo chown -R root:root *
also I tried to change
try_files $uri $uri/ /index.php?$query_string;
try_files $uri $uri/ /index.php$is_args$args;
try_files $uri $uri/ /index.php;
php artisan cache:clear
Mostly questions in google i have read, but nothing helps me.

nginx - Friendly URL's in directory

I move my Friendly URL's from Apache to nginx and I have a problem. I want to Friendly URL's only works within the subdirectory sgforum.
In PHP, I receive the addresses as: 127.0.0.1/sgforum/index, 127.0.0.1/sgforum/member etc.
When I go on 127.0.0.1/sgforum/ - it works, but when I give member (127.0.0.1/sgforum/member), or index, it downloads a file to my computer, instead of opening with php.
This is my /etc/nginx/sites-available/default file:
server {
listen 80 default_server;
#listen [::]:80 default_server;
root /home/ariel/workspace;
index index.php index.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# FRIENDLY URLS
location /sgforum/ {
if (!-e $request_filename){
rewrite ^/sgforum/(.*)$ /sgforum/index.php break;
}
}
location ~ /\.ht {
deny all;
}
}
I changed it, and finally works as it should.
# FRIENDLY URLS
location /sgforum/ {
try_files $uri $uri/ /sgforum/index.php;
}
you have to set location for member folder
try change
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

CodeIgniter and Nginx configurations

I developed my CI app and tested it on over 4 Apache server and it's working fine. But aftaer i upload it int to the new server which is Nginx, it has problem with urls. I Googled a lot and approximately did every thing but there was not result. Now here's the problem:
Due to project structure and scale i need every url to be like this:
http://example.com/myappname/controller/action
ex: http://example.com/myappname/auth/login
But it's not working. What is working is:
http://example.com/myappname/index.php?/auth/login
which is not what i want.
The nginx configuration is:
server {
listen 80;
listen [::]:80;
root /var/nginx/www/mydomain.com/www;
index index.php index.html index.htm;
server_name mydomain.com www.mydomain.com;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
# With php5-fpm:
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
location ~* \.(png|jpg|jpeg|gif|ico|ttf|woff)(\?ver=[0-9.]+)?$ {
expires 1w;
}
location ~* \.(css|js|html)(\?ver=[0-9.]+)?$ {
expires 1w;
}
}
Your codeIgniter config.php contains the following information:
$config['base_url'] = "http://domain.tld/";
$config['index_page'] = "";
$config['uri_protocol'] = "REQUEST_URI";
And here is the nginx rewrite rule,
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;
}
}
I had the same problem.
I have a website in
http://website.com
and the codeigniter folder in
http://website.com/partners.
I just change the nginx configuration to have a location specifically for this codeigniter folder.
I add it like this.
location / {
try_files $uri $uri/ /index.php;
}
location /partners {
try_files $uri $uri/ /partners/index.php;
}
As for your case. I think this setting should work like below :
server {
listen 80;
listen [::]:80;
root /var/nginx/www/mydomain.com/www;
index index.php index.html index.htm;
server_name mydomain.com www.mydomain.com;
location / {
try_files $uri $uri/ /index.php;
}
#ADD THIS LINE
location /myappname {
try_files $uri $uri/ /myappname/index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
# With php5-fpm:
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
location ~* \.(png|jpg|jpeg|gif|ico|ttf|woff)(\?ver=[0-9.]+)?$ {
expires 1w;
}
location ~* \.(css|js|html)(\?ver=[0-9.]+)?$ {
expires 1w;
}
Make sure to test that everything is working by running sudo nginx -t and then restart nginx. Hopefully it will work.

Nested Laravel Project Nginx Config

We are introducing a new section on our website which uses the Laravel framework. It is placed in a sub-directory, for example /newsection. How should I configure the nginx.conf without making any conflicts with my previous rewrite rules.
This is my current nginx.conf
server {
listen 80;
server_name localhost www.website.com;
root /home/www/website;
index index.html index.php;
location /newsection/ {
rewrite ^/ /newsection/public/index.php last;
# this is my attempt at it
}
location / {
try_files $uri $uri/ #rewrite;
}
location /php-status {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
location #rewrite {
rewrite ^/([\w_-]+)/?$ /index.php?page=$1 last;
rewrite ^/([\w_-]+)/([\w_-]+)/?$ /index.php?page=$1&slug=$2 last;
rewrite ^/([\w_-]+)/([\w_-]+)/([\w_-]+)/?$ /index.php?page=$1&slug=$2&pagination=$3 last;
}
include php.conf;
}
If it works like most frameworks I know work, this should work
location /newsection/ {
try_files $uri $uri/ /newsection/public/index.php$request_uri;
}
If this returns 404 try removing the extra /newsection in the fallback URI of the try_files
Try this for laravelin subdirectories
location ^~ /laravel {
alias /var/www/laravel/public;
try_files $uri $uri/ #laravel;
location ~ \.php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
}
}
location #laravel {
rewrite /laravel/(.*)$ /laravel/index.php?/$1 last;
}

Categories