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>
pull/180/head
Alex Howes 2020-10-13 09:49:23 +01:00 committed by GitHub
parent 02e72a7003
commit 40aed64034
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 27 deletions

View File

@ -85,6 +85,36 @@ const listenConfig = 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) => {
// Use kv so we can use the same key multiple times
const config = [];
@ -311,35 +341,18 @@ export default (domain, domains, global) => {
// HTTP redirect
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
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;