nginx – rewrite to HTTP or HTTPS in nginx.conf file

ytw asked:

In nginx.conf file, is it possible to rewrite to HTTP or HTTPS based on whether the current connection is secured or not?

Some pseudocode of what I want to do.

location ^~ /test_api {
    if (secured connection)
        rewrite "/test_api"     break;
        rewrite "/test_api"      break;


My answer:

You’re looking for $scheme.

rewrite /test_api $scheme:// break;

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.