Exception from localhost redirect to https?

In nginx I'm a novice. Please tell me how to configure nginx to redirect from http to https, but the redirect did not work if the request comes from the localhost (this is necessary for monitoring).
June 10th 19 at 15:54
2 answers
June 10th 19 at 15:56
Solution
server {
listen 127.0.0.1:80;
...
}

server {
listen 80;
return 301 https://$host$request_uri;
}

server {
listen 443 ssl;
...
}

I then the author of the question said that duplicate code is bad), well, here is a solution without duplication)
server {
 listen 80;
 listen 443 ssl;
 set $https "0";
 if ($scheme != "https") { set $https "A"; }
 if ($remote_addr != "127.0.0.1") { set $https "${https}B"; }
 if ($https = "AB") { rewrite ^ https://$http_host$request_uri? permanent; }
...
}

And, you can make a version with a map
map "$scheme:$remote_addr" $https {
 "https:*" 0;
 "http:127.0.0.1" 0;
 default 1;
}
server {
 listen 80;
 listen 443 ssl;
 if ($https) { rewrite ^ https://$http_host$request_uri? permanent; }
...
}


Another option is using include

server {
listen 127.0.0.1:80;
include vhost.conf;
}

server {
listen 80;
return 301 https://$host$request_uri;
}

server {
listen 443 ssl;
include vhost.conf;
}

Yes variations is actually a lot))
P. S. NOTE! The code was written on the knee and memory, check before using, but I think the goal, namely, to show the principle of use it performed.
June 10th 19 at 15:58
to make localhost in a separate location
UPD: I stepped, not location and server
so what?
server {
listen 80;
location localhost {
...
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
...
} - Fabiola_Run commented on June 10th 19 at 16:01
fucking stupid. in a separate section server - Franz.Waters16 commented on June 10th 19 at 16:04
,
server {
listen 127.0.0.1:80;
...
}

server {
listen 80;
return 301 <a href="https://$host$request_uri;">https://$host$request_uri;</a>
}

server {
listen 443 ssl;
...
}
- keara_Mosciski commented on June 10th 19 at 16:07
mark my answer with a solution if he came. - keara_Mosciski commented on June 10th 19 at 16:10
not note - Franz.Waters16 commented on June 10th 19 at 16:13
, soran, promedica)))) - keara_Mosciski commented on June 10th 19 at 16:16
almost came, but the whole zagvostka to localhost if a request comes in, then the site should open on a :80 port, if external, then passed the redirect is to :443. Duplicate code is not welcome. - Fabiola_Run commented on June 10th 19 at 16:19
the , uh, well, here it is. If external, it redirects to 443 if local 80)of Course you can write code with one unit server, what's the problem)) - keara_Mosciski commented on June 10th 19 at 16:22
copied response. Only I'll say this for Nginx is easier to handle so-called redundancy code than each time to check the conditions. Of course, no-load and normal power difference You feel. - keara_Mosciski commented on June 10th 19 at 16:25

Find more questions by tags Nginx