Nginx正在下载重写文件

I have tried the offered solutions:
Nginx rewrite triggers download
nginx rewrite to php file - File is being downloaded

but to no avail.

when I click on the link to /contact it just downloads my redirected file.

my config:

    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;
    }

when I visit /test it redirects successfully, but when I try and access /contact it fails. Accessing php directly works as expected.
I have also tried with the rewrites in the location / block, but I get the same error.

edit

I have taken out all my redirect rules, and it still downloads my index file.

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;
    }
}
dou1908
dou1908 default_typeapplication/octet-stream;强制下载它无法识别的mime类型。你确定你的PHP工作正常吗?
接近 7 年之前 回复
dpik71879
dpik71879 我已经尝试过,我得到同样的东西。编辑清楚
接近 7 年之前 回复
dse3168
dse3168 尝试将重写添加到您的位置块中。
接近 7 年之前 回复

2个回答

Well if the download is still happening then your php block has something wrong, And if you ask me it's too crowded, I always prefer the most minimal options

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: No need for The rewrites for /content and /test for now since the try_files will handle them.

Link about index in locations Multiple Index Directives




它仍会下载我的索引文件。</ p>
</ blockquote>

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

  location / {
index index.php index.html index.htm;
}
</ code> </ pre>

Nginx并没有通过它的处理规则神奇地传递一个带有.php扩展名的东西。</ p>

如果你想要通过所有 通过index.php请求你应该在try_files中包含默认位置。</ p>

  location / {
try_files $ uri /index.php = 404;

fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
包括fastcgi.conf;
}
</ code> </ pre>
</ div>

展开原文

原文

it still downloads my index file.

That's because you're telling it to download your index file here:

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

Nginx doesn't magically pass something has a .php extension back through it's processing rules.

If you want to pass all requests through index.php you should just included that in the try_files for the default location.

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

duanfeiqu1989
duanfeiqu1989 这个答案对我有用,可以阻止下载php文件。
2 年多之前 回复
dongli1920
dongli1920 不知道。 也许在你的nginx.conf中有其他影响它的东西,或者某个地方有一个奇怪的错字。
接近 7 年之前 回复
dqvs45976
dqvs45976 我明白你在说什么,但为什么它会适用于example.com/test,而不是example.com/contact
接近 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐