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语法错误
相关文章推荐
- 【Nginx探究系列二】Nginx配置篇之客户Nginx白名单访问配置
- 为你的网站开启 gzip 压缩功能(nodejs、nginx)
- nginx 反向跳转
- NGINX 安装&配置
- 解决Nginx的connect() to 127.0.0.1:8080 failed (13: Permission denied) while connect
- centos 6.5 配置nginx环境
- window系统中利用dos 重启 nginx
- nginx设置代理配置
- nginx详解
- ubuntu下安装nginx
- How to Install Laravel with an Nginx Web Server on Ubuntu 14.04(Composer,laravel,nginx)
- nginx图片缓存和防盗链
- nginx伪静态rewrite规则写法实例
- windows下nginx安装、配置与使用
- LB集群之nginx
- NGINX开篇
- nginx编译安装和未编译模块的添加
- 源码编译Nginx&MySQL&PHP实现LNMP
- nginx各种常用的配置
- nginx不浏览直接下载文件