Forum

> > Off Topic > Nginx 1.6.2, PHP 7 FPM + alias location
Forums overviewOff Topic overviewLog in to reply

English Nginx 1.6.2, PHP 7 FPM + alias location

16 replies
To the start Previous 1 Next To the start

old Nginx 1.6.2, PHP 7 FPM + alias location

DC
Admin Off Offline

Quote
Hi,

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
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
# 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?

old Re: Nginx 1.6.2, PHP 7 FPM + alias location

DC
Admin Off Offline

Quote
Yes, that's among the things I already tried. Same problem.
What's wrong with nested locations? I think it's pretty straightforward in this case. The nesting is only for the PHP parsing.
edited 1×, last 21.07.16 01:14:53 pm

old Re: Nginx 1.6.2, PHP 7 FPM + alias location

oxytamine
User Off Offline

Quote
Have you tried to specify alias/root in nested location too? You should avoid nesting locations anyway.

user DC has written
The nesting is only for the PHP parsing.

1
2
3
location ~ /customLocation/(.+)\.php$ {
...
}
That would do.

old Re: Nginx 1.6.2, PHP 7 FPM + alias location

DC
Admin Off Offline

Quote
Don't locations inherit from their parent location anyway? So putting a root/alias in
/customLocation/
should automatically set the same root/alias for the nested
~ \.php$


The change you suggested works! Awesome, thanks
It's giving me a 500 error which I also got before with some other configuration and I thought it was the configuration but it actually was the PHP script in that place doing weird stuff
A simple hello world script works fine.

old Re: Nginx 1.6.2, PHP 7 FPM + alias location

Yates
Reviewer Off Offline

Quote
Just to make sure, you know of the depreciated and completely removed methods in the PHP 7 version apposed to 5.6?

By the way: Instead of PHP, you could even try using HHVM, the latest HHVM is even faster than PHP 7. HHVM is however stricter than PHP. So messy code will likely return errors.

old Re: Nginx 1.6.2, PHP 7 FPM + alias location

DC
Admin Off Offline

Quote
I prefer sticking to the "original" PHP for compatibility. The performance boost that already comes with PHP 7 (compared to previous versions) is more than enough

old Re: Nginx 1.6.2, PHP 7 FPM + alias location

DC
Admin Off Offline

Quote
Apache doesn't provide any features that I actually use that Nginx doesn't provide.

Why do you think that Apache is more stable than Nginx? I never heard of that. I always read the opposite and I know that popular websites rather use Nginx (or a completely different solution) than Apache which confirms that Nginx is more reliable and stable.

The only pro for Apache is actually that it is more used and therefore better supported and easier to set up.

old Re: Nginx 1.6.2, PHP 7 FPM + alias location

Infinite Rain
Reviewer Off Offline

Quote
@user DC:
Basically everyone I know says that Apache is more stable, all of them speak from their experience since I work in the IT field. There are a lot of pros to using Apache over Nginx, stability was always the selling point.

But again, it's their view. Fun thing I noticed is that the younger co-workers prefer Nginx while the more experienced ones go for Apache. But take everything with a grain of salt as I never got around using either of the aforementioned services for something permanent.

old Re: Nginx 1.6.2, PHP 7 FPM + alias location

Yates
Reviewer Off Offline

Quote
nginx is just as stable. Apache has only been around for longer thus many prefer to use what they have experience with. Everyone I know actually goes for nginx seeing as it's much faster for static content, can handle more requests and has an average faster response time. But you should usually create a server caching method to make nginx perform even better than Apache when not using static content with PHP.

Apache is good, relyable and fast. But nginx is just that little bit faster and configurable on any server which gives it that little more for me and many others to prefer it over Apache. It's also a lot better for working with docker containers as well (which is mostly used by people who host a vast amount of websites on the same server).

I work at a web/app development company which has years of work experience (mostly PHP) varying from 5 to 30 whom all recommend nginx as well.

Not everything made by Russians is unstable

old Re: Nginx 1.6.2, PHP 7 FPM + alias location

Infinite Rain
Reviewer Off Offline

Quote
@user Yates:
Thanks for explaining. I was just wondering since I'm not really aware of all the pros and cons, the ones I mentioned are just the biggest ones which rise a lot. Well, experience shall tell me.

But anyway, I think we're getting a bit offtopic now.

old Re: Nginx 1.6.2, PHP 7 FPM + alias location

oxytamine
User Off Offline

Quote
user Yates has written
By the way: Instead of PHP, you could even try using HHVM, the latest HHVM is even faster than PHP 7. HHVM is however stricter than PHP.

Completely off-topic, but checkout KPHP which is even faster. Keep in mind that it lacks OOP features which were cut out for performance.
To the start Previous 1 Next To the start
Log in to replyOff Topic overviewForums overview