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"   https://www.somedomain.com/test_api     break;
    else
        rewrite "/test_api"   http://www.somedomain.com/test_api      break;
}

Thanks.

My answer:


You’re looking for $scheme.

rewrite /test_api $scheme://www.example.com/test_api break;

View the full question and answer on Server Fault.

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