Laravel + Valet + Nginx internal redirection

The following config needs to be added to Laravel Valet nginx config file if you’d like nginx to do an internal redirection to a static asset stored in the public location in your storage folder.

This is especially useful for serving media assets like images that you’ve already generated before. Doing an internal redirect to nginx results in faster response times.

Add the following nginx config under the location block added by valet.

location ^~ /storage {
    alias /localpathtoyourproject/storage/app/public;
    expires 1y;
    add_header Cache-Control “public”;
    add_header ‘Access-Control-Allow-Origin’ ‘*’;
    add_header ‘Access-Control-Allow-Methods’ ‘GET, OPTIONS’;
    internal;
}

Before Updating Nginx Config:

server {
    listen 127.0.0.1:80;
    #listen 127.0.0.1:80; # valet loopback
    server_name smart-avatars.test www.smart-avatars.test *.smart-avatars.test;
    return 301 https://$host$request_uri;
}

server {
    listen 127.0.0.1:443 ssl http2;
    #listen 127.0.0.1:443 ssl http2; # valet loopback
    server_name smart-avatars.test www.smart-avatars.test *.smart-avatars.test;
    root /;
    charset utf-8;
    client_max_body_size 512M;
    http2_push_preload on;

    location /41c270e4-5535-4daa-b23e-c269744c2f45/ {
        internal;
        alias /;
        try_files $uri $uri/;
    }

    ssl_certificate "/Users/rajumandapati/.config/valet/Certificates/smart-avatars.test.crt";
    ssl_certificate_key "/Users/rajumandapati/.config/valet/Certificates/smart-avatars.test.key";

    location / {
        rewrite ^ "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php" last;
    }

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

    access_log off;
    error_log "/Users/rajumandapati/.config/valet/Log/nginx-error.log";

    error_page 404 "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php";

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass "unix:/Users/rajumandapati/.config/valet/valet.sock";
        fastcgi_index "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php";
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php";
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }
}

server {
    listen 127.0.0.1:60;
    #listen 127.0.0.1:60; # valet loopback
    server_name smart-avatars.test www.smart-avatars.test *.smart-avatars.test;
    root /;
    charset utf-8;
    client_max_body_size 128M;

    add_header X-Robots-Tag 'noindex, nofollow, nosnippet, noarchive';

    location /41c270e4-5535-4daa-b23e-c269744c2f45/ {
        internal;
        alias /;
        try_files $uri $uri/;
    }

    location / {
        rewrite ^ "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php" last;
    }

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

    access_log off;
    error_log "/Users/rajumandapati/.config/valet/Log/nginx-error.log";

    error_page 404 "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php";

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass "unix:/Users/rajumandapati/.config/valet/valet.sock";
        fastcgi_index "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php";
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php";
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }
}

After Updating Nginx Config:

server {
    listen 127.0.0.1:80;
    #listen 127.0.0.1:80; # valet loopback
    server_name smart-avatars.test www.smart-avatars.test *.smart-avatars.test;
    return 301 https://$host$request_uri;
}

server {
    listen 127.0.0.1:443 ssl http2;
    #listen 127.0.0.1:443 ssl http2; # valet loopback
    server_name smart-avatars.test www.smart-avatars.test *.smart-avatars.test;
    root /;
    charset utf-8;
    client_max_body_size 512M;
    http2_push_preload on;

    location /41c270e4-5535-4daa-b23e-c269744c2f45/ {
        internal;
        alias /;
        try_files $uri $uri/;
    }

    ssl_certificate "/Users/rajumandapati/.config/valet/Certificates/smart-avatars.test.crt";
    ssl_certificate_key "/Users/rajumandapati/.config/valet/Certificates/smart-avatars.test.key";

    location / {
        rewrite ^ "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php" last;
    }

    location ^~ /storage {
    	alias /Users/rajumandapati/Documents/Dev/GIT/smart-avatars/storage/app/public;
    	expires 1y;
    	add_header Cache-Control "public";
    	add_header 'Access-Control-Allow-Origin' '*';
    	add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
    	internal;
    }

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

    access_log off;
    error_log "/Users/rajumandapati/.config/valet/Log/nginx-error.log";

    error_page 404 "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php";

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass "unix:/Users/rajumandapati/.config/valet/valet.sock";
        fastcgi_index "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php";
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php";
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }
}

server {
    listen 127.0.0.1:60;
    #listen 127.0.0.1:60; # valet loopback
    server_name smart-avatars.test www.smart-avatars.test *.smart-avatars.test;
    root /;
    charset utf-8;
    client_max_body_size 128M;

    add_header X-Robots-Tag 'noindex, nofollow, nosnippet, noarchive';

    location /41c270e4-5535-4daa-b23e-c269744c2f45/ {
        internal;
        alias /;
        try_files $uri $uri/;
    }

    location / {
        rewrite ^ "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php" last;
    }

    location ^~ /storage {
    	alias /Users/rajumandapati/Documents/Dev/GIT/smart-avatars/storage/app/public;
    	expires 1y;
    	add_header Cache-Control "public";
    	add_header 'Access-Control-Allow-Origin' '*';
    	add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
    	internal;
    }

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

    access_log off;
    error_log "/Users/rajumandapati/.config/valet/Log/nginx-error.log";

    error_page 404 "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php";

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass "unix:/Users/rajumandapati/.config/valet/valet.sock";
        fastcgi_index "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php";
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME "/Users/rajumandapati/.composer/vendor/laravel/valet/server.php";
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }
}

In