Nginx Installation Notes

Nginx is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. Unlike traditional servers, Nginx doesn’t rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load.

The PHP-FPM (FastCGI Process Manager) is usually used to allow Nginx to process PHP files. Nowadays, PHP-FPM is bundled with any Unix PHP distribution. Phalcon + Nginx + PHP-FPM provides a powerful set of tools that offer maximum performance for your PHP applications.

Configuring Nginx for Phalcon

The following are potential configurations you can use to setup nginx with Phalcon:

Basic configuration

Using $_GET[‘_url’] as source of URIs:

  1. server {
  2. listen 80;
  3. server_name localhost.dev;
  4. index index.php index.html index.htm;
  5. set $root_path '/var/www/phalcon/public';
  6. root $root_path;
  7. try_files $uri $uri/ @rewrite;
  8. location @rewrite {
  9. rewrite ^/(.*)$ /index.php?_url=/$1;
  10. }
  11. location ~ \.php {
  12. fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
  13. fastcgi_index /index.php;
  14. include /etc/nginx/fastcgi_params;
  15. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  16. fastcgi_param PATH_INFO $fastcgi_path_info;
  17. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  18. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  19. }
  20. location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
  21. root $root_path;
  22. }
  23. location ~ /\.ht {
  24. deny all;
  25. }
  26. }

Using $_SERVER[‘REQUEST_URI’] as source of URIs:

  1. server {
  2. listen 80;
  3. server_name localhost.dev;
  4. index index.php index.html index.htm;
  5. set $root_path '/var/www/phalcon/public';
  6. root $root_path;
  7. location / {
  8. try_files $uri $uri/ /index.php;
  9. }
  10. location ~ \.php$ {
  11. try_files $uri =404;
  12. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  13. fastcgi_pass 127.0.0.1:9000;
  14. fastcgi_index index.php;
  15. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  16. include fastcgi_params;
  17. }
  18. location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
  19. root $root_path;
  20. }
  21. location ~ /\.ht {
  22. deny all;
  23. }
  24. }

Dedicated Instance

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. charset utf-8;
  5. #access_log /var/log/nginx/host.access.log main;
  6. set $root_path '/srv/www/htdocs/phalcon-website/public';
  7. location / {
  8. root $root_path;
  9. index index.php index.html index.htm;
  10. # if file exists return it right away
  11. if (-f $request_filename) {
  12. break;
  13. }
  14. # otherwise rewrite it
  15. if (!-e $request_filename) {
  16. rewrite ^(.+)$ /index.php?_url=/$1 last;
  17. break;
  18. }
  19. }
  20. location ~ \.php {
  21. # try_files $uri =404;
  22. fastcgi_index /index.php;
  23. fastcgi_pass 127.0.0.1:9000;
  24. include fastcgi_params;
  25. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  26. fastcgi_param PATH_INFO $fastcgi_path_info;
  27. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  28. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  29. }
  30. location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
  31. root $root_path;
  32. }
  33. }

Configuration by Host

And this second configuration allow you to have different configurations by host:

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. set $root_path '/var/www/$host/public';
  5. root $root_path;
  6. access_log /var/log/nginx/$host-access.log;
  7. error_log /var/log/nginx/$host-error.log error;
  8. index index.php index.html index.htm;
  9. try_files $uri $uri/ @rewrite;
  10. location @rewrite {
  11. rewrite ^/(.*)$ /index.php?_url=/$1;
  12. }
  13. location ~ \.php {
  14. # try_files $uri =404;
  15. fastcgi_index /index.php;
  16. fastcgi_pass 127.0.0.1:9000;
  17. include fastcgi_params;
  18. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  19. fastcgi_param PATH_INFO $fastcgi_path_info;
  20. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  21. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  22. }
  23. location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
  24. root $root_path;
  25. }
  26. location ~ /\.ht {
  27. deny all;
  28. }
  29. }