如何使用Nginx反向代理在同一端口上从http重定向到https [英] How to redirect on the same port from http to https with nginx reverse proxy

查看:1128
本文介绍了如何使用Nginx反向代理在同一端口上从http重定向到https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将反向代理与Nginx结合使用,并且我想将请求强制进入HTTPS,因此,如果用户希望使用http访问网址,他将被自动重定向到HTTPS.

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.

我也在使用非标准端口.

I'm also using a non-standard port.

这是我的Nginx反向代理配置:

Here is my nginx reverse proxy config:

server {
    listen 8001  ssl;
    ssl_certificate /home/xxx/server.crt;
    ssl_certificate_key /home/xxx/server.key;
    location / {
        proxy_pass https://localhost:8000;
        proxy_redirect off;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Ssl on;
        proxy_set_header  X-Forwarded-Proto  https;
    }
}

我尝试了很多事情,还阅读了有关它的文章,包括

I've tried many things and also read posts about it, including this serverfault question, but nothing has worked so far.

推荐答案

找到了运行良好的内容:

Found something that is working well :

server {
        listen 8001  ssl;
        ssl_certificate /home/xxx/server.crt;
        ssl_certificate_key /home/xxx/server.key;
        error_page 497 301 =307 https://$host:$server_port$request_uri;
        location /{
            proxy_pass http://localhost:8000;
            proxy_redirect off;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Ssl on;
        }
}

这篇关于如何使用Nginx反向代理在同一端口上从http重定向到https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆