Nginx + Php-fpm + Zend Framework 1.11 strange rewrite error - zend-framework

I have strange rewrite problem with nginx & php5-fpm on an *nix platform. I'm using a Router.php class as app resource and there are lot of routes in this file which generates user-friendly uri's for corresponding modules & controllers. Now, i want to move some of this regex based application routes to nginx level and when i move a route to nginx config, i'm getting "invalid controller specified" error.
Simply, when a user requests a /foo URI, i want to trigger BarController's bazAction(). It's successfully working on application level (via Router resource)
public function init() {
$router = Zend_Controller_Front::getInstance()->getRouter();
$f = new Zend_Controller_Router_Route_Static('foo',
array( 'controller' => 'Bar',
'action' => 'baz'));
$router->addRoute( 'foo', $f);
...
When i try to do this using nginx rewrite rules like this
rewrite ^/foo /index.php/bar/baz?$args last;
I'm getting Invalid controller specified (foo) error. How can i handle this rewrite rules correctly?
My server.config is:
server {
listen 80;
server_name www.dummy.com;
root /Users/foozy/Sites/dummy.com/public;
index index.php;
location / {
try_files $uri $uri/ #rewrites;
}
location #rewrites {
rewrite ^/foo /index.php/bar/baz?$args last;
# i also tried :
# rewrite ^/foo /bar/baz?$args last;
rewrite ^ /index.php?$args;
}
location ~ ^.+\.php {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
My fastcgi_params file is:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
... nginx's default params here ...
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
...
fastcgi_param APPLICATION_ENV development;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
And in php.ini fix_pathinfo is set to 0:
cgi.fix_pathinfo=0
PHP v5.3.8, Nginx v1.0.10

I used this manual to make routers worked NGINX + PHP-FPM + Zend_Framework Infinite 30x redirect loop

Related

nginx config - negative condition

I make HTTP authorization in nginx config for all files, exclude index.php:
server {
listen 80;
root /var/www/site;
index index.php;
charset utf-8;
location ~ /(?!index.php) {
auth_basic "Unauthorized";
auth_basic_user_file /var/www/site/htpasswd;
}
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;
fastcgi_read_timeout 3000;
}
}
As a result, nginx instead of executing PHP files, begins to give them to download. Without location ~ /(?! index.php) - files are normally executed.
How to make a negative condition correctly?

Nginx multiple root redirect issue

I've the following configuration which works well:
root /var/www/en;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /admin {
try_files $uri $uri/ /admin/index.php;
}
location ~ \.php(/|\?|$) {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_param COCKPIT_URL_REWRITE On;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
}
This redirects all requests to index.php and to admin/index.php for the admin backend.
Now I need to put a copy of the website in a different language to subdirectory having the same redirect rules for the subdirectory. This is actually a separate project with a separate root folder. I'm doing it the following way:
location ~ ^/de {
root /var/www/de;
try_files $uri $uri/ /de/index.php?$args;
}
location ~ ^/de/admin {
root /var/www/de;
try_files $uri $uri/ /de/admin/index.phpd;
}
However, this leads to the following nginx error:
rewrite or internal redirection cycle while internally redirecting to "/de/index.php"
How this could be fixed?
Thanks ;)
UPDATE:
I managed to get static files working by changing root to alias. So, now I can access static files site.com/de/test.txt. However, when I try to access site.com/de/ php handler is not working and browser tries to download the php file. I put php handler inside the new location block but it still not working.
Try this
location ~ \.php(/|\?|$) {
location /de {
root /var/www/de;
try_files $uri $uri/ /de/index.php?$args;
}
location /de/admin {
root /var/www/de;
try_files $uri $uri/ /de/admin/index.php;
}
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_param COCKPIT_URL_REWRITE On;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
}

How to configure Zend Framework project which contains different modules in winginx server

Can any one show how to configure zend framework project in winginx using virtual host? I tried this, but running some issues when I go through the different modules. My server block looks like this:
server {
listen 127.0.0.1:80;
server_name experts.localhost;
root C:/Projects/project1/public;
index index.php index.html;
location / {
if (-f $request_filename) {
root C:/Projects/project1/public;
}
}
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;
}
}

Nginx 1.6 + php5-fpm socket communication fail

I have problem with Nginx and PHP5-fpm, if I configure Nginx 1.2.1 everything is fine, but if I upgrade to 1.6 I have only blank page. Cany anyone say why?
Socket is created fine.
Html pages working
If i see network in Chrome i have 200OK
In PHP is only:
<?php
phpinfo();
?>
My confing files:
/etc/nginx/conf.d/web.conf
server {
listen 192.168.99.127:8080;
server_name sub.localhost;
root /home/clients_ssl/sub/www;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass unix:/etc/php5/fpm/socks/ssl_sub.sock;
include fastcgi_params;
fastcgi_param HTTPS on;
}
location ~ /\. {
deny all;
}
access_log /home/clients_ssl/sub/logs/access.log;
error_log /home/clients_ssl/sub/logs/error.log;
error_page 404 /404.html;
}
/etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
/etc/php5/fpm/pool.d/web.conf
[ssl_sub]
listen = /etc/php5/fpm/socks/ssl_sub.sock
user = nginx
group = nginx
pm = dynamic
pm.max_children = 50
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 5
pm.max_requests = 0
php_admin_value[open_basedir]=/
php_admin_value[session.save_path]=/home/clients_ssl/sub/tmp
php_admin_value[upload_tmp_dir]=/home/clients_ssl/sub/tmp
php_admin_value[disable_functions]=dl
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
((( if i change lister.owner/group and user/group to other system user for example USER0 i have error 502 bad gateway, where is problem? ))))
/etc/nginx/fastcgi_params
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_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
If you have upgraded nginx by including
deb http://nginx.org/packages/debian/ wheezy nginx
deb-src http://nginx.org/packages/debian/ wheezy nginx
in your /etc/apt/sources.list file, you won't have a copy of fastcgi.conf in your /etc/nginx folder.
Find the file fastcgi_params in /etc/nginx and add this line:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
to the top of the file. Rename the file to fastcgi.conf
Then, you can replace include fastcgi_params; to include fastcgi.conf; in your .conf file's php location block.
I had also some problems after update nginx 1.2 to 1.6
I got this information (see below), when I install wheezy-backports main contrib non-free
I put to this list: /etc/apt/sources.list
that source:
deb http://ftp.de.debian.org/debian/ wheezy-backports main contrib non-free
Information by the installation:
Fastcgi configuration issues ============================
nginx shipped a modified fastcgi_params, which declared
SCRIPT_FILENAME fastcgi_param. This line has now been removed.
From now on we are also shipping fastcgi.conf from the upstream
repository, which includes a sane SCRIPT_FILENAME parameter value.
So, if you are using fastcgi_params, you can try switching to
fastcgi.conf or manually set the relevant params.
So I made a change in etc/nginx/conf.d/default.conf
I changed include fastcgi_params; to include fastcgi.conf;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_read_timeout 600;
}
Afterwards, restart php5-fpm and nginx
My php formular / form now works again in nginx 1.6.2

How to remove trailing slash from URL in nginx only if directory doesn't exist?

I am running a server on nginx 1.4.1 with PHP-FastCGI. Currently I have it setup so that it removes trailing slashes from my URLs and issues a 301 redirect. However, when I visit a directory that exists, I am forced into a redirect loop. My current document root looks like this:
- index.php (app)
- webgrind
- index.php
- static
- css
Currently I cannot visit example.com/webgrind or any other directory. My access logs repeatedly read similar to:
GET /webgrind/ HTTP/1.1" 301 178 "-"
GET /webgrind HTTP/1.1" 301 178 "-"
This is the server block in my nginx.conf:
server {
listen 80;
server_name example.com;
location / {
try_files $uri $uri/ /index.php?$args;
root /var/www/example/public;
index index.php index.html index.htm;
}
rewrite ^/(.*)/$ /$1 permanent;
location = /favicon.ico {
access_log off;
log_not_found off;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?$args;
root /var/www/example/public;
index index.php index.html index.htm;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example/public$fastcgi_script_name;
fastcgi_param APPLICATION_ENV testing;
fastcgi_param PATH /usr/bin:/bin:/usr/sbin:/sbin;
fastcgi_intercept_errors on;
include fastcgi_params;
}
}
I am aware that rewrite ^/(.*)/$ /$1 permanent; is the offending line. If I remove it and visit example.com/webgrind, a 301 is issued for me to redirect to example.com/webgrind/ since it is a directory. However, my application will now accept both trailing and non-trailing slashes (i.e. example.com/users/ and example.com/users) and this is not what I want.
Wrapping the 'if' directive around my rewrite as follows still creates a redirect loop for my directories (if is evil, apparently, but a rewrite directive in this case is considered safe):
if (!-d $request_filename) {
rewrite ^/(.*)/$ /$1 permanent;
}
(I know that visiting webgrind/index.php would solve my problem, but I'd like to avoid costly and unprofessional redirect loops when my production directories are pushed live.)
So how can I conditionally strip trailing slashes only for resources that don't exist (my web application paths)?
UPDATE: My (unaltered) fastcgi_params config:
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_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param HTTPS $https;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
Putting the root directive outside of the location block as a direct child of the server block fixed the issue.
server {
listen 80;
server_name example.com;
# This WORKS!
root /var/www/example/public;
location / {
try_files $uri $uri/ /index.php?$args;
index index.php index.html index.htm;
}
if (!-d $request_filename) {
rewrite ^/(.*)/$ /$1 permanent;
}
location = /favicon.ico {
access_log off;
log_not_found off;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?$args;
index index.php index.html index.htm;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example/public$fastcgi_script_name;
fastcgi_param APPLICATION_ENV testing;
fastcgi_param PATH /usr/bin:/bin:/usr/sbin:/sbin;
fastcgi_intercept_errors on;
include fastcgi_params;
}
}
Apparently it is a pitfall that the Nginx wiki recommends to avoid.