【发布时间】: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