I have an app that is running on Nginx with a working server block like so:
server {
listen 80;
server_name example.com;
root /home/deployer/apps/my_app/current/;
index index.php;
location / {
index index.php;
try_files $uri $uri/;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/home/deployer/apps/shared/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /foo {
root /home/deployer/apps/modules/;
# tried this:
# alias /home/deployer/apps/modules/foo/;
# but php is not working with alias, only with root
}
}
When I visit /foo, Nginx looks in the path /home/deployer/apps/modules/foo/ for an index.php file and it works.
The problem:
I set up a deploy script using capistrano that deploys to the foo directory:
/home/deployer/apps/modules/foo/
Capistrano creates a 'current' directory within the 'foo' directory to contain the application files pulled in from Github, so I needed to change the root path to be:
/home/deployer/apps/modules/foo/current/
But Nginx appends the location directive to the end of the root directive.... so, when you visit /foo, Nginx tries to look in:
/home/deployer/apps/modules/foo/current/foo/
Using alias is supposed to disregard the /foo set in the location directive and serve files from the exact alias path (which the logs confirm is happening), but when I use the alias directive, the php configuration is not being applied correctly and I am getting a 404 returned.
If I go back to the root directive and remove the 'current' directory altogether, it works fine. I need the files to be served from the 'current' directory to work smoothly with the Capistrano deploy, but cannot figure out how to make the alias directive work with php.
Anyone have any ideas or advice, am I missing something?
thanks to #xavier-lucas for the suggestion about not being able to use try_files with alias.
To use alias with php, I had to remove the try_files directive from the php location block shown in the original question:
try_files $uri =404;
I actually had to restate the php location block within the /foo location and remove the above line. It ended up looking like this:
location /foo {
alias /home/deployer/apps/modules/foo/;
location ~ \.php$ {
# try_files $uri =404; -- removed this line
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/home/deployer/apps/shared/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
This allows php files to be processed directly from the directory listed in the alias directive.
Use
location /foo/ {
alias /home/deployer/apps/modules/foo/current/;
}
instead of
location /foo {
alias /home/deployer/apps/modules/foo/;
}
Related
I've got this really simple server block under sites-available.
Problem: When I try to access to mydomain.com, Nginx returns a « 404 Not Found », but if I try to access to a file in particular, it works fine, like mydomain.com/index.php
server {
listen 80;
index index.php;
server_name mydomain.com;
root /home/myusername/sites/mydomain.com/htdocs;
access_log /home/myusername/sites/mydomain.com/logs/access.log;
error_log /home/myusername/sites/mydomain.com/logs/error.log;
location / {
try_files $uri =404;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Note that:
my hosts file is configured ;
I restart Nginx after each edit ;
the access rights, user and group are correct ;
the error.log file is empty, the access.log returns me all the 404 ;
I tried to change the config by adding/removing some lines, still no changes ;
the site is enabled in sites-enabled with a correct symlink (I tried to edit it and it opened the right file) ;
I've got a few sites on the same server who runs well (so the including of sites-available and sites-enabled is OK, and Nginx works fine).
So, the answer was giver to me on ServerFault by Alexey Ten, here is a copy of the answer
Your try_files directive is too restrictive and, I guess, is in wrong place.
Either remove location / completely, it doesn't makes much sense, or, at least add $uri/ so index directive will work.
try_files $uri $uri/ =404;
But my guess is, you need to move this try_files into location ~ \.php$, this will make sure that php-file exsists before pass it to PHP-FPM for processing. All other files will be served by nginx with proper use of index directive.
server {
listen 80;
index index.php;
server_name mydomain.com;
root /home/myusername/sites/mydomain.com/htdocs;
access_log /home/myusername/sites/mydomain.com/logs/access.log;
error_log /home/myusername/sites/mydomain.com/logs/error.log;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
I just installed laravel 4.1 using the recommended install option.
composer create-project laravel/laravel your-project-name --prefer-dist
All went fine, as I was able to see the default page inside /public/ then I removed the .htaccess file since I am using nginx and added try_files $uri $uri/ /index.php?$query_string; for nginx inside the location ~ \.php$ { ... } then I created a simple route
Route::get('/about', function()
{
return "about page";
});
but I am getting the common 404 Not Found nginx/1.0.15 from nginx. I have given 777 permission even to the entire laravel folder. What could be the problem?
You need to point your nginx to the public folder. Here's one of my site config files in nginx:
server {
listen 80 default;
server_name _;
root /var/www/laravel/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
This way, you can browse to your laravel project by requesting: http://localhost/
Watch out: maybe you php cgi is different: use the fastcgi_pass rule you already had.
And readding the .htaccess file doesn't do any harm.
Hope this works for you.
I have mediawiki installed. All is right in the world except for when I try to alias a external directory (webalizer web stats). I see that Nginx passes off the request to /usage/* to PHP/Mediawiki. I don't want that. I literally want everything under /usage/ to point to my alias and nothing else. Completely separate from Mediawiki code and functionality.
# in no way related to Mediawiki. I just want to serve this as static HTML.
location /usage {
alias /var/www/webalizer/wiki.longnow.org/;
}
# This answers to anything, which may be my problem
location / {
try_files $uri $uri/ #rewrite;
index index.php;
}
# A special rewrite to play nicely with Mediawiki
location #rewrite {
rewrite ^/(.*)$ /index.php?title=$1&$args;
}
# PHP, nom nom nom
location ~ \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/tmp/php-fastcgi.socket;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
I was hoping that listing the /usage location directive ahead of the rest would short-circuit the system, but I have been spoiled by Django ;)
To stop Nginx from processing further location directives, it should be prefixed by ^~.
I think you will still want a try_files falling back to a 404 response inside the location.
location ^~ /usage {
alias /var/www/webalizer/wiki.longnow.org/;
try_files $uri $uri/ =404;
}
See http://wiki.nginx.org/HttpCoreModule#location for reference.
I have a hard time figuring out how try_files directive work.
This configuration works as expected if index file is used (http://domain.com/):
X-Fcgi-Script-Name /main_apps/domain.com/index.php
X-Script-Filename /var/www/main_apps/domain.com/index.php
However if I specify PHP script (index.php or other) try_files find file as expected, but take a look at variables:
X-Fcgi-Script-Name /index.php
X-Script-Filename /var/www/index.php
So my question is: How can I find which directory was picked by try_files directive?
Used server configuration:
server {
server_name ~^(?P<domain>.+)$;
root /var/www;
autoindex off;
index index.php;
try_files /main_apps/$domain$uri/ /partners/$domain$uri/ =404;
more_set_headers "X-Script-Filename: $request_filename";
more_set_headers "X-Fcgi-Script-Name: $fastcgi_script_name";
# Pass PHP scripts to PHP-FPM
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME $request_filename;
include php_fpm_params;
}
}
Note: Nginx I use is compiled with 3rd party module HeadersMore.
However if I specify PHP script (index.php or other) try_files find file as expected
You're wrong here. In this case your request handled by location ~ \.php$ where no try_files.
btw, server_name ~^(?P<domain>.+)$; is overkill. You should use $host variable.
What rule would I use for nginx so my default file extension is .php?
I currently access a pages using www.mywebsite.com/home.php but I want to just use www.mywebsite.com/home
Thanks
Assuming you also want to serve static files, you could use something like this:
server {
server_name example.com;
# Set the docroot directly in the server
root /var/www;
# Allow index.php or index.html as directory index files
index index.html index.php;
# See if a file or directory was requested first. If not, try the request as a php file.
location / {
try_files $uri $uri/ $uri.php?$args;
}
location ~ \.php$ {
# If the php file doesn't exist, don't pass the request to php, just return a 404
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass your_php_backend_address;
}
}