我正在尝试为具有大量自定义域名的多租户Saas设置Nginx代理服务器。我想要做的是创建一个服务器块,该服务器块可以处理以下所有请求,所有请求均为301永久性请求:
http://custom-domain.com到https://www.custom-domain.com(custom-domain.com可以是任何用户设置的域名)
从http://www.custom-domain.com到https://www.custom-domain.com(同样,任何域名)
http:// .saas-domain.com到https:// .saas-domain.com(saas-domain,com是我的服务的单个域名)
我目前正在使用一些If语句来处理此问题,但是它看起来很骇人,我希望以更有效的方式寻求帮助:
server {
listen 80 default_server;
location / {
# if 'www' redirect to https
if ($host ~* ^(www)) {
return 301 https://$host$request_uri;
}
# if '*.saas-domain.com' redirect to https://*.saas-domain.com
if ($host ~* ^(.*)\.saas-domain\.com) {
return 301 https://$host$request_uri;
}
# if not 'www' redirect to https and add 'www'
if ($host !~* ^(www)) {
return 301 https://www.$host$1 permanent;
}
}
}
这是处理我所有情况的最佳方法吗?我认为复杂之处在于通配符自定义域。我担心If语句的开销。TIA!
Nginx建议不要使用“ If”语句,除非您没有其他方法可以解决问题。我建议为您的域名添加单独的块,因为这将为您提供更大的灵活性。
请尝试以下操作,以查看是否有帮助。
# Capture requests that already have www and redirect to https
server {
listen 80;
server_name www.*;
return 301 https://$server_name$request_uri;
}
# Captures the saas-domain.com requests and redirects them
server {
listen 80 ;
server_name *.saas-domain.com;
return 301 https://$server_name$request_uri;
}
# Default capture everything else and redirect to https://www.
server {
listen 80 default_server;
server_name _;
return 301 https://www.$host$request_uri;
}
在生产中实施之前先进行测试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句