duanque3125 2015-08-30 21:11
浏览 60
已采纳

如何使PHP脚本可执行而不是使用nginx下载

Recently I set up a virtual machine through vagrant and I'm using nginx as a web server. The OS I'm using for the VM is Ubuntu Linux 12.04. The problem is that any script I write in PHP gets downloaded, instead of being executed on the browser. Seeing as the PHP interpreter is normally installed I figured I'd post here the config file of nginx, as this is where the problem is likely to be found. Being a novice in the world of web development, I can't figure out what is out of the ordinary, can you guys take a look and tell me if you see something wrong? Thanks.

server {
  listen 80;

  server_name localhost;
  root /vagrant/www/web;

  error_log /var/log/nginx/error.log;
  access_log /var/log/nginx/access.log;

  #strip app.php/ prefix if it is present
  rewrite ^/app\.php/?(.*)$ /$1 permanent;

  location / {
    index app.php;
    try_files $uri @rewriteapp;
  }

  location @rewriteapp {
    rewrite ^(.*)$ /app.php/$1 last;
  }

  # pass the PHP scripts to FastCGI server listening socket
  location ~ ^/(app|app_dev)\.php(/|$) {
    fastcgi_pass unix://var/run/php5-fpm.sock;
    fastcgi_keep_conn on;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  HTTPS              off;
  }

  # enable global phpMyAdmin
  location /phpmyadmin {
    root /usr/share/;
    index index.php index.html index.htm;
    location ~ ^/phpmyadmin/(.+\.php)$ {
      try_files $uri =404;
      root /usr/share/;
      fastcgi_pass unix://var/run/php5-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include /etc/nginx/fastcgi_params;
    }
    location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
      root /usr/share/;
    }
  }
  location /phpMyAdmin {
    rewrite ^/* /phpmyadmin last;
  }
}
  • 写回答

2条回答 默认 最新

  • duanjuelian4640 2015-08-31 17:38
    关注

    As was my suspicion the problem was in this location block:

    location ~ ^/(app|app_dev)\.php(/|$) {
        fastcgi_pass unix://var/run/php5-fpm.sock;
        fastcgi_keep_conn on;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  HTTPS              off;
      }
    

    The parameter to the location directive is a regular expression that defines only two possible file names: app.php and app_dev.php, thus making any other filename non-executable. For this to change, one needs to either add the names of one's php scripts to the parameter or change the regular expression to a more inclusive form as per Frederic Henri's suggestion.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型