2013年7月9日星期二

Nginx alias try_files php-fpm work together

There is such a configuration on my Nginx 1.2.1 server:
server {
 listen 80 default_server;
 listen [::]:80 default_server ipv6only=on;
 root /var/www;
 index index.php index.html;
 autoindex on;
 autoindex_localtime on;
 try_files $uri $uri/ =404;
 location ~ ^/~([^/]+)(/.*)?\.php(/.*)?$ {
  alias /home/$1/public_html$2.php;
  fastcgi_pass unix:/var/run/php5-fpm.sock;
  fastcgi_index index.php;
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME /home/$1/public_html$2.php;
  fastcgi_param PATH_INFO $3;
 }
 location ~ ^/~([^/]+)(/.*)?$ {
  alias /home/$1/public_html$2;
 }
}
It aims to map /~username/ to /home/username/public_html/ so that users on my server can access and manage their own files.
However, this brought a problem. If one tried to access a non-exist .php file, such as /~username/asdfasdf.php, php-fpm would complain about “Invalid Argument” instead of displaying the “404 Not Found” page of Nginx.
I have browsed Nginx Wiki and found:
Note that there is a longstanding bug that alias and try_files don't work together.
But I would never give up!
I observed the environment variables that Nginx passed to PHP and found that $_SERVER["DOCUMENT_ROOT"] became the same value as $_SERVER["SCRIPT_FILENAME"] instead of /var/www.
try_files directive tries files from $document_root. For example, try_files $uri =404; tests the existence of the file named $document_root$uri.
It is clear after I noticed this. The workaround is just append try_files "" =404; after the line of alias.

没有评论:

发表评论