How to redirect on the same port from http to https with nginx reverse proxy

后端 未结 5 1882
借酒劲吻你
借酒劲吻你 2020-12-24 01:12

I use reverse proxy with Nginx and I want to force the request into HTTPS, so if a user wants to access the url with http, he will be automatically redirected to HTTPS.

5条回答
  •  抹茶落季
    2020-12-24 01:50

    This worked for me:

    server {
    listen       80;
    server_name  localhost;
    ...
    if ($http_x_forwarded_proto = "http") {
          return 301 https://$server_name$request_uri;
    }
    location / {
        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;
        proxy_pass http://localhost:8080;
    }
    
    ...
    }
    

提交回复
热议问题