简体   繁体   English

将异常添加到 nginx 重定向 https

[英]Add exception to nginx redirect https

I have the following config in my nginx's.conf file.我的 nginx's.conf 文件中有以下配置。 I'm quite new to configuring web servers, so I'm facing some issues.我对配置 web 服务器很陌生,所以我遇到了一些问题。 Now if I understand correctly the first block redirects everything to https.现在,如果我理解正确,第一个块会将所有内容重定向到 https。 The second block is the actual server config for example.com, while the third one redirects www.example.com to the second server block without the www.第二个块是实际的服务器配置,例如 com,而第三个将 www.example.com 重定向到没有 www 的第二个服务器块。 tag.标签。

Now there's an exception, where I want to access a folder on the server without using https.现在有一个例外,我想在不使用 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. 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. I tried adding我尝试添加

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

to all the server blocks, but none of them did the trick actually.到所有服务器块,但实际上它们都没有做到这一点。

I'd really appreciate any advice on how to solve the issue.我非常感谢有关如何解决该问题的任何建议。 Thanks in advance!提前致谢!

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

Did you try this?你试过这个吗? This will do an exact match.这将进行完全匹配。 Why do want to access without https?为什么要访问没有https? Not a good practice.不是一个好习惯。

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

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

So in the end I finally figured it out.所以最后我终于想通了。 I added the following to the top level server block:我将以下内容添加到顶级服务器块:

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

The first block "location /folder/page.php" blocks the redirect to the https site.第一个块“location /folder/page.php”阻止重定向到 https 站点。 But since I want to host scripts here I needed to add the relevant code so the link would actually execute the php script, and not just download it straight.但由于我想在此处托管脚本,我需要添加相关代码,以便链接实际执行 php 脚本,而不仅仅是直接下载。

Then I realized I also host dnl files on my server, which I want the client to be able to download.然后我意识到我还在我的服务器上托管了 dnl 文件,我希望客户端能够下载这些文件。 To solve this I added the second block "location ~.dnl$", which means that whenever a request ends in.dnl, the file will be looked up in the given folder and will be served as a downloadable content to the client, again without https.为了解决这个问题,我添加了第二个块“location ~.dnl$”,这意味着每当请求以 .dnl 结尾时,该文件将在给定文件夹中查找,并将再次作为可下载的内容提供给客户端不带 https。

I hope this helps others with similar situation.我希望这可以帮助其他有类似情况的人。

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

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