Using Alias ​​for Laravel Project in Nginx

Php
714
March 21, 2024
4 Minutes

Alias ​​is an Nginx configuration feature that allows a file or directory to be served to the server in a different path. Alias ​​can come in handy when you want to move Laravel projects in different directories or between servers.

server {
    listen 80;
    server_name example.com;
    root /var/www/html;

    location ~ /laravel { 
       alias /var/www/laravel/public; 
       try_files $uri $uri/ /index.php$is_args$args; 
#try_files /index.php =404;        fastcgi_pass unix:/run/php/php8.2-fpm.sock; fastcgi_index index.php; include fastcgi.conf; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }     # Other configuration settings... }

In the above configuration:

  • listen 80;: Specifies Nginx to listen on port 80.
  • server_name example.com;: Specifies the domain name of the server.
  • root /var/www/html;: Specifies the main web directory.
  • alias /var/www/laravel/public;: Specifies the home directory of the Laravel project.
  • try_files $uri $uri/ /laravel/index.php?$query_string;: It tells Nginx to perform Laravel redirect operations if the requested file or directory is not found.

You can access this page faster by scanning the QR code.