I'm experimenting with Nginx because it's faster than Apache. I struggle with the PHP setup though when using PHP-FPM in combination with an alias-location.
My test config looks somewhat like this (names changed and comments stripped):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
server { 	listen 80 default_server; 	listen [::]:80 default_server; 	server_name ... localhost; 	root /var/www; 	index index.php; 	 	location / { 		try_files $uri $uri/ =404; 	} 	location /customLocation/ { 		alias /pathOutsidevarwww/; 		 		location ~ \.php$ { 			include snippets/fastcgi-php.conf;	 		} 	} 	location ~ \.php$ { 		include snippets/fastcgi-php.conf; 	} 	location ~ /\.ht { 		deny all; 	} }
snippets/fastcgi-php.conf is a slightly modified version of the original fastcgi-php.conf shipped with Nginx 1.6.2 (added index and pass statements):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# regex to split $uri to $fastcgi_script_name and $fastcgi_path fastcgi_split_path_info ^(.+\.php)(/.+)$; # Check that the PHP script exists before passing it try_files $fastcgi_script_name =404; # Bypass the fact that try_files resets $fastcgi_path_info # see: http://trac.nginx.org/nginx/ticket/321 set $path_info $fastcgi_path_info; fastcgi_param PATH_INFO $path_info; include fastcgi.conf; fastcgi_index index.php; fastcgi_pass unix:/run/php/php7.0-fpm.sock;
The setup works fine for all PHP scripts in the root location. It also works fine for all other files which are no PHP scripts.
The problem is customLocation. The alias statement successfully reroutes normal file requests to the /pathOutsidevarwww/ location in the local file system when the path customLocation is opened in the browser. This however does not work for PHP files. I get a 404 error for them.
I googled a lot and also found a lot of different approaches but none seems to work for me. I'm very sure that something goes wrong with the path which is passed to the PHP-FPM when using alias but I simply don't manage to fix it.
Did anyone here encounter the same problem already and knows how to solve it?