Last active
March 28, 2022 10:24
-
-
Save Justintime50/af4f5666a8591a30d6dc354fbbddc7b3 to your computer and use it in GitHub Desktop.
Host Laravel in a Nested Subfolder/Directory - Nginx Configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's extremely important that all your Laravel routes use the
url
androute
helper and don't have links hardcoded, otherwise this won't work. Eg:<a href="{{ route('my-route') }}">
.