简体   繁体   中英

Add exception to nginx redirect https

I have the following config in my nginx's.conf file. I'm quite new to configuring web servers, so I'm facing some issues. Now if I understand correctly the first block redirects everything to 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. tag.

Now there's an exception, where I want to access a folder on the server without using 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. 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? 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. 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.

Then I realized I also host dnl files on my server, which I want the client to be able to download. 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.

I hope this helps others with similar situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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