由于zf自带的工具,生成的项目都是基于apache的,而nginx和apache的htaccess(即Rewriter规则)又不一样
- server {
- listen 80;
- server_name servername.com;
- root /var/www/zendapp/public;
- location / {
- index index.php;
- }
- # Deny access to sensitive files.
- location ~ (\.inc\.php|\.tpl|\.sql|\.tpl\.php|\.db)$ {
- deny all;
- }
- location ~ \.htaccess {
- deny all;
- }
- # Rewrite rule adapted from zendapp/public/.htaccess
- if (!-e $request_filename) {
- rewrite ^.*$ /index.php last;
- }
- #图片/js/css不显示解决
- location ~* ^.+\.(js|ico|gif|jpg|jpeg|pdf|png|css)$ {
- access_log off;
- expires 7d;
- }
- # PHP scripts will be forwarded to fastcgi processess.
- # Remember that the `fastcgi_pass` directive must specify the same
- # port on which `spawn-fcgi` runs.
- location ~ \.php$ {
- include /etc/nginx/fastcgi_params;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- }
- location = /50x.html {
- root /var/www/default;
- }
- }
Nginx 不支持 Apache 的 .htaccess 文件,所以需要在 Nginx 配置文件中编写重写规则。Apache 的绝大部分 RewriteRule 命令都可以不做修改的放到 Nginx 中直接使用。你只要把 RewriteRule 改成 rewrite,[L] 改成 last 之类的就可以了,具体可以看一下 Nginx 的 Rewrite 文档。
Nginx部署ThinkPHP项目的办法网上通用解决方法的配置如下
- server {
- ...
- location / {
- index index.htm index.html index.php;
- #访问路径的文件不存在则重写URL转交给ThinkPHP处理
- if (!-e $request_filename) {
- rewrite ^/(.*)$ /index.php/$1 last;
- break;
- }
- }
- location ~ \.php/?.*$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- #加载Nginx默认"服务器环境变量"配置
- include fastcgi.conf;
- #设置PATH_INFO并改写SCRIPT_FILENAME,SCRIPT_NAME服务器环境变量
- set $fastcgi_script_name2 $fastcgi_script_name;
- if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
- set $fastcgi_script_name2 $1;
- set $path_info $2;
- }
- fastcgi_param PATH_INFO $path_info;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2;
- fastcgi_param SCRIPT_NAME $fastcgi_script_name2;
- }
- }
nginx多server
1.检查/etc/nginx/nginx.conf配置文件,确保文件中有:include /etc/nginx/conf.d/*.conf;
- user lg;
- worker_processes 2;
- error_log /var/log/nginx/error.log warn;
- pid /var/run/nginx.pid;
- events {
- worker_connections 1024;
- debug_connection 127.0.0.1;
- debug_connection 192.168.1.0/24;
- }
- http {
- include /etc/nginx/mime.types;
- # ......
- include /etc/nginx/conf.d/*.conf;
- }
2.关键步骤,在目录/etc/nginx/conf.d/下面新建文件site1.conf,site2.conf,文件名任意写,自己看明白就OK,后缀名需要与步骤1配置的一致,这里为.conf
Nginx多Server反向代理配置
在一个server块中配置多个站点
- server
- {
- listen 80;
- server_name ~^(www\.)?(.+)$;
- index index.php index.html;
- root /data/wwwsite/$2;
- }
站点的主目录应该类似于这样的结构:
- /data/wwwsite/ssdr.info
- /data/wwwsite/linuxtone.org
- /data/wwwsite/baidu.com
类似
- server_name http://www.*.yourdomain.com/;
- root /PATH/TO/WEBROOT/$host;
- location / {
- root /PATH/TO/WEBROOT/$host/;
- index index.php;
- }
- location ~ .php$ {
- fastcgi_param SCRIPT_FILENAME /PATH/TO/WEBROOT/$host/$fastcgi_script_name;
- }
在一个server块中为一个站点配置多个二级域名 。
实际网站目录结构中我们通常会为站点的二级域名独立创建一个目录,同样我们可以使用正则的捕获来实现在一个server块中配置多个二级域名:
- server
- {
- listen 80;
- server_name ~^(.+)?\.ssdr\.info$;
- index index.html;
- if ($host = ssdr.info){
- if ($host ~ ^(.*)\.ssdr\.info$ )
- set $domain $1;
- }
- root /data/wwwsite/ssdr.info/$domain/;
- }
站点的目录结构应该如下:
- /data/wwwsite/ssdr.info/www/
- /data/wwwsite/ssdr.info/nginx/
这样访问www.ssdr.info时root目录为/data/wwwsite/ssdr.info/www/,nginx.ssdr.info时为/data/wwwsite/ssdr.info/nginx/,以此类推。
后面if语句的作用是将ssdr.info的方位重定向到www.ssdr.info,这样既解决了网站的主目录访问,又可以增加seo中对www.ssdr.info的域名权重
通用配置
- server
- {
- listen 80;
- server_name *.dev.com;
- if ($host ~ ^(.*)\.dev\.com$ )
- {
- set $path /Users/apple/development/$1;
- set $webpath $path/trunk/htdocs;
- }
- root $path/trunk/htdocs;