【发布时间】:2013-11-24 22:45:45
【问题描述】:
我已经尝试过提供的解决方案:
Nginx rewrite triggers download
nginx rewrite to php file - File is being downloaded
但无济于事。
当我点击指向/contact 的链接时,它只会下载我的重定向文件。
我的配置:
rewrite ^/test$ /index.php last;
rewrite ^/contact$ /index.php last;
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location / {
index index.php index.html index.htm;
}
当我访问 /test 时,它会成功重定向,但是当我尝试访问 /contact 时,它会失败。直接访问php按预期工作。
我也尝试过在 location / 块中进行重写,但我得到了同样的错误。
编辑
我已经删除了所有重定向规则,它仍然会下载我的索引文件。
nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
passenger_root /usr/lib/passenger/;
passenger_ruby /usr/local/rvm/wrappers/ruby-head/ruby;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server_names_hash_bucket_size 64;
#gzip on;
include /opt/nginx/conf/enabled/*.conf;
}
/opt/nging/conf/enabled/root.conf
server {
listen 80;
server_name example.com;
root /srv/http/public;
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location / {
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
【问题讨论】:
-
尝试将您的重写添加到您的位置块中。
-
我已经尝试过了,我得到了同样的结果。为清楚起见进行了编辑
-
default_type application/octet-stream;强制下载它无法识别的 mime 类型。你确定你的 PHP 工作正常吗?