Answer the question
In order to leave comments, you need to log in
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;
}
}
Page unavailable
example.com redirected too many times.
ERR_TOO_MANY_REDIRECTS
Answer the question
In order to leave comments, you need to log in
server {
server_name www.example.com;
listen *:80;
listen [::]:80 ipv6only=on;
if ($uri !~* ^/downloads/(.+)$) {
return 301 https://example.com/downloads/$1;
}
}
if
it 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;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question