由于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 {
# 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项目的办法网上通用解决方法的配置如下
...
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
在一个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_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;