如何使用 nginx 使 php 脚本可执行而不是可下载
how to make php scripts executable instead of downloadable using nginx
最近我通过 vagrant 设置了一个虚拟机,我使用 nginx 作为网络服务器。我用于 VM 的 OS 是 Ubuntu Linux 12.04。问题是我在 PHP 中编写的任何脚本都会被下载,而不是在浏览器上执行。看到 PHP 解释器正常安装,我想我会 post 这里是 nginx 的配置文件,因为这很可能是发现问题的地方。作为网络开发领域的新手,我无法弄清楚有什么不寻常的地方,你们能不能看看,如果看到什么不对的地方,请告诉我?谢谢
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/?(.*)$ / permanent;
location / {
index app.php;
try_files $uri @rewriteapp;
}
location @rewriteapp {
rewrite ^(.*)$ /app.php/ 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;
}
}
您的位置块中似乎有双 //
来指示 php 的 fastcgi_pass
路径,试试这个
location ~ \.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;
}
正如我所怀疑的那样,问题出在这个位置块中:
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;
}
location 指令的参数是一个正则表达式,它只定义了两个可能的文件名:app.php 和 app_dev.php,因此任何其他文件名都不可执行。要改变这一点,需要将一个 php 脚本的名称添加到参数中,或者根据 Frederic Henri 的建议将正则表达式更改为更具包容性的形式。
最近我通过 vagrant 设置了一个虚拟机,我使用 nginx 作为网络服务器。我用于 VM 的 OS 是 Ubuntu Linux 12.04。问题是我在 PHP 中编写的任何脚本都会被下载,而不是在浏览器上执行。看到 PHP 解释器正常安装,我想我会 post 这里是 nginx 的配置文件,因为这很可能是发现问题的地方。作为网络开发领域的新手,我无法弄清楚有什么不寻常的地方,你们能不能看看,如果看到什么不对的地方,请告诉我?谢谢
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/?(.*)$ / permanent;
location / {
index app.php;
try_files $uri @rewriteapp;
}
location @rewriteapp {
rewrite ^(.*)$ /app.php/ 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;
}
}
您的位置块中似乎有双 //
来指示 php 的 fastcgi_pass
路径,试试这个
location ~ \.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;
}
正如我所怀疑的那样,问题出在这个位置块中:
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;
}
location 指令的参数是一个正则表达式,它只定义了两个可能的文件名:app.php 和 app_dev.php,因此任何其他文件名都不可执行。要改变这一点,需要将一个 php 脚本的名称添加到参数中,或者根据 Frederic Henri 的建议将正则表达式更改为更具包容性的形式。