Skip to content

Instantly share code, notes, and snippets.

@susanBuck
Last active February 16, 2024 21:19
Show Gist options
  • Save susanBuck/d6f450fd4485b1ea9aebeb73e6c937a1 to your computer and use it in GitHub Desktop.
Save susanBuck/d6f450fd4485b1ea9aebeb73e6c937a1 to your computer and use it in GitHub Desktop.
Run Laravel via subdirectories on a Nginx server
# ⭐⭐⭐
# Video explanation: https://youtu.be/SkpgZXDUHio
# Notes: https://codewithsusan.com/notes/run-laravel-apps-via-subdirectories-nginx
# ⭐⭐⭐
server {
# Listen for incoming requests on port 80 (default for HTTP traffic)
listen 80;
# Also listen on port 80 but for IPv6 connections
listen [::]:80;
# Domain to listen for traffic on
server_name yourdomain.com;
# The directory to serve from when we visit the root domain
root /var/www/main/public;
# Default file to serve when requesting a directory
index index.php index.html;
# How to handle requests to the root location (/)
# First try to serve the file or directory if found
# If not, rewrite the request to index.php (Laravel’s main controller)
# Note: This is only needed if your root domain is also serving a Laravel app
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# Handle requests for PHP files by passing them to PHP-FPM for processing
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
#
# Configs specific to our Laravel app running in the /admin subdirectory
#
location /admin {
# The alias directive allows for the replacement of the part of the path specified
# in the location directive with the path defined in the alias directive.
# i.e. makes it so location of /admin will alias to /var/www/admin/public
alias /var/www/admin/public;
# The try_files directive will first attempt to serve the requested URL path as a file,
# then as a directory then as our @admin named location (see below)
try_files $uri $uri/ @admin;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/admin/public/index.php;
}
}
location @admin {
# Take any requests that start with /admin/ and rewrite them to /admin/index.php (Laravel’s front controller)
rewrite /admin/(.*)$ /admin/index.php last;
}
#
# MISC
#
# Security headers to combat clickjacking and cross-site scripting attacks
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
location = /favicon.ico {
access_log off; log_not_found off;
}
location = /robots.txt {
access_log off; log_not_found off;
}
# When a 404 error occurs, don’t show default 404 error page, instead redirect to Laravel’s main controller
# So that a Laravel-specific 404 error can be shown
error_page 404 /index.php;
# NGINX will add the Content-Type header with the charset=utf-8 parameter in HTTP responses,
# which informs the client (like a web browser) that the server is sending data encoded in UTF-8.
# This helps ensure that text in the HTTP response is properly displayed to the user.
charset utf-8;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment