我知道这一定是已经回答了,但是我一直在寻找一段时间,找不到答案。 只是不在正确的地方,我想,也许有人可以帮助我。
基本上我通过SSL在非标准端口上运行phpmyadmin,在这个例子中是12345。
现在我有https://example.com:12345
设置好了,它的工作原理和一切。 现在我想添加inputhttp://example.com:12345
并redirect到https://的function。
我假设以下将工作,但事实并非如此。
server { listen 12345; server_name php.myadmin.com; if ( $scheme = http ) { rewrite ^ https://php.myadmin.com:12345$request_uri? redirect; } root /var/www/php; ssl on; [....] }
这给了我一个400 bad request
。
现在,在发布答案之前,请务必仔细阅读redirect表单。 检查链接下面的陷阱。
https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#taxing-rewrites
此外,如果甚至可以在没有if语句的情况下完成这将是很好的:
https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#using-if
我正是这样做的。 gWaldo是正确的,但你不想通过HTTP提供内容,只是一个redirect。 诀窍是捕获http-on-an-https-port错误,并使用该错误将客户端反弹到正确的位置。
server { listen 12345; server_name my.domain.com; ssl on; ssl_certificate /etc/ssl/certs/your.pem; ssl_certificate_key /etc/ssl/private/your.key; # If they come here using HTTP, bounce them to the correct scheme error_page 497 https://$server_name:$server_port$request_uri; # Or if you're on the default port 443, then this should work too # error_page 497 https://$server_name$request_uri; location / { # Your config here... } }
不幸的是,你不能用nginx单个端口来处理http和https请求 。
你可以做的是有一个代理预处理请求,但是。 这是我们如何处理scheme处理:
set $real_scheme http; if ($http_x_forwarded_proto) { set $real_scheme $http_x_forwarded_proto; } set $target_scheme https; if ($real_scheme != $target_scheme) { rewrite ^ $target_scheme://$host$request_uri redirect; }
您可以将访问端口80 redirect到端口443(以使用默认端口)。 但是这将需要你在该主机上运行iptables。
不知道是否你会得到一个400 ,因为SSL,但基本上是多么透明的代理工作。
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 443
除此之外,你可以redirect来自端口80的HTTP请求到端口443(或任何端口组合)在Nginx,但没有他们在同一个端口上工作,就像上面提到的。
编辑 :正如前面提到/评论下面这种方法将导致400由于协议混合。
这是否可以通过编程方式解决问题? 尝试在phpMyAdmin的启动文件顶部使用类似以下代码段的内容。 这应该将您的初始呼叫redirect到https。 之后,只要您将其configuration为通过SSL(当然)提供服务,那么您的页面的其余部分就在SSL上下文中。
<?php if ($_SERVER['SERVER_PORT']!=443) { $url = "https://". $_SERVER['SERVER_NAME'] . ":443".$_SERVER['REQUEST_URI']; header("Location: $url"); } ?>
(只要你的会话仍然有效,就不会明确地避免你跳入不带SSL的地方,例如通过书签)