du1068 2013-11-22 14:36
浏览 156
已采纳

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;
    }
}
  • 写回答

2条回答

  • douyan8070 2013-11-24 14:45
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器