douhuangjie4503
2016-05-20 09:47
浏览 338
已采纳

使用nginx在子目录中时不执行PHP

When going on example.com, example.com/foo/ or example.com/foo/bar/, PHP was working great. Then I tried to modify Nginx conf to have foo.example.com pointing on example.com/foo/ (the DNS is already configured). It works, but now when I access to foo.example.com/bar/, I can download foo/bar/index.php instead of executing it.

Here is the Nginx configuration:

server {
    listen         80;
    server_name    *.example.com;
    root /var/www/html/;


    location / {
        index index.html;
    }
}

server {
    listen         80;
    server_name    foo.example.com;
    root /var/www/html/foo/;

    location / {
        index index.php;
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass   web_fpm:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

I know there are plenty of similar threads, but none of those I read worked.

图片转代码服务由CSDN问答提供 功能建议

进入 example.com </ code>时, example.com/foo/ < / code>或 example.com/foo/bar / </ code>,PHP工作得很好。 然后我尝试修改Nginx conf以使 foo.example.com </ code>指向 example.com/foo / </ code>(DNS已经配置)。 它可以工作,但现在当我访问 foo.example.com/bar / </ code>时,我可以下载 foo / bar / index.php </ code>而不是执行它。</ p >

以下是Nginx配置:</ p>

  server {
 listen 80; 
 server_name * .example.com; 
 root / var /  www / html /; 
 
 
 location / {
 index index.html; 
} 
} 
 
server {
 listen 80; 
 server_name foo.example.com; 
 root /  var / www / html / foo /; 
 
 location / {
 index index.php; 
 try_files $ uri $ uri / /index.php?$args;
} 
 
 location~ \。  php $ {
 fastcgi_pass web_fpm:9000; 
 fastcgi_index index.php; 
 fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; 
包括fastcgi_params; 
} 
} 
 </ code> </ pre> 
 \  n 

我知道有很多类似的线程,但我读过的都没有。</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题