Fix HTTP to HTTPS redirects for letsencrypt and subdomains (#178)
* Fix HTTP to HTTPS redirects for letsencrypt and subdomains * Update src/nginxconfig/generators/conf/website.conf.js * Update src/nginxconfig/generators/conf/website.conf.js Co-authored-by: Matt (IPv4) Cowley <me@mattcowley.co.uk>
This commit is contained in:
parent
02e72a7003
commit
40aed64034
|
@ -85,6 +85,36 @@ const listenConfig = domain => {
|
||||||
return httpListen(domain);
|
return httpListen(domain);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
const httpRedirectConfig = (domain, global, domainName, redirectDomain) => {
|
||||||
|
// Build the server config on its own before adding it to the parent config
|
||||||
|
const config = [];
|
||||||
|
|
||||||
|
config.push(...httpListen(domain));
|
||||||
|
config.push(['server_name', domainName]);
|
||||||
|
|
||||||
|
if (domain.https.certType.computed === 'letsEncrypt') {
|
||||||
|
// Let's encrypt
|
||||||
|
|
||||||
|
if (global.tools.modularizedStructure.computed) {
|
||||||
|
// Modularized
|
||||||
|
config.push(['include', 'nginxconfig.io/letsencrypt.conf']);
|
||||||
|
} else {
|
||||||
|
// Unified
|
||||||
|
config.push(...Object.entries(letsEncryptConf(global)));
|
||||||
|
}
|
||||||
|
|
||||||
|
config.push(['location /', {
|
||||||
|
return: `301 https://${redirectDomain ? redirectDomain : domainName}$request_uri`,
|
||||||
|
}]);
|
||||||
|
} else {
|
||||||
|
// Custom cert
|
||||||
|
config.push(['return', `301 https://${redirectDomain ? redirectDomain : domainName}$request_uri`]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
};
|
||||||
|
|
||||||
export default (domain, domains, global) => {
|
export default (domain, domains, global) => {
|
||||||
// Use kv so we can use the same key multiple times
|
// Use kv so we can use the same key multiple times
|
||||||
const config = [];
|
const config = [];
|
||||||
|
@ -311,35 +341,18 @@ export default (domain, domains, global) => {
|
||||||
|
|
||||||
// HTTP redirect
|
// HTTP redirect
|
||||||
if (domain.https.forceHttps.computed) {
|
if (domain.https.forceHttps.computed) {
|
||||||
// Build the server config on its own before adding it to the parent config
|
|
||||||
const redirectConfig = [];
|
|
||||||
|
|
||||||
redirectConfig.push(...httpListen(domain));
|
|
||||||
redirectConfig.push(['server_name',
|
|
||||||
`${domain.server.redirectSubdomains.computed ? '.' : ''}${domain.server.domain.computed}`]);
|
|
||||||
|
|
||||||
if (domain.https.certType.computed === 'letsEncrypt') {
|
|
||||||
// Let's encrypt
|
|
||||||
|
|
||||||
if (global.tools.modularizedStructure.computed) {
|
|
||||||
// Modularized
|
|
||||||
redirectConfig.push(['include', 'nginxconfig.io/letsencrypt.conf']);
|
|
||||||
} else {
|
|
||||||
// Unified
|
|
||||||
redirectConfig.push(...Object.entries(letsEncryptConf(global)));
|
|
||||||
}
|
|
||||||
|
|
||||||
redirectConfig.push(['location /', {
|
|
||||||
return: `301 https://${domain.server.wwwSubdomain.computed ? 'www.' : ''}${domain.server.domain.computed}$request_uri`,
|
|
||||||
}]);
|
|
||||||
} else {
|
|
||||||
// Custom cert
|
|
||||||
redirectConfig.push(['return', `301 https://${domain.server.wwwSubdomain.computed ? 'www.' : ''}${domain.server.domain.computed}$request_uri`]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the redirect config to the parent config now its built
|
// Add the redirect config to the parent config now its built
|
||||||
config.push(['# HTTP redirect', '']);
|
config.push(['# HTTP redirect', '']);
|
||||||
config.push(['server', redirectConfig]);
|
if (domain.server.wwwSubdomain.computed && !domain.server.redirectSubdomains.computed) {
|
||||||
|
config.push(['server', httpRedirectConfig(domain, global, domain.server.domain.computed, `www.${domain.server.domain.computed}`)]);
|
||||||
|
config.push(['server', httpRedirectConfig(domain, global, `www.${domain.server.domain.computed}`)]);
|
||||||
|
}
|
||||||
|
if (domain.server.cdnSubdomain.computed) {
|
||||||
|
config.push(['server', httpRedirectConfig(domain, global, `cdn.${domain.server.domain.computed}`)]);
|
||||||
|
}
|
||||||
|
if (domain.server.redirectSubdomains.computed) {
|
||||||
|
config.push(['server', httpRedirectConfig(domain, global, `.${domain.server.domain.computed}`, `${domain.server.wwwSubdomain.computed ? 'www.' : '' }${domain.server.domain.computed}`)]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
|
|
Loading…
Reference in New Issue