1

I have website on port 8080 and I want to force https on it. ex. If I type http://mywebsite.com:8080, it should redirect to https://mywebsite.com:8080

How can I do this in nginx conf?

Dave M
  • 4,514
  • 22
  • 31
  • 30
k1ler
  • 23
  • 3
  • Does this answer your question? [In Nginx, how can I rewrite all http requests to https while maintaining sub-domain?](https://serverfault.com/questions/67316/in-nginx-how-can-i-rewrite-all-http-requests-to-https-while-maintaining-sub-dom) – Thomas Dec 11 '19 at 08:27

1 Answers1

3

Nginx will emit non-standard error code 497 which could be processed with error_page directive. So this would work:

server {
    listen 8080 ssl;

    error_page 497 @force_https;

    location @force_https {
        return 301 'https://$host:$server_port$request_uri';
    }
}

Works:

$ curl -I http://localhost:8080/test/me
HTTP/1.1 301 Moved Permanently
...
Location: https://localhost:8080/test/me
Alexey Ten
  • 8,435
  • 1
  • 34
  • 36