【问题标题】:Nginx redirect to SSL except for certain URLNginx 重定向到 SSL,除了某些 URL
【发布时间】:2014-11-16 23:01:36
【问题描述】:

我想将到我网站的所有流量重定向到 SSL,但单个 URL 方案除外。我希望这个单一方案返回一个响应,表明该站点的这个特定部分只能通过 SSL 直接使用。

我网站的 conf 文件目前部分看起来像这样,可以很好地重定向所有流量:

server {
    listen 80;
    server_name sub.example.com;
    rewrite ^ https://$server_name$request_uri? permanent;
}

server {
    listen 443;
    ssl on;
    ssl_certificate /etc/nginx/ssl/12345.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;
    server_name sub.example.com;

    client_max_body_size 4G;

    # ....
}

我想这样做:

【问题讨论】:

    标签: ssl nginx


    【解决方案1】:

    将服务器级别的重写移动到根位置,然后为 API 生成错误代码,为该状态代码设置错误页面并将您的人类可读描述放在那里。 假设您返回 403,这似乎很合适,然后湿了这个:

    root /path/to/webroot;
    location / {
        return 301 https://$host$request_uri;
    }
    
    location /api/ {
        error_page 403 /api_direct.html;
        deny all;
    }
    

    我已经设置了root,以便可以找到自定义错误页面,根据您的需要进行调整。

    【讨论】:

      猜你喜欢
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 2017-07-11
      相关资源
      最近更新 更多