Nginx and Directory Aliases for PHP

In NGINX it's not as easy as in apache to define an alias for a directory for a virtual host, especially when you want to serve PHP or other FastCGI-Stuff. Here's how I made it work:

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
29
30
31
32
server {
  listen 80;
  root /var/www/cms/;
  index index.php index.htm index.html;

  # Make site accessible from http://localhost/
  server_name www.my.cms;

  access_log /var/log/nginx/ssl.www.my.cms.access.log;
  error_log /var/log/nginx/ssl.www.my.cms.error.log;

  location /phpmyadmin {
    alias /usr/share/phpmyadmin;
    location ~ .php$ {
      include /etc/nginx/fastcgi_params;
      fastcgi_index index.php;
      fastcgi_read_timeout 3600s;
      if (-f $request_filename) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
      }   
    }   
  }

  location ~ .php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_read_timeout 3600s;
    if (-f $request_filename) {
      fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
  }
}
Letzte Änderung: 2013