A
A
Andrey Tumanchik2017-02-01 11:15:58
Nginx
Andrey Tumanchik, 2017-02-01 11:15:58

How to set redirect to HTTPS in Nginx for all but some URIs?

Good afternoon.
Please help me write rewrite correctly.
I'm doing a 301 redirect from http to https. However, I need to exclude the following things from the redirect:
site.ru/monitoring
site.ru/automatic
site.ru/status/jnlp.php?id=789
POST requests come to the first two URIs, the last one should just open via HTTP.
Perhaps we should separate
location / {
}
location = /monitoring {
}
location = /automatic {
}
location = /status {
location ~ \.php$ {}
}
Please tell me where to dig, do I think in the right direction?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Korvyakov, 2017-02-09
@LNShadow

The best practics is to create a separate http-only server {} that handles redirects to http.
To exclude by uri, you should use map:

map $uri $need_redirect {
    "/monitoring"  0;
    "/automatic"  0;
    "~^/status/.*"  0;
    default    1;
}

server {
    server_name www.example.com;
    access_log      off;
    error_log       /dev/null crit;

    if ($need_redirect){
       rewrite  ^/(.*)$  https://$server_name/$1  permanent;
    }
}

A
Andrey Tumanchik, 2017-02-16
@jsand

Thanks for your reply.
Perhaps you have a typo.
"Best practics is to create a separate http-only server {} that redirects to http."
Does this mean
".....who redirects to http S "?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question