I had the same problem and could not set environmental variables in nginx as easily as apache.
Actually your first solution does not work because the content "Additional Parameters for directive" is placed after this directive generated by MAMP.
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI_php7.2.1.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /Applications/MAMP/conf/nginx/fastcgi_params;
}
Your location directive is then ignored by nginx since it comes after the one generated by MAMP on the same location (.php$).
This is the workaround. MAMP recreates at every server startup the nginx.conf file. This is created using a template. You can actually modify the templates to generate a conf file the suits your needs.
This is the link to the MAMP documentation. https://documentation.mamp.info/en/MAMP-PRO-Mac/Menu/File/
I edited the nginx.cenf template to add the content of the "Additional Parameters for directive" box right before the creation of the location done by MAMP.
- Edit the nginx.conf in MAMP. File > Edit Template > nginx
- Locate the keyword MAMP_VirtualHost_AdditionalCustom_MAMP and remove it
Place the same directive just in front of this block
MAMP_VirtualHost_AdditionalCustom_MAMP
MAMP_Nginx_ReverseProxy_IF_MAMP
# proxy the PHP scripts to Apache
location ~ \.php$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://MAMP_Apache_IP_MAMP:MAMP_Apache_Port_MAMP;
}
MAMP_Nginx_ReverseProxy_ELSE_MAMP
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI_phpMAMP_PhpHost_MAMP.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /Applications/MAMP/conf/nginx/fastcgi_params;
}
MAMP_Nginx_ReverseProxy_END_MAMP
After restart for me it worked. You can fiddle around with a more sophisticated solution.