J
J
John Freeman2019-01-09 14:30:04
Nginx
John Freeman, 2019-01-09 14:30:04

How to properly configure Nginx config?

Good afternoon!
Tell me how to properly configure the server config on Nginx, I need the site to work on https BUT so that everything in the downloads folder is downloaded via http (required via http, you can also use https), but so that the entire site works via https (redirect from http to https except for the files in the downloads folder), and also to make subdomains work, for example docs.example.com and telemirta.example.com, I set up the config as follows:

server {
    server_name          www.example.com;
    listen               *:80;
    listen               [::]:80 ipv6only=on;

    if ($uri !~* ^/downloads/(.+)$) {
        return 301 https://example.com/downloads/$1;
    }

}

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


    server_name example.com *.example.com;
    root /var/www/html/$subdomain;
    set $subdomain "";
    if ($host ~* ^([a-z0-9-\.]+)\.example.com$) {
        set $subdomain $1;
        return 301 https://$1.example.com$request_uri;
    }
    if ($host ~* ^example.com$) {
        set $subdomain "";
        return 301 https://example.com$request_uri;
    }

  index index.html index.htm index.nginx-debian.html index.php;

    if ($uri !~* ^/downloads/(.+)$) {
        return 301 https://example.com/downloads/$1;
    }

  location / {
    try_files $uri $uri/ =404;
  }

  location /munin/ {
          alias /var/cache/munin/www/;
  }


}

server {
  listen *:443 ssl;
  listen [::]:443;

    server_name example.com *.example.com;
    root /var/www/html/$subdomain;
    set $subdomain "";
    if ($host ~* ^([a-z0-9-\.]+)\.example.com$) {
        set $subdomain $1;
    }
    if ($host ~* ^example.com$) {
        set $subdomain "";
    }

index index.php index.html index.htm;
ssl_certificate /etc/ssl/example_com_crt.crt;
ssl_certificate_key /etc/ssl/example_com.key;

  location / {
    try_files $uri $uri/ =404;
  }
#rewrite ^/(downloads) http://$server_name$request_uri permanent;
rewrite ^/downloads/(.+)$ http://example.com/downloads/$1 permanent;

  location /munin/ {
          alias /var/cache/munin/www/;
  }

location ~* \.php$ { try_files $uri = 404; include fastcgi_params;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/run/php/php7.0-fpm.sock;
 }

}

the site itself works on https everything is ok, php works, subdomains work, but when I go to example.com/downloads/testarch.zip or example.com/downloads/testfi.amx I get a response from the browser
Page unavailable
example.com redirected too many times.
ERR_TOO_MANY_REDIRECTS

and if I try to go to example.com/downloads , then it redirects to https://example.com (home)
Please help me figure it out!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2019-01-09
@dodo512

server {
    server_name          www.example.com;
    listen               *:80;
    listen               [::]:80 ipv6only=on;

    if ($uri !~* ^/downloads/(.+)$) {
        return 301 https://example.com/downloads/$1;
    }

}

Here instead ifit is better to use location.
server {
    server_name  example.com  www.example.com;
    listen               *:80;
    listen               [::]:80 ipv6only=on;

    location / {
         return 301 https://example.com$request_uri;
    }
    
    location /downloads/ {
        root /var/www/html;
    }
}

A
Adamos, 2019-01-09
@Adamos

/*HTTP*/
    if ($uri !~* ^/downloads/(.+)$) {
        return 301 https://example.com/downloads/$1;
    }

/*HTTPS*/
    rewrite ^/downloads/(.+)$ http://example.com/downloads/$1 permanent;

You already decide where you want to send the user ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question