nginx dynamic doute for laravel project use first param

Viewed 30

hi i am a laravel developer. working at envato. i have some laravel project . i would like to make a custom configuration for my laravel projects.

example

  1. http://demo.jubayed.com/demo1/

  2. http://demo.jubayed.com/demo2/

  3. http://demo.jubayed.com/demo3/

  4. http://demo.jubayed.com/demo4/

  5. http://demo.jubayed.com/demo5/

    root demo.jubayed.com/$demoProject/

server {
    listen 80;
    
    server_name demo.jubayed.com;

    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location /($demoProject)/ {
        try_files $uri $uri/ /index.php?$query_string;
        root /var/www/$demoProject/public;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    location = /sitemap.xml { access_log off; log_not_found off; }

    error_page 404 /index.php;
   # error_page 500 /index.php;
   # error_page 401 /index.php;
   # error_page 403 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
1 Answers

You may want to use alias instead of root as you need to manipulate the URI to insert public into the pathname. See this document for details.

You will also need to use a nested location for PHP as it needs access to the same pathname.

location ~ ^/(?<project>[^/]+)/(?<suffix>.*)$ {
    alias /var/www/$project/public/$suffix;

    if (!-e $request_filename) { rewrite ^ /$project/index.php last; }

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }

        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

I avoid using try_files and alias within the same block due to long term issues. See this caution on the use of if.

Related