Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CORS Rules for Laravel Valet Nginx
# To enable CORS you should add lines with CORS rules below to your valet.conf file
# Find the file /usr/local/etc/nginx/valet/valet.conf - this is Valet conf for Nginx
# of try to execute `locate valet.conf` and find the `valet.coinf` in `nginx` subdirectory
# after you edit your valet.conf do not forget to execute `valet restart`
server {
listen 80 default_server;
root /;
charset utf-8;
client_max_body_size 128M;
location /41c270e4-5535-4daa-b23e-c269744c2f45/ {
internal;
alias /;
# CORS Rules
add_header Access-Control-Allow-Origin *;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, OPTIONS, DELETE';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Authorization';
# END of CORS Rules #
try_files $uri $uri/;
}
location / {
# CORS Rules
add_header Access-Control-Allow-Origin *;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, OPTIONS, DELETE';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Authorization';
# END of CORS Rules #
rewrite ^ /Users/pavel/.composer/vendor/laravel/valet/server.php last;
}
access_log off;
error_log /Users/pavel/.valet/Log/nginx-error.log;
error_page 404 /Users/pavel/.composer/vendor/laravel/valet/server.php;
location ~ \.php$ {
# CORS Rules
add_header Access-Control-Allow-Origin *;
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, OPTIONS, DELETE';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Authorization';
# END of CORS Rules #
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/Users/pavel/.valet/valet.sock;
fastcgi_index /Users/pavel/.composer/vendor/laravel/valet/server.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /Users/pavel/.composer/vendor/laravel/valet/server.php;
}
location ~ /\.ht {
deny all;
}
}
@parse-code

This comment has been minimized.

Copy link

parse-code commented Oct 25, 2017

tips, after change config, can run valet restart to reload the config

@vwasteels

This comment has been minimized.

Copy link

vwasteels commented Mar 30, 2018

3h looking for this ! thanks !!

@ciprian-cimpan

This comment has been minimized.

Copy link

ciprian-cimpan commented Jun 22, 2018

Really useful - thanks for sharing!

@StefanNeuser

This comment has been minimized.

Copy link

StefanNeuser commented Aug 1, 2018

To load fonts its enough to set just

location /41c270e4-5535-4daa-b23e-c269744c2f45/ {
        internal;
        alias /;
        # CORS Rules
        add_header Access-Control-Allow-Origin *;
        # END of CORS Rules #
        try_files $uri $uri/;
    }
@AfikDeri

This comment has been minimized.

Copy link

AfikDeri commented May 15, 2019

Note that in newer versions of valet there is a separate config file for each site stored in
~/.config/valet/Nginx/your-site.test

@drazulay

This comment has been minimized.

Copy link

drazulay commented Jul 26, 2019

Doesn't work for me on valet-plus..

EDIT: it does! thanks!

@alancwoo

This comment has been minimized.

Copy link

alancwoo commented Nov 1, 2019

On Ubuntu 19.10 using valet linux v2.1.10, the config is in /etc/nginx/sites-enabled/valet.conf

This managed to fix an error I was having trying to use fetch to grab json from a valet site: TypeError: NetworkError when attempting to fetch resource.

@BrunoFenzl

This comment has been minimized.

Copy link

BrunoFenzl commented Jan 24, 2020

Awesome! thanks for sharing this. It worked perfectly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.