Nginx allows to process both HTTP and HTTPS within the same server
block. Thus you don't have to duplicate directives for both and can redirect the path you want to secure
server {
listen 80 default_server;
listen 443 ssl;
... ssl certificate and other configs ...
location /user {
if ($scheme = 'http') {
rewrite ^ https://$http_host$request_uri? permanent;
}
}
... your basic configuration ...
}
Be sure not to put ssl on
line there because it'll break plain HTTP.
Optionally, you can redirect all other requests from HTTPS back to HTTP the same way:
if ($scheme = 'https') {
rewrite ^ http://$http_host$request_uri? permanent;
}
UPDATE: as Alexey Ten kindly points out in comments section, checking scheme
on each request is not a very bright idea. You should follow declarative way of configuring your nginx. In this case, declare two server blocks with redirects by location
, move common logic to a separate file and include
it in both. So GruffTech's answer is better.