My nginx configuration for laravel
server {
listen 80;
server_name app.dev;
rewrite_log on;
root /var/www/l4/angular;
index index.html;
location /{
# URLs to attempt, including pretty ones.
try_files $uri $uri/ /index.php?$query_string;
}
location /lara/ {
index index.php;
alias /var/www/l4/public/;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
location ~ ^/lara/(.*\.php)$ {
alias /var/www/l4/public/$1;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Laravel route:
Route::get('/', function()
{
return View::make('index');
});
Route::get('x', function()
{
return "alpha";
});
my problem is,"http://app.dev/lara/index.php" is working but "http://app.dev/lara" and lara/x is not working.
In a nutshell, make the following edits. An explanation of why is below.
Replace
try_files $uri $uri/ /index.php?$query_string;
with
try_files $uri $uri/ /lara/index.php?$query_string;
Replace the last location directive with this
location ~ /lara/(.*)$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME "/var/www/l4/public/index.php";
fastcgi_param REQUEST_URI /$1;
}
Restart nginx.
Now the why. I spotted a couple of mistakes with your nginx config. First, /index.php?$query_string in the try_files directive should be /lara/index.php?$query_string, otherwise nginx will try a request like http://app.dev/lara as /var/www/l4/angular/index.php?, which leads no where (unless you have an index.php there, and even is it will be served as text, not through fpm).
The second has to do with the location ~ ^/lara/(.*\.php)$ directive. I think restricting it to URIs that end with .php is wrong, because it won't work for http://app.dev/lara/x, which will make nginx only search for /var/www/l4/public/x, returning 404 of course. Changing the regex to ^/lara/(.*)$ should do the job of catching /lara/x. Now the fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; directive is erroneous because for http://app.dev/lara/x, SCRIPT_FILENAME is /var/www/l4/public/x/lara/x, and removing the $1 in the alias directive won't make it any better. Instead, make the fastcgi_param like this fastcgi_param SCRIPT_FILENAME "/var/www/l4/public/index.php";, remove the alias directive, it's useless now, then move include fastcgi_params; above the fastcgi_param so it won't override SCRIPT_FILENAME value.
Done? Not yet :). Trying /lara/x will show a Laravel routing error, because it tries to find the route lara/x instead of x, this is because you're including fastcgi_params. Just add fastcgi_param REQUEST_URI /$1; after SCRIPT_FILENAME param directive. Now it should be working fine. Don't forget to restart nginx :).
Related
In this nginx configuration:
server {
server_name site.example.com;
index index.html index.php;
location / {
root /projects/proj1/frontend;
}
location /api/v1.0/ {
root /projects/proj1;
try_files $uri /api/v1.0/index.php$is_args$args;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 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 $document_root$fastcgi_script_name;
}
}
}
Why every url that contains site.example.com is served by the first location, even site.example.com/api/v1.0/ ? Many thanks in advance.
EDIT: I've already tried to invert the order, so first /api/v1.0/ and second /, but with no luck.
EDIT2: Trying with curl the response is correct, but chrome keep using caching even in incognito...
That's the expected behavior in nginx. location / basically matches everything. If you want to match / only, use exact location: location = /
Based on this question How to install symfony2 app in a subdirectory in nginx
I've created symfony3 application that works in subdirectory called bcms4. I've manged to make php work with PHP-FPM but I have probelms with assets. When I want to GET asset it directs the request to app_dev and shows 404 because obviosly the path does not exist.
My question is how to make assets not to be proccesed by app_dev but downloaded as supposed?
So when I enter
test.localhost/s/asdfad -> it runs symfony
test.localhost/asdf -> it runs other app living in main dir
test.localhost/s/assets/css/test.css -> it will show file in directory /var/www/test.localhost/bcms4/web/assets/css/test.css
My nginx config:
server {
listen 80;
root /var/www/test.localhost;
index index.html index.htm index.php;
# Make site accessible from http://localhost/
server_name test.localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ ^/s(/.*)$ {
try_files /s/web$1 /web$1 #sf2dev =404;
}
location #sf2dev {
expires off;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/test.localhost/bcms4/web/app_dev.php;
fastcgi_param SCRIPT_NAME /s/app_dev.php;
fastcgi_param REQUEST_URI /s$1;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_intercept_errors on;
}
}
After hours of trying I've managed to figure it out with little hack.
This is what I've added to my config file
location ~ ^/s(/.*).\w{1,5}$ {
rewrite ^/s(/.*) /bcms4/web$1 break;
return 404;
}
It'll rewrite files that has prefix /s and extension to directory where they are actually.
Maybe it will help someone. I'll leave question open for a while maybe someone has better solution cause it's seems hacky for me.
I have the following Nginx configuration for forwarding requests to a PHP-FPM backend:
server {
...
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~* \.php$ {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
One specific route in the app needs a slightly longer php max_execution_time setting. I've configured this successfully and verified it works by setting a longer fastcgi_read_timeout in the above config.
However, I don't need this to be applied to every single route. I'm guessing I need a nested location somewhere but nothing I've tried seems to work!
The fastcgi_read_timeout directive does not appear to accept dynamic values, so a separate location block for the special route will be required. Looking at your configuration file, I assume the special route is a unique URI processed by the /index.php script. Something like this should work:
location ^~ /special/route/uri {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 100s;
}
You can use a prefix location with the ^~ modifier (as above) to override the regex location that usually processes PHP files. Alternatively, you can use a regex location, but place it above the existing regex location so that it takes precedence.
See this document for location syntax.
Nginx returns a 404 when I query for an URL with a "path info" appended after the script name, e.g. http://example.com/index.php/hello.
Here is my config:
server {
listen 80;
root #PROJECT_ROOT#/;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass unix:#PHP_FPM_SOCK#;
include fastcgi_params;
fastcgi_read_timeout 300s;
}
}
I don't get why \.php$ doesn't match that URL, and I've tried searching for similar problems but can't find anything useful.
Use
location ~ ^.+.php {
fastcgi_split_path_info ^(.+?.php)(/.*)$;
to match a .php in the uri split the parameters
This works for me:
location ~ \.php {
# Split the path appropriately
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# Work around annoying nginx "feature" (https://trac.nginx.org/nginx/ticket/321)
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
# Make sure the script exists.
try_files $fastcgi_script_name =404;
# Configure everything else. This part may be different for you.
fastcgi_pass localhost:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I can't figure out how to make the Nginx config file locate my Codeigniter app. Serving PHP on this server is not the problem b/c if I put a php file in my root directory, I can echo "hello world";.
Here's my Nginx config file which is nearly verbatim from this tutorial. Note that I've manipulated many of these parameters and none had an effext so I'm wondering whether I need to look beyond this file to get it to work?:
server {
listen 80;
server_name www.mysite.com mysite.com;
root /var/www;
index index.html index.php index.htm;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
I've been thinking all along this is a simple path issue b/c Codeigniter has a somewhat confusing routing structure but just can't see the problem with my config. Thoughts?
Everything up top matches what I have for a CI project, then this is my location config:
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Not sure if by removing the wildcard * would help, or getting rid of the split_info param...