nginx 配置虚拟主机

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语法错误

 

转载于:https://www.cnblogs.com/yhdsir/p/4866511.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值