I am trying to setup a Nginx server configuration to serve a CakePHP installation from and to a subfolder.
URL: https://sub.domain.com/cakefolder
Folder on system: /var/www/domain/sub/cakefolder
So i am using a sub folder for both the URL as well as on the system. Now it took me a while to figure the following config out with which requests are properly handled by CakePHP. This means it's correctly bootstraping and handling controllers.
What doesn't work however, is serving static files from the webroot directory (e.g. *.css files) as those are all interpreted as CakePHP controllers leading to a CssController could not be found. error.
My site.conf:
server {
listen *:80;
listen *:443 ssl;
server_name sub.domain.com;
ssl_certificate ./ssl/domain.crt;
ssl_certificate_key ./ssl/domain.key;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
if ($ssl_protocol = "") {
rewrite ^ https://$server_name$request_uri? permanent;
}
root /var/www/domain/sub/cakefolder/;
autoindex off;
index index.php;
location /cakefolder {
root /var/www/domain/sub/cakefolder/app/webroot/;
try_files $uri $uri/ /cakefolder/index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
How do I stop Nginx from routing existing static files through the FastCGI PHP interpreter?
Based on https://stackoverflow.com/a/22550332/671047 I already tried replacing my location /cakefolder { ... } with
location ~ /cakefolder/(.*) {
try_files /cakefolder/$1 /cakefolder/$1/ /cakefolder/index.php?$args;
}
but this leads to a redirection loop causing a HTTP 500 error.
Solution (thanks Pete!):
I found the following additional directive to be working for this specific setup. This might not be the most elegant solution but who cares, glad it's working for now.
location ~* /cakefolder/(.*)\.(css|js|ico|gif|png|jpg|jpeg)$ {
root /var/www/domain/sub/cakefolder/app/webroot/;
try_files /$1.$2 =404;
}
you could catch it early:
location ~* \.(css|js|ico)$ {
try_files $uri =404;
}
i have a similar setup and that worked for me when i experienced the same thing (just not cake.) I won't lie, i never understood why the try_files w/redirect always failed on existing static files, where as throwing a try_files like ^above finds the file np. ideas on that? (perhaps today is a source-reading day)
Related
I'm using a local nginx server for the first time to set up a website i'm building and i'm having trouble setting up the nginx config to handle url requests the way I want. My website serves multiple php pages as the user navigates through the website. When developing the site initially using a local php server, I used GET requests with window.location.href changes for site navigation. For example:
http://localhost:8000/shop.php?filter=all&sort=id_asc&page=3
However, since its going to be an ecommerce website for a small business, I wanted to handle the URLs in a cleaner and more professional manner.
My site structure looks something like this:
Website:
->index.php
->shop.php
->about.php
->product-page.php
->/css/
->/javascript/
->/php/
I want to configure nginx to route url paths in the following way
www.mywebsite.com -> routes to index.php
www.mywebsite.com/shop -> routes to shop.php
www.mywebsite.com/shop/anything -> routes to shop.php
www.mywebsite.com/about -> routes to about.php
www.mywebsite.com/product -> routes to product-page.php
www.mywebsite.com/product/anything -> routes to product-page.php
I've tried numerous suggestions over a couple of days before asking here but everything failed due to one reason or another, 404s, 500 internal errors, and redirect loops. I'm hoping to gain some inside here while I move onto other aspects of the site, so as to stop beating my head against the wall. Here is the state of my nginx conf at this moment:
server {
listen 80 ;
listen [::]:80 ;
server_name localhost;
root /var/www/html/reagansrockshop;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location = /shop {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index shop.php;
try_files $uri /shop.php;
}
location /shop/ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
try_files $uri /shop.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
How could I go about solving this? And if there is a better standard in structuring a website and its URLS please let me know. This is my first website and first time using nginx - so i'm a little naive on best practices.
If you need a certain php script to be responsible for a whole path, you need a config like this:
root /var/www/html/reagansrockshop; # root directive is necessary to define where '/' is
location /shop/ { # this means "all URLs starting with '/shop/' "
index /shop.php; # be careful with path to the file here
}
Although I would rather recommend a more traditional and cleaner project structure.
In your project root create two directories: shop and product. Move shop.php and product-page.php into designated folder and rename both to index.php. Your nginx config for this structure will be like this:
server {
listen 80 ;
listen [::]:80 ;
server_name localhost;
root /var/www/html/reagansrockshop;
index index.php index.html;
location / {
index index.php;
try_files $uri $uri/ =404;
}
location /shop/ {
try_files $uri $uri/ /shop/index.php?$args;
}
location /product/ {
try_files $uri $uri/ /product/index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I'm currently trying to setup a generic, multi-project development environment in Vagrant for students of a web-development mentoring project. The idea is the domain <project>.vagrant maps to ~/code/<project>
I thought I had enough experience with Nginx to solve this, but it turns out I don't.
Assuming that PHP-FPM is correctly setup, I need help with the try_files/routing for the site-configuration.
Whilst the homepage (/) works fine, any request to a non-static file (which should therefore be passed to PHP-FPM) results in either a 301 Moved Permanently to the homepage, or downloads the contents of the PHP script instead of executing it.
And yes I know listing so many index files is not ideal but the students will be dealing with multiple projects (phpMyAdmin, WordPress) and frameworks (Symfony, Silex, Laravel, etc).
Any help with this would be greatly appreciated!
The contents of the single site-available configuration file so far is:
map $host $projectname {
~^(?P<project>.+)\.vagrant$ $project;
}
upstream phpfpm {
server unix:/var/run/php5-fpm.sock;
}
server {
listen 80;
server_name *.vagrant;
server_tokens off;
root /home/vagrant/code/$projectname/web;
index app_dev.php app.php index.php index.html;
autoindex on;
client_max_body_size 5M;
location / {
try_files $uri $uri/ / =404;
}
# Pass all PHP files onto PHP's Fast Process Manager server.
location ~ [^/]\.php(/|\?|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
try_files $fastcgi_script_name =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Specify the determined server-name, not the literal "*.vagrant".
fastcgi_param SERVER_NAME $projectname.vagrant;
fastcgi_pass phpfpm;
}
}
I'm having an issue where when I go to the /public directory it shows the Laravel app as normal, but navigating away to any other page results in it saying
No input file specified.
I am using an Nginx server with PHP 5.5.9 FPM.
I've scoured google for the last 4 hours or so, looking at every tutorial and stackoverflow page for rewriting issues in Laravel however they all yield the same result.
I've even set all the files and folders to 777 so I could see if it was some sort of permissions issue. I've checked the Laravel config and it's all set, I've no idea what is wrong.
Can anyone point me in the right direction?
The last config I tried is below:
server {
listen 80 default_server;
root /usr/share/sites/base;
index index.php
server_name localhost;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
}
I have also tried many others such as:
server {
listen 80;
server_name domain.com;
root /usr/share/sites/base;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
location ~* \.php$ {
# Server PHP config.
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
The error "No input files specified" will nearly always be related to the fact that the wrong path was sent to php.
Looking at your 'last config tried' I can see that fastcgi_param SCRIPT_FILENAMEis not defined in your php location. You should first begin by defining it in the location :
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name
}
Furthermore you say that you can reach the app so this means that index.php is working but not when you change page. So the problem should also come from /index.php?$args. Indeed, using this line if I try to reach yourserver.com/test and if 'test' is not a file in your root path nginx will then try request /index.php? (I had this probem). You should try only with /index.php.
EDIT : The solution was that root directive should point to the Laravel public folder, in that case /usr/share/sites/base/public.
I can't wrap my head around Nginxs' try_files. Recently I installed Nginx in favour of Apache on my development machine. How I used to work was like this: I'd bootstrap a project in a subfolder of my http directory. If this involved laravel, wordpress, codeigniter or any other framework that's using a front controller to make URLs more readable, I'd add a .htaccess in that directory to rewrite all non-existing paths to index.php.
Supposedly, this is very simple on Nginx: try_files $uri $uri/ /index.php?$query_string and it should work. However, all this does is redirect everything to the index.php in the $document_root. Example:
File structure:
http/clients/cms/public/index.php front controller
http/clients/cms/public/some/application/url request uri with the parameters some, application and url.
http/clients/cms/public/images/image.png a static file.
http/clients/blog/index.php another front controller
http/clients/blog/wp-content/image.png another static file
http/clients/blog/some-article-title a 'pretty url' that should be directed to 4
Now, hen a request is made to 2, try_files detects that /some, /some/application and /some/application/url don't exist. It will now redirect to /index.php. I would expect that it would redirect to index.php in the public/ directory, but it doesn't. Instead, it redirects to the index.php in the $document_root (which happens to contain a phpinfo(); call, so it's easy to see that it goes wrong..).
A solution would be to create multiple location blocks in my Nginx config, but I'm not liking that. It would add a significant amount of extra work to my bootstrapping process.
Is there a way to use try_files in the directories so that it works exactly like the apache alternative?
My config file:
server {
listen 80;
server_name localhost,imac.local;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
rewrite_log on;
root /Volumes/Storage/Fabian/Dropbox/Sites;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
#echo $request_filename;
#break;
}
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;
}
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
}
thanks!
I'm having some issues getting a subdirectory working on my nginx server.
I'm using nginx to serve a wordpress installation as the web root, and trying to run an additional php application at a subdirectory. Wordpress runs fine, but I cannot for the life of me get the application to run in the subdirectory without a 404, 403, or "No input file specified." error with various configurations. I'm sure there is something obvious, but I can't seem to figure it out!
Here is the relevant config:
Any help would be greatly appreciated!
server {
listen myserver.edu:8081;
server_name myserver.edu:8081;
try_files $uri $uri/ /index.php;
location / {
root /path/to/nginx/html/wordpress;
index index.php;
}
location /stacks {
alias /another/path/to/usr/local/share/stacks/php;
index index.php;
}
location ~ \.php$ {
set $php_root /path/to/nginx/html/wordpress;
include fastcgi_params;
fastcgi_pass localhost:8082;
fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
}
location ~ \stacks.php$ {
set $php_root /another/path/to/usr/local/share/stacks/php;
include fastcgi_params;
fastcgi_pass localhost:8082;
fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
}
I don't know how to do it using your alias and setting $php_root. I do know how to fix it if you make a symbolic link from the external folder into your wordpress-rootdirectory.
So using the terminal you make a symbolic link so that your stacks-subdirectory is an actual subdirectory:
ln -s /another/path/to/usr/local/share/stacks/php /path/to/nginx/html/wordpress/stacks
As an nginx-config I would use
server {
listen myserver.edu:8081;
server_name myserver.edu:8081;
root /path/to/nginx/html/wordpress;
index index.php;
location / {
try_files $uri $uri/ /index.php;
}
location /stacks {
try_files $uri $uri/ /stacks/index.php;
}
location ~ \.php$ {
fastcgi_pass localhost:8082;
include fastcgi_params;
}
}
Comment out 'try_files'. Do the sub directories start to work then? Perhaps it is processed before the 'location' directives are considered. If that's the case, then move the 'try_files' into the block for 'location /'.
I think that's a better place for 'try_files' anyway. In the current configuration, it looks like requests for files that don't exist will all be sent to Wordpress, even if they are in the 'stacks' directory.