簡體   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