(Yii)使用nginx的配置

nginx的配置以下:php

upstream phpfpm {
   #server unix:/var/run/php5-fpm.sock;
   server 127.0.0.1:9000;
}css

server {
   listen       80;
   # Specify this vhost's domain name
   server_name lxy.me;
   root /home/lxy.me/public;
   index index.php index.html index.htm;html

# Specify log locations for current site
   access_log /home/lxy.me/log/access.log;
   error_log /home/lxy.me/log/error.log warn;nginx

# Typically I create a restrictions.conf file that I then include across all of my vhosts
   #include conf.d/restrictions.conf;git

# BEGIN restrictions.conf
   # Disable logging for favicon
   location = /favicon.ico {
       log_not_found off;
       access_log off;
   }web

# Disable logging for robots.txt
   location = /robots.txt {
       allow all;
       log_not_found off;
       access_log off;
   }緩存

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
   location ~ /\. {
       deny all;
       access_log off;
       log_not_found off;
   }
   # END restrictions.confapp

# Typically I create a yiiframework.conf file that I then include across all of my yii vhosts
   #include conf.d/yiiframework.conf;
   # I've included the content of my yiiframework.conf in-line for this example框架

# BEGIN yiiframework.conf
   # Block access to protected, framework, and nbproject (artifact from Netbeans)
   location ~ /(protected|framework|nbproject) {
       deny all;
       access_log off;
       log_not_found off;
   }dom

# Block access to theme-folder views directories
   location ~ /themes/\w+/views {
       deny all;
       access_log off;
       log_not_found off;
   }

# Attempt the uri, uri+/, then fall back to yii's index.php with args included
   # Note: old examples use IF statements, which nginx considers evil, this approach is more widely supported
   location / {
       try_files $uri $uri/ /index.php?$args;
   }
   # END yiiframework.conf

# Tell browser to cache image files for 24 hours, do not log missing images
   # I typically keep this after the yii rules, so that there is no conflict with content served by Yii
   location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
       expires 24h;
       log_not_found off;
   }

# Block for processing PHP files
   # Specifically matches URIs ending in .php
   location ~ \.php$ {
       try_files $uri =404;

# Fix for server variables that behave differently under nginx/php-fpm than typically expected
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       # Include the standard fastcgi_params file included with nginx
       include fastcgi_params;
       fastcgi_param  PATH_INFO        $fastcgi_path_info;
       fastcgi_index index.php;
       # Override the SCRIPT_FILENAME variable set by fastcgi_params
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       # Pass to upstream PHP-FPM; This must match whatever you name your upstream connection
       fastcgi_pass phpfpm;
   }
}



我曾經針對yii製做了 個nginx配置,其中包括瞭如下幾項內容:

  • rewrite規則(try_file),須要nginx0.8.6版本以上支持。

  • 針對於icon, robots.txt文件的日誌優化

  • .svn, .git,等版本控制文件的忽略,以及Mac自己索引文件目錄

  • Yii框架自己應該禁止web訪問的目錄。

  • 圖片等靜態文件緩存優化

在這裏分享一下demo

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
server {
     listen       80;
     server_name  youdomain.com;
     index index.html index.htm index.php;
     root  /home/wwwroot/htdocs/yii-1 .1.8.r3324 /demos/blog ;
     #charset koi8-r;
 
     # 這裏的main,是nginx默認的httpd段的一個日誌格式定義
     access_log  /home/wwwlogs/localhost .access.log  main;
     #error_page  404              /404.html;
 
     # redirect server error pages to the static page /50x.html
     #
     #error_page   500 502 503 504  /50x.html;
 
     location = /favicon .ico {
         log_not_found off;
         access_log off;
     }
 
     location = /robots .txt {
         allow all;
         log_not_found off;
         access_log off;
     }
 
     ################ Yii framework rule #################
     location / {
         try_files $uri $uri/ /index .php?$args;
     }
 
     location ~ /(protected|framework|nbproject|themes/\w+ /views |index- test \.php) {
         deny all;
         # for production
         internal;
         log_not_found off;
         access_log off;
     }
     ################ for Yii framework end #################
 
     location ~ \.php$ {
         fastcgi_pass   php;
         fastcgi_index  index.php;
         include fastcgi.conf;
     }
 
     # deny access to .htaccess files, if Apache's document root
     # concurs with nginx's one
     #
     location ~ /(\.svn|\.git|\.ht|\.DS) {
         deny all;
         internal;
     }
 
     location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
          expires max;
          log_not_found off;
     }
 
}
相關文章
相關標籤/搜索