Phalcon + Nginx Routes / Rewrites not working - php

I finally got Phalcon+phpfpm+nginx on a Osx to work... trying to setup a quick REST application I am stuck, I believe with an nginx rewrite.
I am always getting a Route not matched error.
File Structure:
/myapp/
public/
index.php
app/
models/
...
index.php
<?php
$app = new \Phalcon\Mvc\Micro();
$app->get('/api/robots', function() {
echo json_encode(['true']);
});
$app->notFound(function(){
echo json_encode(['Route Not Found']);
});
$app->handle();
nginx config file:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
root /var/www/bmex/public;
listen 80;
server_name some.name.com;
charset utf-8;
#access_log /var/log/nginx/host.access.log main;
set $root_path '/var/www/bmex/public';
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;
}
}
location ~ \.php {
# try_files $uri =404;
fastcgi_index /index.php;
fastcgi_pass 127.0.0.1:9000;
include 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;
}
}
}

It appears the Phalcon docs are wrong... or I am missing something... I will find out soon enough.
Replace _uri=/$1 for =$1 ... so far it works..
rewrite ^(.+)$ /index.php?_url=/$1 last;
rewrite ^(.+)$ /index.php?_url=$1 last;

Related

nginx 404 not found in subfolder

I installed a PHP script in /files/ subfolder on Nginx but when I access the page, it's not working. Just showing 404 not found. I also tried changing the root directly to the subfolder but still not working, like this: root /home/smart/web/example.com/public_html/files;
Also, I added this in the config but not working.
location ^~ /files {
if (!-e $request_filename) { rewrite ^/(.*) /files/index.php?_page_url=$1 last; }
location ~ \.php$ {
if (!-e $request_filename) { rewrite ^/(.*) /files/index.php?_page_url=$1 last; }
fastcgi_pass unix:/run/php/php8.0-fpm-example.com.sock;
index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
location /files/files/ {
internal;
}
# these locations would be hidden by .htaccess normally
location /files/logs/ {
deny all;
}
}
Here is my full nginx config:
#=========================================================================#
# Default Web Domain Template #
# DO NOT MODIFY THIS FILE! CHANGES WILL BE LOST WHEN REBUILDING DOMAINS #
# https://docs.hestiacp.com/admin_docs/web.html#how-do-web-templates-work #
#=========================================================================#
server
{
listen myip:443 ssl http2;
server_name example.com;
root /home/smart/web/example.com/public_html;
index index.php index.html index.htm;
access_log /var/log/nginx/domains/example.com.log combined;
access_log /var/log/nginx/domains/example.com.bytes bytes;
error_log /var/log/nginx/domains/example.com.error.log error;
client_max_body_size 5G;
ssl_certificate /home/smart/conf/web/example.com/ssl/example.com.pem;
ssl_certificate_key /home/smart/conf/web/example.com/ssl/example.com.key;
ssl_stapling on;
ssl_stapling_verify on;
include /home/smart/conf/web/example.com/nginx.hsts.conf*;
location /
{
location ~* ^.+\.(jpeg|jpg|png|webp|gif|bmp|ico|svg|css|js)$
{
expires max;
fastcgi_hide_header "Set-Cookie";
}
location ~ [^/]\.php(/|$)
{
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name)
{
return 404;
}
fastcgi_pass unix:/run/php/php8.0-fpm-example.com.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
include /home/smart/conf/web/example.com/nginx.fastcgi_cache.conf*;
}
}
location ^~ /files {
if (!-e $request_filename) { rewrite ^/(.*) /files/index.php?_page_url=$1 last; }
location ~ \.php$ {
if (!-e $request_filename) { rewrite ^/(.*) /files/index.php?_page_url=$1 last; }
fastcgi_pass unix:/run/php/php8.0-fpm-example.com.sock;
index
index.php;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
location /files/files/ {
internal;
}
# these locations would be hidden by .htaccess normally
location /files/logs/ {
deny all;
}
}
location /error/
{
alias /home/smart/web/example.com/document_errors/;
}
location ~ /\.(?!well-known\/)
{
deny all;
return 404;
}
location /vstats/
{
alias /home/smart/web/example.com/stats/;
include /home/smart/web/example.com/stats/auth.conf*;
}
include /etc/nginx/conf.d/phpmyadmin.inc*;
include /etc/nginx/conf.d/phppgadmin.inc*;
include /home/smart/conf/web/example.com/nginx.conf_*;
}

Nginx + php-fpm downloading php file and not executing [duplicate]

This question already has answers here:
Nginx serves .php files as downloads, instead of executing them
(28 answers)
Closed 3 years ago.
I have nginx, php5.6-fpm, setup on the default port (9000).
And I have gone through several forums, SO pages and documentation to alleviate this problem, but to no avail.
The index.php file of my web-app executes as should, but all other .php files get downloaded, It appears it has to do with my rewrite-rules and not Fast Process Manager per-say, but I ca't seem to figure.
Below is my site.conf
server {
listen 80 default_server;
listen [::]:80 ipv6only=on;
server_name xx.xx.xx.xx;
root /var/www/foo.bar.com/html;
# index.php
index index.php index.html;
error_page 404 /404.php;
autoindex off;
location / {
rewrite ^/(.*)/p/(.*)?$ /product.php?slug=$2 break;
rewrite ^/?([A-Za-z0-9_-]+)/?$ /vendor.php?vendor=$1 break;
if (!-e $request_filename){
rewrite ^(.*)$ /$1.php break;
}
}
location /cart {
rewrite ^/cart/?$ /cart.php break;
}
location /checkout {
rewrite ^/checkout/?$ /checkout.php break;
}
location /search/ {
rewrite ^/search/?$ /search.php break;
rewrite ^/search/(.*)?$ /search.php?slug=$1 break;
rewrite ^/search/brand/(.*)?$ /search.php?brandslug=$2 break;
}
location /brand {
rewrite ^/brand/(.*)?$ /search.php?brand=$1 break;
}
location /brands {
rewrite ^/brands/?$ /productbrands.php break;
}
location /404 {
rewrite ^/404/?$ /404.php break;
}
location /vendors {
rewrite ^/vendors/?$ /vendors.php break;
}
include /etc/nginx/mime.types;
# handle .php
location ~* \.php(/|$) {
try_files $uri =404;
include /etc/nginx/mime.types;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param HTTPS off;
include nginxconfig.io/php_fastcgi.conf;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
include /etc/nginx/mime.types;
access_log off;
log_not_found off;
expires 360d;
}
include nginxconfig.io/general.conf;
}
Please add this:
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php5.6-fpm.sock;
}

nginx rewrite rules codeigniter

i am using Winginx for using nginx php and mysql on windows.
i want run and config CodeIgniter 2.
Winginx structure:
d:\winginx|
nginx.exe
php5\php.exe
mysql\mysqld.exe
\home\localhost\public_html\codeigniter
base this (http://wiki.nginx.org/Codeigniter)
and this (http://www.farinspace.com/codeigniter-nginx-rewrite-rules/)
and this (Nginx rewrite rule for CodeIgniter)
i try coonfig ci but i just see codeigniter 404 page (no nginx 404 page)
my ci config.php is
$config['base_url'] = "";
$config['index_page'] = "";
$config['uri_protocol'] = "AUTO";
..
nginx.config is
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid temp/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
client_max_body_size 55m;
#gzip on;
scgi_temp_path temp/uwsgi_temp 1 2;
uwsgi_temp_path temp/uwsgi_temp 1 2;
fastcgi_connect_timeout 1;
include codeigniter.conf;
}
i include codeigniter.conf in nginx.conf
codeigniter.conf is:
server {
listen 80;
server_name localhost;
root home/localhost/public_html/codeigniter:
autoindex on;
index index.php;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
location = /index.php{
fastcgi_pass localhost:9000;
#fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
location ~ \.php$ {
return 444;
}
}
i try 3 different coonfig ci but i just see codeigniter 404 page (no nginx 404 page).
http://localhost/codeigniter/welcome/index -> CI 404 page!
http://localhost/codeigniter/welcome -> CI 404 page!
http://localhost/codeigniter -> CI 404 page!
i guest the problem is created by the php file location in codeigniter.conf
please help me to config it !
I'd try this
server {
listen 80;
server_name localhost;
root home/localhost/public_html/codeigniter;
autoindex on;
index index.php;
location / {
try_files $uri $uri/ /index.php;
}
location = /index.php {
fastcgi_pass localhost:9000;
#fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
location ~ \.php$ {
return 444;
}
}
Here's what I use for a production CI install
server
{
listen 80 default_server;
server_name www.site.com;
root /var/www;
index index.php;
# canonicalize codeigniter url end points
location ~ ^/index.php/(.*[^/])$ { return 301 $scheme://$host/$1/$is_args$args; }
location ~ ^/index.php/(.*)/$ { return 301 $scheme://$host/$1/$is_args$args; }
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;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I got it working the following way...
server {
listen 127.0.0.1:80;
server_name xxxx www.xxxx;
root home/xxxx/public_html;
autoindex on;
index index.php;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
location ~ \.php$ {
if (!-e $document_root$document_uri){
return 404;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ \.php$ {
return 444;
}
}

nginx.conf for url routing

I'm trying to have my index.php to handle http routing so make my app restful.
I've used the try_files directive within nginx.cong but did'nt work, I hit /blablabla and instead of going through index.php it throws a 404.
Here's my current nginx.conf
<pre>
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
}
http {
include /etc/nginx/mime.types;
access_log /var/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
location / {
try_files $uri $uri/ /index.php;
}
}
}
</pre>
You might want to try something like this, works like a charm for me:
location / {
try_files $uri $uri/ #rules;
}
location #rules {
rewrite ^/(.*)$ /index.php?param=$1;
}
This looks for the location at / which is your web root. All of your web accessible files are found in this directory. If a file exists, it'll take you to that file. If not, then it'll throw you into the #rules block. You can use regexp matching to vary your url formatting. But in short, the (.*) matches any string in your url and takes you to your index. I modified what you had written slightly to feed the original input in to index.php as a parameter. If you don't do this, your script won't have any info about how to route the request.
For example, then going to /blablabla will mask the url but pull up /index.php?param=blablabla so long as /blablabla isn't a directory.
Hope this helps!
server {
listen 80;
server_name example.com;
index index.php;
error_log /path/to/example.error.log;
access_log /path/to/example.access.log;
root /path/to/public;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
}
}

How to use Nginx to separate variables with slashes

I'd like to be able to use
www.example.com/profiles/1234567890
instead of
www.example.com/profiles?id=1234567890
but was unable to figure out how to do so.
I found an apache equivalent here: Turn text after slashes into variables with HTACCESS but I don't know how to get it to work in Nginx. I only need the one variable which is id.
Additionally, is it possible to do a rewrite such that
www.example.com/id/1234567890
points to
www.example.com/profiles/1234567890
but the url doesn't change?
Nginx config
server {
server_name www.domain.com;
rewrite ^(.*) http://domain.com$1 permanent;
}
server {
listen 80;
server_name domain.com;
root /var/www/domain.com/public;
index index.php;
access_log /var/www/domain.com/access.log;
error_log /var/www/domain.com/error.log;
rewrite ^/profiles?id=(.*)$ /profiles/$1 last;
rewrite ^/id/(.*)$ /profiles/$1 last;
# unless the request is for a valid file, send to bootstrap
if (!-e $request_filename)
{
#rewrite ^(.+)$ /index.php?q=domain.com last;
rewrite ^(.*)$ $1.php last;
}
# catch all
error_page 404 /index.php;
# Directives to send expires headers and turn off 404 error logging.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
access_log off;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass 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 = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
deny all;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ #extensionless-php?$args;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
# use fastcgi for all php files
location ~ \.php$
{
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
# Some default config
fastcgi_connect_timeout 20;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
}
}
Inside the server block of your configuration file, try adding this:
rewrite ^/profiles?id=(.*)$ /profiles/$1 last;
rewrite ^/id/(.*)$ /profiles/$1 last;
Restart nginx. It should work.
More information can be found here: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
I think you put the rules swapped, try something like
rewrite ^/(?:profiles|id)/(.*) /profiles?id=$1 last;
For anyone looking for the answer, this worked:
rewrite ^/profiles/([a-zA-Z0-9_-]+)$ /profile.php?id=$1;
rewrite ^/id/([a-zA-Z0-9_-]+)$ /profile.php?id=$1;

Categories