首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当Hunchentoot在反向代理后面时如何重定向到HTTPS

当Hunchentoot在反向代理后面时如何重定向到HTTPS
EN

Stack Overflow用户
提问于 2022-04-29 18:46:36
回答 1查看 105关注 0票数 2

我在http://localhost:4242上运行了一个Hunchentoot应用程序。此应用程序使用非SSL受体(即hunchentoot:easy-acceptor而不是hunchentoot:easy-ssl-acceptor)。我将Nginx设置为反向代理,以便使用SSL为该应用程序服务。这是Nginx配置:

代码语言:javascript
复制
server {
    listen 443 ssl;
    server_name example.test;

    ssl_certificate /etc/my-ssl/certs/example.pem;
    ssl_certificate_key /etc/my-ssl/private/example.key;

    location / {
        include /etc/nginx/proxy_params;
        proxy_pass http://localhost:4242;
    }
}

/etc/nginx/proxy_params在哪里

代码语言:javascript
复制
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

问题是Hunchentoot不知道反向代理(即Nginx)实际上正在使用HTTPS为客户端服务。因此,(hunchentoot:redirect "/some-page/")将重定向到http://example.test/some-page/而不是https://example.test/some-page/。Hunchentoot不考虑X-Forwarded-Proto头。

为了解决这个问题,我编写了一个替代的重定向函数,而不是#'hunchentoot:redirect

代码语言:javascript
复制
(defun my-redirect (target
                    &rest args
                    &key (protocol
                           (if (string= (hunchentoot:header-in* :x-forwarded-proto)
                                        "https")
                             :https
                             :http))
                    &allow-other-keys)
  (apply #'hunchentoot:redirect target :protocol protocol args))

这是解决问题的正确方法吗?有没有更好的方法?

(Hunchentoot 1.3.0;SBCL 2.2.2;Nginx 1.18.0;Ubuntu 20.04)

编辑:在Hunchentoot的问题跟踪器:使用HTTPS反向代理服务应用程序时将错误重定向到HTTP

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-04 10:24:25

一个选项是将hunchentoot:easy-acceptor子类化,并为您的受体专门化acceptor-ssl-p方法。这将使您能够调用hunchentoot:redirect并让协议决策在幕后进行。

代码语言:javascript
复制
(defclass my-easy-acceptor (hunchentoot:easy-acceptor)
  ())

(defmethod hunchentoot:acceptor-ssl-p ((acceptor my-easy-acceptor))
  (string= (hunchentoot:header-in* :x-forwarded-proto) "https"))

但是,在我看来(从nginx配置中),您总是在为HTTPS服务--因此您可以省略逻辑并始终返回t

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72062476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档