Skip to content

Instantly share code, notes, and snippets.

@Justintime50
Last active March 28, 2022 10:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Justintime50/af4f5666a8591a30d6dc354fbbddc7b3 to your computer and use it in GitHub Desktop.
Save Justintime50/af4f5666a8591a30d6dc354fbbddc7b3 to your computer and use it in GitHub Desktop.
Host Laravel in a Nested Subfolder/Directory - Nginx Configuration
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/site;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php_justinpaulhammond:9000; # I'm using Docker here, this is the name of my PHP container
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # We use document root here only for the root directory.
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location /blog {
alias /var/www/html/blog/laravel/public;
try_files $uri $uri/ @blog;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php_justinpaulhammond:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename; # We use SCRIPT_FILENAME here so routing works properly for the nested instance.
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
location @blog {
rewrite /blog/(.*)$ /blog/index.php?/$1 last; # We create a named location here to workout the routes properly.
}
}
@Justintime50
Copy link
Author

It's extremely important that all your Laravel routes use the url and route helper and don't have links hardcoded, otherwise this won't work. Eg: <a href="{{ route('my-route') }}">.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment