ssl - Nginx 配置 : if not iPhone/iPad then 301 redirect to https

标签 ssl nginx https safari ssl-certificate

我通过 startssl 认证将我的网站升级到 https。 但是 Safari 不信任 startssl 认证。 所以,我考虑设置nginx,如果是http,并且UserAgent不包含iPhone/iPad,那么301重定向到https。(意思是其他浏览器会301到https。) 这是我试过的:

server {
        listen       80;
        listen       443 ssl;
        server_name  abc.com alias abc.com;
        ssl_certificate      D:\cert\1_abc.com_bundle.crt;
        ssl_certificate_key  D:\cert\abc.com.key;
        ssl_session_timeout  5m;
        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers   on;
        if ($scheme = http) && if ($http_user_agent !~* iPhone|iPad) {
            return   301 https://$host$request_uri;
        }
        location / {
            root           D:/www;
            index  index.html index.htm default.html default.htm index.php;
            include        D:/www/up-*.conf;
        }

========或==========

if ($scheme = http && $http_user_agent !~* iPhone|iPad)

========或==========

但两者都没有效果。

最佳答案

if 指令只能包含简单的条件。参见 this document了解详情。

一种方法是将 httphttps 服务器分成单独的 server block 。

server {
    listen       80;
    server_name  abc.com alias abc.com;

    if ($http_user_agent !~* "iPhone|iPad") {
        return 301 https://$host$request_uri;
    }

    include path/to/common/config;
}

server {
    listen       443 ssl;
    server_name  abc.com alias abc.com;
    ssl_certificate      D:\cert\1_abc.com_bundle.crt;
    ssl_certificate_key  D:\cert\abc.com.key;
    ssl_session_timeout  5m;
    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

    include path/to/common/config;
}

它确实涉及在两个 server block 之间复制通用配置,但是,include 指令可用于将任何通用配置卸载到单独的文件中(如上所示)。

关于ssl - Nginx 配置 : if not iPhone/iPad then 301 redirect to https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41724280/

相关文章:

php - 由于路径,图像类功能无法正常工作

node.js - 请求 Github API 的 NodeJS 应用程序中的 408 超时

ssl - 链接具有相同主题的两个证书

带有或不带有 ssl 证书的 git

ruby-on-rails - 引发 HTTP 请求时 Rails 中的 OpenSSL::SSL::SSLError

php - 如何防止 Nginx 从 FastCGI/PHP-FPM 捕获错误?

c# - 具有使用 SSL 的上下文的 UserPrincipal

Nginx 子目录 404

apache - 每个带有 SSL 的 Apache 服务器的多个站点显示带有 HTTPS 的错误站点

ios - 如何使用swift绕过HTTPS服务器请求