douchuanghan1344 2014-01-14 20:40
浏览 81

NGINX显示脚本代码而不是执行

I have a troble with NGINX that makes me insane. I have Ubuntu 12.04.3 LTS on VirtualBox. I installed NGINX-MYSQL-PHP following these instructions:

  1. install php5-fpm;
  2. set cgi.fix_pathinfo=0 in /etc/php5/fpm/php.ini;
  3. set listen = var/run/php5-fpm.sock in /etc/php5/fpm/pool.d/www.conf

Then I set in /etc/nginx/sites-available/default:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Both localhost and info.php are executed properly. Now i want to set up a local website and try to make some redirect. So I created a new folder in /usr/share/nginx/www called mywebsite, put a index.php inside with a echo instruction, a php script i make redirect to, added a new config file in sites-available and a link to that in sites-enabled, and disable application/octet-stream in nginx.conf in order to avoid to downloading the script file. So i set my new config file as following:

server {
    listen   80;

    root /usr/share/nginx/www/mysite;
    index index.php index.html index.htm;

    server_name www.mysite.com;

    location / {
      try_files $uri $uri/ /index.html;
      if (!-e $request_filename){
        rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/$ /senseddata.php break;
        rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/lasthour/$ /senseddata.php break;
        rewrite ^/location/([0-9]+)/device/([0-9]+)/senseddata/daily/$ /senseddata.php break;
      }
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
       root /usr/share/nginx/www;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
       fastcgi_pass unix:/var/run/php5-fpm.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include fastcgi_params;
       try_files $uri =404;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
    deny all;
   }
}

So what happened after all? Index.php is executed properly. Rewrite is done well but the script is not executed. I only see the entire code i wrote displayed in the browser. What's the problem? Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongyixiu3110 2014-08-07 07:04
    关注

    Try add fastcgi_split_path_info ^(.+\.php)(/.+)$; to location ~ \.php$ and restart nginx.

    Example from my config

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?