Skip to content

Commit 7284227

Browse files
🐛 Fixed 404s when using a proxy setup (#11269)
no issue When using certain proxy setups that result in `host` and `x-forwarded-host` being different, it became impossible to access Ghost because all routes showed generic 404 pages. - `vhost` module that we are using to separate front-end and admin urls does not use express' `req.hostname` so it does not pick up the `x-forwarded-host` url that express' `'trust proxy'` config gives us - switched to the forked `@tryghost/vhost-middleware` package which has a one-line change to use `req.hostname || req.host` - added `'trust proxy'` config to the admin express app and switched to using `req.hostname` in our redirect code to avoid infinite redirect loops
1 parent b1e20d2 commit 7284227

7 files changed

Lines changed: 23 additions & 10 deletions

File tree

core/server/web/admin/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ module.exports = function setupAdminApp() {
1111
debug('Admin setup start');
1212
const adminApp = express();
1313

14+
// Make sure 'req.secure' and `req.hostname` is valid for proxied requests
15+
// (X-Forwarded-Proto header will be checked, if present)
16+
adminApp.enable('trust proxy');
17+
1418
// Admin assets
1519
// @TODO ensure this gets a local 404 error handler
1620
const configMaxAge = config.get('caching:admin:maxAge');

core/server/web/parent-app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const debug = require('ghost-ignition').debug('web:parent');
22
const express = require('express');
3-
const vhost = require('vhost');
3+
const vhost = require('@tryghost/vhost-middleware');
44
const config = require('../config');
55
const compress = require('compression');
66
const netjet = require('netjet');

core/server/web/shared/middlewares/url-redirects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ _private.getBlogRedirectUrl = (options) => {
9696
*/
9797
_private.redirect = (req, res, next, redirectFn) => {
9898
const redirectUrl = redirectFn({
99-
requestedHost: req.get('host'),
99+
requestedHost: req.hostname,
100100
requestedUrl: url.parse(req.originalUrl || req.url).pathname,
101101
queryParameters: req.query,
102102
secure: req.secure

core/test/unit/web/middleware/url-redirects_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('UNIT: url redirects', function () {
1313

1414
beforeEach(function () {
1515
req = {
16-
get: function get() {
16+
get hostname() {
1717
return host;
1818
}
1919
};

core/test/unit/web/parent-app_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('parent app', function () {
3030

3131
parentApp = proxyquire('../../../server/web/parent-app', {
3232
express: expressStub,
33-
vhost: vhostSpy,
33+
'@tryghost/vhost-middleware': vhostSpy,
3434
'./api': apiSpy,
3535
'./admin': adminSpy,
3636
'./site': siteSpy,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@tryghost/social-urls": "0.1.4",
4747
"@tryghost/string": "^0.1.3",
4848
"@tryghost/url-utils": "0.6.11",
49+
"@tryghost/vhost-middleware": "1.0.0",
4950
"ajv": "6.10.2",
5051
"amperize": "0.6.0",
5152
"analytics-node": "3.3.0",
@@ -124,7 +125,6 @@
124125
"simple-html-tokenizer": "0.5.8",
125126
"uuid": "3.3.3",
126127
"validator": "6.3.0",
127-
"vhost": "3.0.2",
128128
"xml": "1.0.1"
129129
},
130130
"optionalDependencies": {

yarn.lock

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,15 @@
334334
remark "^11.0.1"
335335
unist-util-visit "^2.0.0"
336336

337+
"@tryghost/vhost-middleware@1.0.0":
338+
version "1.0.0"
339+
resolved "https://registry.yarnpkg.com/@tryghost/vhost-middleware/-/vhost-middleware-1.0.0.tgz#b9318b837054e87c35b27a31961847ad8bf70868"
340+
integrity sha512-F+rSk4YmquxjfSPvRc4s20NuOQfnQluK6drvttkPybKWTMoRvs5/uDfVdyil3lTVZC5COh7sSBCCM3Fin7FYGA==
341+
dependencies:
342+
bluebird "^3.7.1"
343+
ghost-ignition "^3.1.0"
344+
lodash "^4.17.15"
345+
337346
"@types/bluebird@^3.5.26", "@types/bluebird@^3.5.27":
338347
version "3.5.27"
339348
resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.27.tgz#61eb4d75dc6bfbce51cf49ee9bbebe941b2cb5d0"
@@ -914,6 +923,11 @@ bluebird@^3.0.5, bluebird@^3.4.1, bluebird@^3.4.3, bluebird@^3.4.6, bluebird@^3.
914923
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.0.tgz#56a6a886e03f6ae577cffedeb524f8f2450293cf"
915924
integrity sha512-aBQ1FxIa7kSWCcmKHlcHFlT2jt6J/l4FzC7KcPELkOJOsPOb/bccdhmIrKDfXhwFrmc7vDoDrrepFvGqjyXGJg==
916925

926+
bluebird@^3.7.1:
927+
version "3.7.1"
928+
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.1.tgz#df70e302b471d7473489acf26a93d63b53f874de"
929+
integrity sha512-DdmyoGCleJnkbp3nkbxTLJ18rjDsE4yCggEwKNXkeV123sPNfOCYeDoeuOY+F2FrSjO1YXcTU+dsy96KMy+gcg==
930+
917931
body-parser@1.19.0, body-parser@^1.19.0:
918932
version "1.19.0"
919933
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
@@ -8971,11 +8985,6 @@ vfile@^4.0.0:
89718985
unist-util-stringify-position "^2.0.0"
89728986
vfile-message "^2.0.0"
89738987

8974-
vhost@3.0.2:
8975-
version "3.0.2"
8976-
resolved "https://registry.yarnpkg.com/vhost/-/vhost-3.0.2.tgz#2fb1decd4c466aa88b0f9341af33dc1aff2478d5"
8977-
integrity sha1-L7HezUxGaqiLD5NBrzPcGv8keNU=
8978-
89798988
video-extensions@~1.1.0:
89808989
version "1.1.0"
89818990
resolved "https://registry.yarnpkg.com/video-extensions/-/video-extensions-1.1.0.tgz#eaa86b45f29a853c2b873e9d8e23b513712997d6"

0 commit comments

Comments
 (0)