繁体   English   中英

将异常添加到 nginx 重定向 https

[英]Add exception to nginx redirect https

我的 nginx's.conf 文件中有以下配置。 我对配置 web 服务器很陌生,所以我遇到了一些问题。 现在,如果我理解正确,第一个块会将所有内容重定向到 https。 第二个块是实际的服务器配置,例如 com,而第三个将 www.example.com 重定向到没有 www 的第二个服务器块。 标签。

现在有一个例外,我想在不使用 https 的情况下访问服务器上的文件夹。 So if I type http://www.example.com/folder/page.php then the site should not be redirected to either https, nor to plain example.com without the www. 我尝试添加

location /folder/page.php
{
    http://www.example.com/folder/page.php
} 

到所有服务器块,但实际上它们都没有做到这一点。

我非常感谢有关如何解决该问题的任何建议。 提前致谢!

server
{
    listen 80;
    listen [::]:80;
    return 301 https://$host$request_uri;
}

server
{
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.com;

    root /data/web/example.com/web;

    location /phpmyadmin
    {
        root /usr/share;
        location ~ \.php$
        {
            include php.conf;
        }

        location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ { 
            root /usr/share/; 
        } 
    }

    location /webftp
    {
        root /data/web;
        location ~ \.php$
        {
            include php.conf;
        }
    }
}

server
{
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

你试过这个吗? 这将进行完全匹配。 为什么要访问没有https? 不是一个好习惯。

 server
  {
    listen 80;
    listen [::]:80;
    return 301 https://$host$request_uri;

    location=/folder/page.php
    {
      proxy_pass http://www.example.com/folder/page.php;
    } 
  }

所以最后我终于想通了。 我将以下内容添加到顶级服务器块:

server
{
    listen 80;
    listen [::]:80;

    location /folder/page.php {
         try_files $uri $uri/ =404;
         root /data/web/example.com/web;
         index index.php index.html index.htm default.html default.htm;
         include /etc/nginx/fastcgi_params;

         fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ \.dnl$ {
        root /data/web/example.com/web;
        try_files $uri $uri/ /index.php?$args;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

第一个块“location /folder/page.php”阻止重定向到 https 站点。 但由于我想在此处托管脚本,我需要添加相关代码,以便链接实际执行 php 脚本,而不仅仅是直接下载。

然后我意识到我还在我的服务器上托管了 dnl 文件,我希望客户端能够下载这些文件。 为了解决这个问题,我添加了第二个块“location ~.dnl$”,这意味着每当请求以 .dnl 结尾时,该文件将在给定文件夹中查找,并将再次作为可下载的内容提供给客户端不带 https。

我希望这可以帮助其他有类似情况的人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM