I want case insensitive for my php file.
I tried :
location ~* / {
rewrite ^/(.+)$ /index.php?url=$1 last;
}
location ~* \.php$ {
try_files $uri =404;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 300;
}
I get 500 Internal Server. Where is error? Thanks in advance
Related
I'm not getting through this.
My server is app.local and I need to respond to:
http://app.local/api/v1/
I need to configure my nginx to serve files placed in:
/app/api/code
So the filesystem is not reflecting the http request form.
CURRENT VERSION
server {
server_name app.local;
index index.php;
location /api/v1 {
alias /app/api/v1/code;
try_files $uri /api/v1/index.php$is_args$args;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass api-v1-php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
Ok, if I remove the outer try_files it seems to find the index.php, BUT i lose some functionality (I need to redirect every path to the index handler). How can I solve this? Is this a bug?
SOLUTION
This post had the solution: https://stackoverflow.com/a/35102259/2373113
UPDATED VERSION
server {
server_name app.local;
index index.php;
location /api/v1/ {
alias /app/api/v1/code/;
try_files $uri $uri/ /api/v1//api/v1/index.php$is_args$args;
location ~ /api/v1/.+\.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass api-v1-php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
My nginx configuration:
location ~(\d*?)-(\d*?).news.html{
try_files $uri $uri/ /controller/news.php?id=$2&count=$3;
}
location ~/(\d*?)-(\d*?).journal.html {
try_files $uri $uri/ /controller/journal.php?id=$1&count=$2;
}
location ~/(\d*?)-(\d*?).event.html{
try_files $uri $uri/ /controller/event.php?id=$1&count=$2;
}
location ~ /news.php$ {
fastcgi_cache my_cache;
fastcgi_cache_key $scheme$host$request_uri$request_method;
#cache for 2 hours
fastcgi_cache_valid 200 2h;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /journal.php$ {
fastcgi_cache my_cache;
fastcgi_cache_key $scheme$host$request_uri$request_method;
#cache for 1 day
fastcgi_cache_valid 200 1d;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /event.php$ {
fastcgi_cache my_cache;
fastcgi_cache_key $scheme$host$request_uri$request_method;
#cache for 5 hours
fastcgi_cache_valid 200 5h;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I have three (or more) locations with html url redirected to corresponding php script, and with different fastcgi_cache_valid time.
So that I need to add totally six location routes to handle such logic. But at the bottom, a php location route without caching is needed for other php scripts.
However, all php location route have the nearly same attributes. How can it be shared among all php location route? Or is there any other shorter way to achieve the same mechanism?
I guess you at least can move this code to external file
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
and replace it by something like...
include /path/to/php4fpm-nginx-fastcgi.conf;
Location parsing also looks replaceable
location ~(\d*?)-(\d*?).news.html{
try_files $uri $uri/ /controller/news.php?id=$2&count=$3;
}
location ~/(\d*?)-(\d*?).journal.html {
try_files $uri $uri/ /controller/journal.php?id=$1&count=$2;
}
location ~/(\d*?)-(\d*?).event.html{
try_files $uri $uri/ /controller/event.php?id=$1&count=$2;
}
with
location ~(\d*?)-(\d*?).(news|journal|event).html{
# note it has changed order for vars.
try_files $uri $uri/ /controller/$3.php?id=$1&count=$2;
}
I would go even deeper and use set $var "value" in conditions and then reuse code, but let it be your homework.
I am configurating a virtual hosts with nginx. When I put my address, the page return me this error: File not found.
My configuration is the next:
server {
listen 80;
server_name vcarlos.lan;
root /home/tfc_dev/tfc/web;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log error;
index app.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /app.php/$1;
}
location ~ \.php(/|$) {
# try_files $uri =404;
fastcgi_index app.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
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;
fastcgi_buffer_size 1280k;
fastcgi_buffers 4 2560k;
fastcgi_busy_buffers_size 2560k;
}
location ~ /\.ht {
deny all;
}
}
EDIT #1
I have modified the code and I put the next:
server {
server_name vcarlos.lan;
root /home/sierra/tfc_dev/tfc/web;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fasserver {
server_name vcarlos.lan;
root /home/sierra/tfc_dev/tfc/web;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}tcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
Now it recognise Symfony2 but it return this error:
Oops! An Error Occurred
The server returned a "404 Not Found".
Something is broken. Please let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.*
Please replace:
rewrite ^/(.*)$ /app.php/$1;
with:
rewrite ^/(.*)$ /app.php?query_string;
UPD: Try this one:
server {
listen 0.0.0.0:80;
server_name vcarlos.lan;
root /home/tfc_dev/tfc/web;
index app.php;
try_files $uri $uri/ /app.php?$query_string;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_pass php-fpm;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
}
I have a little experience with configuring nginx server and here is my trouble.
I am trying set correct locations. I have two directs: address.com and address.com/api.
For last direction(API) I have setted locations and it works fine. API is located in /var/www/project/api folder.
root /var/www/project;
index index.php;
server_name localhost;
location /api {
try_files /api/$uri $uri/ /api/index.php?$query_string;
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^/api/(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_buffer_size 32k;
fastcgi_busy_buffers_size 64k;
fastcgi_buffers 4 32k;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
}
Now I need implement root for address.com to /var/www/project/website. And here I have some troubles.
First thing, what I did I had written that:
location / {
alias /var/www/project/website/;
}
And then I tried to add many different variants and here is my last note.
I have put it inside location / {}
location ~ ^/(.+\.php)$ {
alias /var/www/project/website/;
include /etc/nginx/fastcgi.conf;
proxy_intercept_errors on;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
In /etc/nginx/fastcgi.conf file I have added
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
And I get all time 403 Forbidden or 404 Not found or in nginx errors log is written that, for example, /var/www/project/website/... is not found.
Has somebody experience with nginx configuring and can tell, how to set /website location correct?
Something like that:
server {
listen 80;
server_name localhost;
root /var/www/src/website;
index index.php index.html;
error_log /var/log/nginx/error.log;
location / {
try_files $uri $uri/ =404;
}
location /test {
try_files $uri $uri/test.html =404;
}
location /api/ {
alias /var/www/src/api/;
try_files $uri $uri/ /index.php =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location /pmants {
root /var/www/src/;
index index.php index.html index.htm;
location ~ ^/pmants/(.+\.php)$ {
try_files $uri =404;
root /var/www/src/;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/pmants/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /var/www/src/;
}
}
location ~* \.php {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_cache off;
fastcgi_index index.php;
}
}
I want to make a sub directory my on web server, system that serves a backend from another folder for people, but I am having some difficulty.
The server configuration should translate system as index.php of the /srv/www/xxx/backend/web, essentially system should alias to the index of another directory.
I have a configuration like:
location /system {
alias /srv/www/xxx/backend/web;
rewrite ^(.*) /index.php?r=$1;
return 200 $document_root$fastcgi_script_name;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
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 / {
rewrite /(.*) /index.php?r=$1;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
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 have tried numerous iterations (including using root), however even though I can get:
return 200 $document_root$fastcgi_script_name;
to give me:
/srv/www/xxx/backend/web/index.php
and I have vi'ed into this file to make sure it works when I take out the return wget gives me a 404. I am sure I am missing something really simple.
Can someone help me understand what is wrong?
As location php is nested the /index.php URI is not resolved here but in the last block of your configuration. Due to a long standing bug in nginx alias doesn't work with try_files so you need to use the root/rewrite couple instead. So you can fix this with :
location /system {
root /srv/www/xxx/backend/web;
rewrite ^/system/(.*)$ /$1 break;
try_files $uri /system/index.php?r=$uri;
location ~ \.php$ {
rewrite ^/system/(.*)$ /$1 break;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
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 / {
rewrite /(.*) /index.php?r=$1;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
To complete the accepted answer, I add some parts to get static files working:
location ~ ^/system(.*) {
root /srv/www/xxx/backend/web;
rewrite ^/system/(.*)$ /$1 break;
try_files $uri /system/index.php?r=$1&$args;
location ~ \.php$ {
rewrite ^/system/(.*)$ /$1 break;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
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 ~ (.*\.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar|mp4|ogg|woff|ttf))$ {
rewrite ^/system/(.*)$ /$1 break;
try_files $uri =404;
}
}
The last location solves the static files problem whereby files would not load from this place.