如何将error_page重定向到外部URL,并在Nginx中附加相应的方案?

时间:2017-02-13 11:53:13

标签: redirect nginx url-redirection

我在nginx配置中使用了以下指令来重定向到外部自定义URL。

<location block>
     :
   allow 123.123.123.123;
   deny all;
   error_page 403 http://domain.com/unauthorized.html;
     :
</location block>

它正在运行,但如果原始请求使用https方案,我想重定向到相应的https版本。

我的nginx服务器块正在为端口80和443共享配置。

server {
    listen 80;
    listen 443 ssl http2;
      :
      :
}

我尝试了以下但没有成功:

error_page 403 /unauthorized.html;https中向我提供了包含原始请求网址路径的自定义网页内容(这是我希望避免显示)。

error_page 403 //domain.com/unauthorized.html;向我提供404 Not Found中的默认https错误页面,与上面相同,在地址栏中显示原始请求网址路径。

我想:http(s)://domain.com/admin/admin.php -> http(s)://domain.com/unauthorized.html

如何在不进行冗余服务器块声明的情况下完成此操作?

1 个答案:

答案 0 :(得分:0)

$scheme变量设置为httphttps,具体取决于用于请求的协议。

allow 123.123.123.123;
deny all;
error_page 403 $scheme://domain.com/unauthorized.html;

有关详细信息,请参阅this document