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;
}
}
}
Related
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 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
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.
There is a problem with the location.
We need to do that at the request:
http://domain.com open the site by way of /var/www/domain/main/web
http://domain.com/job open the site by way of /var/www/domain/job/web
Try this config:
server {
listen *:80;
server_name domain.com;
access_log /var/log/nginx/domain.com.access.log;
error_log /var/log/nginx/domain.com.error.log;
location /job/ {
root /var/www/domain/job/web;
try_files $uri $uri/ /index.php?$args;
index index.html index.htm index.php;
location ~ \.php$ {
root /var/www/domain/job/web;
try_files $uri $uri/ /index.php?$args;
index index.html index.htm index.php;
fastcgi_index index.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_param APP_ENV dev;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
}
}
location / {
root /var/www/domain/main/web;
try_files $uri $uri/ /index.php?$args ;
index index.html index.htm index.php;
location ~ \.php$ {
root /var/www/domain/main/web;
try_files $uri $uri/ /index.php?$args;
index index.html index.htm index.php;
fastcgi_index index.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_param APP_ENV dev;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
}
}
}
The location /job/ not work, open site in /