您的位置:首页 > 运维架构 > Nginx

nginx 配置虚拟主机

2015-10-10 12:45 477 查看
server {
listen 80;
server_name www.test.com;
location / {
root /usr/share/nginx/html;#网站目录
index index.php index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;#注:fastcgi及监听的端口与php的cgi启动时要一致
fastcgi_index index.php;#默认首页
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}

}

server {
listen 80;
server_name www.xmarket.net.cn;
location / {
root /usr/share/nginx/work/shop;#网站目录
index index.php index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

location ~ \.php$ {
root /usr/share/nginx/work/shop;
fastcgi_pass 127.0.0.1:9000;#注:fastcgi及监听的端口与php的cgi启动时要一致
fastcgi_index index.php;#默认首页
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/work/shop$fastcgi_script_name;
include fastcgi_params;
}

}


在etc/hosts的文件里面添加
  127.0.0.1 www.test.com
  127.0.0.1 www.test.com

Nginx兼容ThinkPHP的URL重写和PATHINFO方法

ThinkPHP支持通过PATHINFO和URL rewrite的方式来提供友好的URL,只需要在配置文件中设置 'URL_MODEL' => 2 即可。在Apache下只需要开启mod_rewrite模块就可以正常访问了,但是Nginx中默认是不支持PATHINFO的,所以我们需要修改nginx.conf文件。

网上搜了很多方法都不奏效,研究了一天,发现通过以下的配置可以完美支持 'URL_MODEL' => 2 的情况了

server {
listen 80;
server_name www.test.com;
location / {
root /usr/share/nginx/html;#网站目录
index index.php index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;#注:fastcgi及监听的端口与php的cgi启动时要一致
fastcgi_index index.php;#默认首页
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}

}


这里先把project下的请求都转发到index.php来处理,亦即ThinkPHP的单一入口文件;然后把对php文件的请求交给fastcgi来处理,并且添加对PATH_INFO的支持。

重启Nginx以后,http://localhost/project/Index/insert, http://localhost/project/index.php/Index/delete 这样的URL都可以正确访问了。

还有一个地方需要注意的是,Nginx配置文件里 if 和后面的括号之间要有一个空格,不然会报nginx: [emerg] unknown directive "if"错误。

还有nginx版本要升级到nginx-1.1.16;我在nginx-0.9.5版本上就算加了空格也照样会提示上面if语法错误
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: