Nginx正在下载重写文件

时间:2013-11-22 14:36:23

标签: php nginx rewrite

我尝试过提供的解决方案:
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;
    }
}

2 个答案:

答案 0 :(得分:2)

如果下载仍在进行,那么你的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的关联

答案 1 :(得分:1)

  

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

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

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