I'am going crazy to understand this nginx vhost config. My issue is with the /v2 location, it does not send php stuff to php-fpm while it works properly outside /v2. Can anyone point me the mistake ?
server {
listen 443 ssl;
include ssl.conf;
include hardening.conf;
server_name myapp.domain.com myapp;
ssl_certificate /etc/pki/tls/certs/myapp.domain.com.crt;
ssl_certificate_key /etc/pki/tls/private/myapp.domain.com.key;
access_log /var/log/nginx/myapp.domain.com-access.log main;
error_log /var/log/nginx/myapp.domain.com-error.log notice;
root /var/www/html/myapp.domain.com;
location ~ /\.ht {
deny all;
}
location ~ /v2 {
alias /var/www/html/myapp.domain.com/version-2/web;
try_files $uri index.php$is_args$args;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass localhost:9000;
fastcgi_index index.php;
include fastcgi_params.conf;
}
}
According to comments, I'm trying the nested location solution but I receive now 404 when I try https://myapp.domain.com/v2/index.php while /var/www/html/myapp.domain.com/version-2/web/index.php is present on the filesystem. Also as explained on the link given, I modified my location from ^ to ^~. Any idea what's wrong?
server {
listen 443 ssl;
include ssl.conf;
include hardening.conf;
server_name myapp.domain.com myapp;
ssl_certificate /etc/pki/tls/certs/myapp.domain.com.crt;
ssl_certificate_key /etc/pki/tls/private/myapp.domain.com.key;
access_log /var/log/nginx/myapp.domain.com-access.log main;
error_log /var/log/nginx/myapp.domain.com-error.log notice;
root /var/www/html/myapp.domain.com;
location ~ /\.ht {
deny all;
}
location ^~ /v2 {
alias /var/www/html/myapp.domain.com/version-2/web;
try_files $uri index.php$is_args$args;
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass localhost:9000;
fastcgi_index index.php;
include fastcgi_params.conf;
}
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass localhost:9000;
fastcgi_index index.php;
include fastcgi_params.conf;
}
}
locationblocks to process the.phpURIs. Consider using a nestedlocationblocks - like this answer.$document_root$fastcgi_script_namedoes not work withalias, use:$request_filenameinstead. Also, you need to set a value forSCRIPT_FILENAME. Also, I would avoid usingtry_filesandaliastogether due to this issue.$document_root$fastcgi_script_nameby$request_filenameand replacingtry_filesby a rewrite rule also reording some stuff in the php location made it working.