【问题标题】:Nginx downloading filesNginx 下载文件
【发布时间】:2017-10-11 11:12:06
【问题描述】:

我有一个服务器(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 ^ ~ 的这个问题不起作用。

【问题讨论】:

  • 关于专业服务器或网络相关基础设施管理的问题对于 Stack Overflow 来说是无关紧要的,除非它们直接涉及编程或编程工具。您可以通过Server Fault 获得帮助。

标签: php nginx nginx-location php-5.4


【解决方案1】:

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

假设您想在 /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;
}

在服务 php 的两个/所有位置块上。

请记住,精确的位置匹配会胜过不太精确/几乎匹配的匹配。也许您对 /sys/something.php 的 GET 请求没有与 php-location 块匹配,而是与 /sys-location 块匹配。 sys-location 块有什么用,如果你不把东西放在那里,比如不同的根?

【讨论】:

  • 我对 /sys/ 文件夹的期望是,当尝试访问文件和子文件夹时,它不会返回文件下载。
  • 什么文件? php?
猜你喜欢
  • 2016-07-31
  • 1970-01-01
  • 2015-09-12
  • 2015-09-02
  • 1970-01-01
  • 2019-11-20
  • 2018-01-23
  • 2016-03-25
  • 1970-01-01
相关资源
最近更新 更多