【问题标题】:Redirect to separate URL if 403 or 404如果是 403 或 404,则重定向到单独的 URL
【发布时间】:2019-11-20 17:20:02
【问题描述】:

我有一个 Nginx 设置,其中包含一个包含白名单 IP 的文件,该文件可以访问我网站的管理门户 admin.site.com。如果未经授权的 IP 尝试访问我的管理门户,它通常会根据配置返回 403 禁止或 404,但我觉得这不是很好。 如果响应是 403 或 404,我需要将所有用户重定向到 site.com。这就是我所拥有的

server {
    # listen on port 80 (http)
    listen 80;
    server_name admin.site.com;

    location ~ /.well-known/acme-challenge {
        allow all;
        root /var/www/.well-known/acme-challenge/;
        default_type "text/plain";
        try_files $uri =404;
    }

    location / {
        # redirect any requests to the same URL but on https
        return 301 https://$host$request_uri;
    }
}

server {
    # listen on port 443 (https)
    listen 443 http2 ssl;
    server_name admin.site.com;

    root /var/www/admin.site.com/site-frontend/;
    index index.html;
        location / {
       include /etc/nginx/snippets/whitelist.conf;
       try_files $uri $uri/ /index.html;
    }

    # location of SSL certificate
    ssl_certificate /etc/some/path/admin.site.com/fullchain.pem;
    ssl_certificate_key /etc/some/path/admin.site.com/privkey.pem;
    ...
    ...
    ... 

    # write access and error logs to /var/log
    access_log /var/log/nginx/admin.site.com_access.log;
    error_log /var/log/nginx/admin.site.com_error.log;
}

这是负责检查某个 IP 是否在白名单中的位:

    index index.html;
        location / {
       include /etc/nginx/snippets/whitelist.conf;
       try_files $uri $uri/ /index.html;
    }

如果返回 403,我如何重定向到 site.com

【问题讨论】:

  • 您应该简单地返回 301 或 302 而不是 403,它会开箱即用。

标签: nginx nginx-reverse-proxy nginx-config


【解决方案1】:

您可以使用 error_page 指令将 403 和/或 404 状态页面重定向到外部站点。详情请见this document

例如:

error_page 404 403 =301 https://example.com/;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-16
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 2015-07-25
    • 1970-01-01
    • 2021-03-14
    相关资源
    最近更新 更多