Skip to content

Instantly share code, notes, and snippets.

@Phally
Last active December 11, 2015 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phally/4541119 to your computer and use it in GitHub Desktop.
Save Phally/4541119 to your computer and use it in GitHub Desktop.
This gist describes briefly how to setup a virtual host for nginx that will be a project as a sub folder. Please note that you need a virtual host like this one for every project you have.
<?php
// In APP/Config/bootstrap.php add the following code:
if (($baseUrl = Configure::read('App.baseUrl')) !== false) {
Configure::write('App.base', $baseUrl);
}
<?php
// In APP/Config/core.php uncomment the line:
Configure::write('App.baseUrl', env('SCRIPT_NAME'));
# Author: Frank de Graaf (Phally)
# Date: 15-01-2013
# Location for the project.
location /myproject {
# Basic password protection.
auth_basic "Authorization required";
auth_basic_user_file htpasswd/myproject;
# The webroot of the project.
root /home/phally/projects/myproject/webroot;
# Strip a trailing slash if any and redirect.
rewrite (.+)/$ $1 permanent;
# Strip the sub folder name.
rewrite ^/myproject/(.+)$ /$1 break;
# For all other php files which aren't for the CakePHP
# core. Example: CakePHP test suite.
location ~ \.php$ {
# Strip the sub folder name.
rewrite ^/myproject/(.+)$ /$1 break;
# Pass it to PHP-FPM.
fastcgi_pass 127.0.0.1:9000;
# Include the fastcgi parameter commands.
include /etc/nginx/fastcgi_params;
# Prepend the sub folder name to the script name.
fastcgi_param SCRIPT_NAME /myproject$uri;
}
# Try static files first, else pass it on to CakePHP.
try_files $uri @myproject;
}
# Location for the CakePHP paths.
location @myproject {
# Pass it to PHP-FPM.
fastcgi_pass 127.0.0.1:9000;
# Include the fastcgi parameter commands.
include /etc/nginx/fastcgi_params;
# Always execute the index.php from the webroot.
fastcgi_param SCRIPT_FILENAME /home/phally/projects/myproject/webroot/index.php;
# Set the sub folder name as the script name.
fastcgi_param SCRIPT_NAME /myproject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment