1

My current nginx.conf looks something like this:

server {
    listen      80;
    server_name www.example.com;

    root /var/www/;
    location / {
    }
    location /users {
        rewrite ^ https://$http_host$request_uri? permanent;
    }
}

server {
    listen      443 ssl;
    server_name www.example.com;

    root /var/www/;
    location /users {
    }
    location / {
        rewrite ^ http://$http_host$request_uri? permanent;
    }
}

With this configuration, the connection switches from/to ssl/non-ssl when a user navigates the pages on the site, ssl for urls started with /users and non-ssl for all other urls. As a result, even the user explicitly types https://www.example.com/ in the address bar of the browser, the resulting page is redirected to http://www.example.com/.

Is there a way to implement the auto-url-rewrite between ssl/non-ssl as achieved by the settings above, but still respect the explicit ssl request if https:// is explicitly typed by user in the browser's address bar?

4
  • 1
    Why would you do that - just force ssl when desired (or simply always, site wide) and never redirect to http. And before someone says seo: canonical URL metatag.
    – AD7six
    Jun 13, 2015 at 9:33
  • Never tried it but couldn't you use rewrite ^ $scheme://$http_host$request_uri?
    – Drifter104
    Jun 13, 2015 at 20:46
  • @MichaelLewis that's an infinite loop.
    – AD7six
    Jun 13, 2015 at 21:30
  • oh of course it would be
    – Drifter104
    Jun 13, 2015 at 22:19

1 Answer 1

1

From server standpoint there is no way to differ the request made by following a link, by crawling a site or by typing the full address in location bar.

When you type in an address, web browser sends HTTP request to your web server. If you typed https it will try to connect to port 443 (by default), and the request headers don't hold info if the user typed the request or the request or for example bot crawled it.

All you can do is keep SSL for certain user agents and redirect for other.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .