Problem redirecting http to https

Viewed 192

Hi everyone, I want to redirect my website's http to https, but I'm running into a problem.

Listen 80 cannot be used in nginx because answer is occupied.
ee6b4728d8d8e03cded40b33c2c79ee.png

If answer does not use 80, such as 9001 or the like, http cannot be accessed.
What should be the correct configuration?

2 Answers

You can config reverse proxy rules in nginx.


server {
        listen       80;
        server_name  example.com;

        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		
        location / {
			proxy_pass http://127.0.0.1:9001;
        }
    }

I have solved it by adding a 301 redirect to http.

server {
    listen 80;
    server_name www.wenbihui.com wenbihui.com;
    rewrite ^(.*)$ https://$host$1 permanent;
}

then nginx -s reload.

It's so sad, even if I use the method above, there is still a problem, which will cause too many redirects and cannot be opened.
I think it might be a conflict with cloudflare?
There is no way, now my website only has http enabled, I will try again later.

It would be great if anyone could tell me how to configure it. My environment and needs are:

  1. I use docker , the port of answer is 9001:80
  2. I use nginx
  3. I expect to use both http and https
  4. I expect http to redirect to https.