Problem redirecting http to https

Viewed 257

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;
        }
    }

But it’s strange. Look at the picture I just showed. Does it mean that answer system occupies 80? If answer system does not occupy 80, http cannot be accessed

What is the function of proxy_pass 9001? What is 9001 based on?

This is the port that answer is bound to, and you'll need to use it depending on what you're using.

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.

I feel like this is to redirect the external incoming 9001 to the answer container.