Nginx 安装说明(Nginx Installation Notes)

Nginx 是一个免费的,开源的高性能的HTTP服务器和反向代理服务器,同样也可作为IMAP/POP3代理服务器。不同于传统的服务器,Nginx 不依赖线程去处理请求。相反,它使用一个高扩展的事件驱动(异步)架构。 这种架构对系统资源的消耗很低,但更重要的是,承载了非常高的负载,并发能力很强。

PHP-FPM (FastCGI 进程管理器)通常用来允许 Nginx 来处理PHP文件。到了如今,PHP-FPM 已经捆绑在所有的PHP发行版中。Phalcon + Nginx + PHP-FPM 提供了一套强大的工具集,为你的PHP应用提供最强大性能。

Niginx 下配置 Phalcon(Configuring Nginx for Phalcon)

下面是nginx可以合Phalcon配合使用的大概配置:

基础配置(Basic configuration)

使用 $_GET[‘_url’] 作为 URLs的源:

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

使用 $_SERVER[‘REQUEST_URI’] 作为 URLs的源:

  1. server {
  2. listen 80;
  3. server_name localhost.dev;
  4. root /var/www/phalcon/public;
  5. index index.php index.html index.htm;
  6. charset utf-8;
  7.  
  8. location / {
  9. try_files $uri $uri/ /index.php;
  10. }
  11.  
  12. location ~ \.php$ {
  13. try_files $uri =404;
  14.  
  15. fastcgi_pass 127.0.0.1:9000;
  16. fastcgi_index /index.php;
  17.  
  18. include fastcgi_params;
  19. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  20. fastcgi_param PATH_INFO $fastcgi_path_info;
  21. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  22. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  23. }
  24.  
  25. location ~ /\.ht {
  26. deny all;
  27. }
  28. }

专属实例(Dedicated Instance)

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

使用 Host 配置(Configuration by Host)

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

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

原文: http://www.myleftstudio.com/reference/nginx.html