Skip to content

Instantly share code, notes, and snippets.

@eldorplus
Last active January 14, 2021 17:21
Show Gist options
  • Save eldorplus/9d9a439ab91a65e2eebbac6ad63c0b9d to your computer and use it in GitHub Desktop.
Save eldorplus/9d9a439ab91a65e2eebbac6ad63c0b9d to your computer and use it in GitHub Desktop.
Homestead with CORS
#!/usr/bin/env bash
declare -A params=$6 # Create an associative array
paramsTXT=""
if [ -n "$6" ]; then
for element in "${!params[@]}"
do
paramsTXT="${paramsTXT}
fastcgi_param ${element} ${params[$element]};"
done
fi
block="server {
listen ${3:-80};
listen ${4:-443} ssl http2;
server_name .$1;
root \"$2\";
index index.html index.htm index.php;
charset utf-8;
# CORS support
add_header 'Access-Control-Allow-Origin' '\$http_origin';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, HEAD';
add_header 'Access-Control-Allow-Credentials' true;
add_header 'Access-Control-Allow-Headers' 'Origin,Content-Type,Accept,Authorization';
location / {
include fastcgi_params;
# CORS support
if (\$request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '\$http_origin';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, HEAD';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Access-Control-Allow-Credentials' true;
add_header 'Access-Control-Allow-Headers' 'Origin,Content-Type,Accept,Authorization';
add_header 'Content-Type' 'text/plain; charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
try_files \$uri \$uri/ /index.php?\$query_string;
}
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 /var/log/nginx/$1-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php$5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
$paramsTXT
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/nginx/ssl/$1.crt;
ssl_certificate_key /etc/nginx/ssl/$1.key;
}
"
echo "$block" > "/etc/nginx/sites-available/$1"
ln -fs "/etc/nginx/sites-available/$1" "/etc/nginx/sites-enabled/$1"
@magicspon
Copy link

life saver!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment