Nginx 下载文件

Nginx downloading files

我有一台服务器 (nginx/1.6.0 PHP 5.5.14),这些设置可以正常工作。问题是当我使用 location ^~ /sys/ 当我尝试访问 sys 文件夹时,它会下载索引。如果我删除 location ^~ / sys / 恢复正常工作。错误只发生在 php 个文件中。 html 个文件正常工作。错误在哪里?

server {
    server_name  site.com;
    root /home/www/site.com;
    index index.php index.html index.htm;

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

    location ~ [^/]\.php(/|$) {
        fastcgi_pass unix:/data/php5514/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_read_timeout 300;
        include fastcgi_params;
    }

    location ^~ /sys/ {
    }
}

我正在使用脚本,需要在 nginx 中进行此设置以保护文件夹免遭未经授权的访问。

location ^~ /FOLDER/ {
    if ($cookie_amember_nr !~* [a-zA-Z0-9]+) { #not authorized
        rewrite ^(.*)$ http://EXAMPLE.COM/AMEMBER/protect/new-rewrite?f=FOLDERID&url=$request_uri?$args redirect;
    }
    set $file $document_root/AMEMBER/data/new-rewrite/$cookie_amember_nr-FOLDERID;
    if (!-f $file) { #have not access
        rewrite ^(.*)$ http://EXAMPLE.COM/AMEMBER/no-access/folder/id/FOLDERID?url=$request_uri?$args redirect;
    }    
    #everything is ok
}

但是location ^ ~这个问题不成立。

您应该更详细地解释您的实际期望。

假设你想在 /sys/ 提供 php 内容,你还必须将 fastcgi_pass 块放在这个位置上下文中,如下所示:

location ^~ /sys/ {
     fastcgi_pass unix:/data/php5514/var/run/php5-fpm.sock;
     fastcgi_index index.php;
     fastcgi_read_timeout 300;
     include fastcgi_params;
}

如果那是你想要的,你可能想要使用 upstream

upstream php { server unix:/data/php5514/var/run/php5-fpm.sock; }

并将其引用为

location ^~ /sys/ {
     fastcgi_pass http://php;
}

在 both/all 个位置块上服务 php。

请记住,准确的位置匹配胜过较少的 exact/almost 匹配。也许您对 /sys/something.php 的 GET 请求没有与 php-location 块匹配,而是与 /sys-location 块匹配。 无论如何,sys-location 块有什么用,如果你不把东西放在那里像不同的根?