skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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;
        } 
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search