Nginx location for PHP files that do not exist - nginx

I have config(minimal):
server {
listen test.local:80;
server_name test.local;
server_name_in_redirect off;
location / {
root /data/www/test/public;
try_files $uri $uri/ /index.php?route=$uri&$args;
index index.html index.php;
}
location ~ \.php$ {
root /data/www/test/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/test/public$fastcgi_script_name;
fastcgi_param ...
...
}
}
Thats work file for urls /help/ or /contacts/ etc… (all redirect to index.php with get variables).
But if url, for example, /help.php or contacts.php, and this files not exists, I have output:
File not found.
How update my Nginx config? I need URLs, for example:
/help.php => /index.php?route=/help.php
/contacts.php?foo=bar... => /index.php?route=/contacts.php&args=…

Here is simple config for 404 error page . Its custom 404 page.
error_page 404 /404.php;
location /404.php {
root /var/www/public_html;
allow all;
}
Last part
Important line is ========== try_files $uri =404;
location ~ .php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/public_html$fastcgi_script_name;
include fastcgi_params;
}

Related

Conditionally prepend path to url in NGINX

I have this directory structure for my projects
/var/www
/project-a
/data <-- configuration and user files/data
/app <-- all the code is in sub-dirs in here
/pm <-- a home brew project management app
/.pmdata <-- data used by pm
My goal is to configure NGINX so I can access the project itself through
http://project-a.dev/ and the project management with http://project-a.dev/pm/.
In other words, I want the second url preserved as is, but if the url does not point to /pm/* it should be re-written to have the missing /app prepended to it.
I have tried the following configuration but http://project-a.dev/pm/ results in 404 and http://project-a.dev/ first redirects to http://project-a.dev/app/ and then gives 404.
What am I doing wrong?
server {
listen 127.0.0.1:80;
root /var/www/project-a;
index index.php index.html index.htm;
server_name project-a.dev;
location / {
try_files $uri $uri/app $uri/app/ =404;
}
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;
}
}
Alternatively you could append /app to the root value for all URIs that do not begin with /pm. For example:
server {
...
root /var/www/project-a/app;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
location ^~ /pm {
root /var/www/project-a;
try_files $uri $uri/ =404;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
The nested location block for location ~ \.php$ executes PHP files within the /pm hierarchy. The ^~ modifier is necessary to avoid the other location ~ \.php$ block taking control. See this document for details.

Nginx - Use different directory for serving files while stripping out path from URL

I am trying to use different directory from where to serve the files, but have been unsuccessful. /var/www/site.app/public is where the root, but I want to serve news from /var/www/news/api instead, while having URL http://site.app/news/123, from which I want to remove the news part, because otherwise it would map to /var/www/news/api/news/123.
Judging by the debug logs, it seems that it gets rewritten correctly when first testing all location blocks, but after it is done rewritting, it goes through all of them again, and ends up serving content with location block /.
Here is my configuration file I have.
server {
listen 80;
server_name site.app;
root /var/www/site.app/public;
rewrite_log on;
error_log /var/log/nginx/error.log debug;
index index.php index.html;
location / {
try_files $uri $uri/ index.php?$query_string;
}
location /news/ {
root /var/www/news/api;
rewrite ^/news/(.*) /api.php?_=$1;
index index.php index.html;
}
location ~ \.(hh|php)$ {
try_files $uri /index.php =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
The root directive in your /news/ location currently does nothing. You are rewriting the URI to /api.php which then hits your \.(hh|php)$ location, which inherits the root from the parent server container.
You need a distinct location for /api.php, possibly /news/api.php. In which case you can construct a separate location block (possibly nested within /news/) where you can place fastcgi_pass code using the different root.
So this is not ideal solution, but it works. By default it would still allow access to api.php, but I am denying access to it by checking if the URL contains /news/
server {
listen 80;
server_name site.app;
root /var/www/site.app/public;
index index.php index.html;
location / {
try_files $uri $uri/ index.php?$query_string;
}
location = /api.php {
if ($request_uri !~ ^/news/) {
return 444;
}
root /var/www/site.app/news/;
try_files $uri $uri/ /api.php?$query_string =408;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /news/ {
rewrite ^/news/(.*) /api.php?$1;
}
location ~ \.(hh|php)$ {
try_files $uri /index.php =407;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

nginx configuration for Laravel 4

I am trying to setup my Laravel 4 project using nginx . Here is my nginx server block for laravel :
server {
listen 80;
root /home/prism/www/laravel/public;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
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;
}
But my problem is , Its showing "404 not found" error for all other routes except the default one , that comes with default installation .
This is an NGINX Configuration i've used with Laravel 4 and Laravel 4.1 that works.
server {
listen 80;
server_name sub.domain.com;
set $root_path '/var/www/html/application_name/public';
root $root_path;
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
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;
}
}
you could try this for location / { ... }
location / {
try_files $uri $uri/ /index.php?$query_string;
}
$query_string worked for me.

Kohana does not seem to work with NGiNX

Here is the error I get in the logs:
2012-08-22 17:20:35 --- ERROR: HTTP_Exception_404 [ 404 ]: Unable to find a route to match the URI: index.php ~ SYSPATH/classes/kohana/request.php [ 1126 ]
2012-08-22 17:20:35 --- STRACE: HTTP_Exception_404 [ 404 ]: Unable to find a route to match the URI: index.php ~ SYSPATH/classes/kohana/request.php [ 1126 ]
--
#0 /var/www/index.php(109): Kohana_Request->execute()
#1 {main}
Here is my nginx configs:
server {
listen 80;
server_name 000.000.00.00;
root /var/www;
index index.php;
location / {
try_files $uri $uri/ #kohana;
}
location ~ /\. {
deny all;
}
location ~* \.php$ {
try_files $uri $uri/ #kohana;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
include /usr/local/nginx/conf/fastcgi_params;
}
location #kohana {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
In the front page I get:
HTTP_Exception_404 [ 404 ]: Unable to find a route to match the URI: index.php
and
DOCROOT/index.php [ 109 ] » Kohana_Request->execute()
Thanks in advance for any help!
UPDATE
Kohana::init(array(
'base_url' => '/',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
I had a similar issue and this was the nginx config we ended up with
server {
listen 80;
server_name <<INSERT SERVER NAME>>;
root /add/root/directory;
index index.php;
# ROUTING TO KOHANA IF REQUIRED
location / {
try_files $uri $uri/ #kohana;
}
# BLOCKS ACCESS TO . FILES (.svn, .htaccess, ...)
location ~ /\. {
deny all;
}
# FOR PHP FILES
location ~* \.php$ {
# PHP FILES MIGHT BE TO HANDLED BY KOHANA
try_files $uri $uri/ #kohana;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# HANDLES THE REWRITTEN URLS TO KOHANA CONTROLLER
location #kohana
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
# CACHE CONTROL FOR STATIC FILES
location ~* \.css|\.js|\.jpg|\.jpeg|\.png|\.gif|\.swf|\.svg|\.tiff|\.pdf$ {
expires 30d;
}
# REDIRECTING MEDIAS TO STATIC
location ^~ /medias/ {
rewrite ^/medias/(.*) http://static.xxxx.com/$1 permanent;
break;
}
}
Try browsing to http://localhost/ instead of http://localhost/index.php.
If that's not it, the example nginx config might help.

nginx Wordpress URL rewrite

I've just installed nginx 1.0.8 and php-fpm and for the last 30 minutes I'm trying to rewrite the URL for Wordpress.
Here is what the Wordpress URL should look like:
http://localhost/website/blog/2011/10/sample-post/
I've looked at this tutorial: http://wiki.nginx.org/WordPress + many other on the web but every time I receive an 404 error (sometimes 403).
Here is what I did in my configuration file:
location /website/blog {
try_files $uri $uri/ /website/blog/index.php;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(/website/blog)(/.*)$;
include fastcgi_params;
error_page 404 /404.html;
}
With this configuration I'm receiving "403 forbidden" status.
What am I missing?
Could be the permission of the root site folder
This is an example that i use to wordpress
server {
listen 80;
server_name www.mysite.com mysite.com;
root /srv/www/mysite.com/public_html;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
include /srv/www/mysite.com/public_html/*.conf;
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;
}
}
Did you try restarting nginx after you have saved your config?
Also, check out my nginx/WordPress setup guide here:
http://themesforge.com/featured/high-performance-wordpress-part-3/

Resources