php-docker-template
php-docker-template copied to clipboard
Get rid of `if` usage in nginx config
We tried to get rid of the newly-introduced if statement inside the cors.conf in order to avoid if, since it is generally discouraged by nginx.
With the help of @stevenjm we came to the following:
# /etc/nginx/location.d-enabled/cors.conf
# Allow CORS if environment variable is set
add_header 'Access-Control-Allow-Origin' "http://127.0.0.1:8888" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' "*" always;
add_header 'Access-Control-Allow-Headers' "*" always;
add_header 'Access-Control-Expose-Headers' "*" always;
# /etc/nginx/conf.d/default.conf
map $request_method $rewrite_target {
default /index.php;
OPTIONS /NGINX_INTERNAL_HANDLE_OPTIONS;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
root /opt/project/public;
charset UTF-8;
include /etc/nginx/location.d-enabled/*.conf;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location / {
rewrite ^ $rewrite_target last;
}
location = /NGINX_INTERNAL_HANDLE_OPTIONS {
return 204;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $document_root;
}
}
The map could be moved into the cors.conf and that file could, for example, live in the conf.d directory, but I'll leave that to you guys to figure out.