【问题标题】:Nginx is dowloading the rewrite fileNginx 正在下载重写文件
【发布时间】: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 工作正常吗?

标签: php nginx rewrite


【解决方案1】:

如果下载仍在进行,那么你的 php 块有问题,如果你问我它太拥挤,我总是更喜欢最小的选项

server { 
    listen       80;
    server_name  example.com;
    root   /srv/http/public;
    #always place default index in server scope if it's common, check link below
    index index.php index.html index.htm;

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

    location ~ \.php$ {
        # check what name exists in your directory fastcgi.conf, or fastcgi_params
        include        fastcgi_params; 
        # make sure this path is correct, otherwise you'll get 502 error.
        fastcgi_pass   unix:/run/php-fpm/php-fpm.sock;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html; # < what is this relative to ?
    }
}

PS:暂时不需要重写/content/test,因为try_files 会处理它们。

Multiple Index Directives 位置链接关于index 的链接

【讨论】:

    【解决方案2】:

    它仍然会下载我的索引文件。

    那是因为你告诉它在这里下载你的索引文件:

    location / {
        index index.php index.html index.htm;
    }
    

    Nginx 不会神奇地将具有 .php 扩展名的东西通过其处理规则传回。

    如果您想通过 index.php 传递所有请求,您应该将其包含在默认位置的 try_files 中。

    location / {
        try_files      $uri /index.php =404; 
        fastcgi_pass   unix:/run/php-fpm/php-fpm.sock;
        include        fastcgi.conf;
    }
    

    【讨论】:

    • 我明白你在说什么,但为什么它会适用于 example.com/test,而不是 example.com/contact
    • 不知道。也许你的 nginx.conf 中有其他影响它的东西,或者某个地方有一个奇怪的错字。
    • 这个答案对我有用,可以阻止 php 文件下载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 2015-09-12
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多