Skip to content

Commit 04aab72

Browse files
authored
feat: allow nginx 500 error page to be disabled (#34)
Closes #31 This PR introduces a new cli command for controlling the visibility of **all** Valet's custom nginx error pages (**both 404 and 500**), updates nginx configuration handling to respect this setting, and enable the error pages by default. Additionally, it includes minor improvements to configuration management and code quality. **Nginx error page configurability:** * Added a new `nginx_error_page` configuration key (default `'on'`) to control whether Valet's nginx error page is enabled. This key will now be included for new configs, and for existing configs, it will automatically be added if it's missing via the Upgrader processes. * Updated Valet's main nginx config stub, and add logic to use the `nginx_error_page` setting in the `Nginx::installServer` method, allowing dynamic enabling or disabling of Valet's error pages. * Added a new `valet nginx-error-page` CLI command to display or update the visibility of the nginx error pages, including automatic nginx reload when changed. **Configuration and Upgrader:** * Extracted the "missing default config keys" code from `writeBaseConfiguration` into a new `addMissingDefaultConfigKeys` Configuration method for better maintainability and to enable it's usage in the Upgrader. * Added new `addMissingConfigKeys` Upgrader method to call the `addMissingDefaultConfigKeys` Configuration method during upgrades to add any missing default config keys to existing installations.
1 parent 8b83537 commit 04aab72

8 files changed

Lines changed: 107 additions & 7 deletions

File tree

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ composer global require ycodetech/valet-windows
138138
<td></td>
139139
<td align="center"><a href="#parity">parity</a></td>
140140
</tr>
141+
142+
<tr>
143+
<th></th>
144+
<td></td>
145+
<td></td>
146+
<td></td>
147+
<td></td>
148+
<td></td>
149+
<td align="center"><a href="#nginx-error-page">nginx-error-page</a></td>
150+
</tr>
141151
</table>
142152

143153
## Introduction
@@ -1421,6 +1431,23 @@ Parity at 87% out of a total 92% possible parity with the Laravel Valet (macOS)
14211431

14221432
This command is a way to determine how much parity has been achieved.
14231433

1434+
##### nginx-error-page
1435+
1436+
```
1437+
nginx-error-page Display the current visibility of the nginx error page.
1438+
[visibility] Optionally, set to "on" to enable Valet's nginx error page, or "off" to disable it.
1439+
```
1440+
1441+
```console
1442+
$ valet nginx-error-page
1443+
Valet's nginx error pages are on.
1444+
1445+
$ valet nginx-error-page off
1446+
Valet's nginx error pages are now off.
1447+
```
1448+
1449+
The nginx error pages are `on` (enabled) by default, which means that Valet's custom nginx 404/500 error pages will be shown when an error is encountered. However, for those who prefer to use a framework's error reporting exception pages like that of Symfony or Laravel, Valet's nginx error pages can be turned `off` (disabled).
1450+
14241451
---
14251452

14261453
### Notes for all `--options`

cli/Valet/Configuration.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,31 @@ public function createXdebugDirectory() {
149149
* Write the base, initial configuration for Valet.
150150
*/
151151
public function writeBaseConfiguration() {
152+
// If the configuration file doesn't exist, create it with the base configuration.
152153
if (!$this->files->exists($this->path())) {
153154
$baseConfig = [
154155
'tld' => 'test',
155156
'paths' => [$this->valetHomePath('Sites')],
156157
'php_port' => PhpCgi::PORT,
157158
'php_xdebug_port' => PhpCgiXdebug::PORT,
158-
'share-tool' => 'ngrok'
159+
'share-tool' => 'ngrok',
160+
'nginx_error_page' => 'on'
159161
];
160162

161163
$this->write($baseConfig);
162164
}
163165

166+
// If the configuration file exists, ensure it has all the necessary keys.
167+
$this->addMissingDefaultConfigKeys();
168+
}
169+
170+
/**
171+
* Add any missing necessary default configuration keys.
172+
*
173+
* It will not overwrite any existing configuration values,
174+
* only add missing keys with default values.
175+
*/
176+
public function addMissingDefaultConfigKeys() {
164177
$config = $this->read();
165178

166179
// Add default_php if missing or is null.
@@ -176,6 +189,8 @@ public function writeBaseConfiguration() {
176189
$this->updateKey('php_xdebug_port', $config['php_xdebug_port'] ?? PhpCgiXdebug::PORT);
177190
// Add share-tool if missing.
178191
$this->updateKey('share-tool', $config['share-tool'] ?? 'ngrok');
192+
// Add nginx_error_page if missing.
193+
$this->updateKey('nginx_error_page', $config['nginx_error_page'] ?? 'on');
179194
}
180195

181196
/**

cli/Valet/Nginx.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,14 @@ public function installServer() {
8888

8989
$valetErrorTemplatePath = $this->files->realpath(valetBinPath() . '../cli/templates');
9090

91+
// Check if nginx error pages are disabled
92+
$interceptErrors = $this->configuration->get('nginx_error_page');
93+
9194
$this->files->putAsUser(
9295
$this->path('valet/valet.conf'),
9396
str_replace(
94-
['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'HOME_PATH', 'VALET_PHP_PORT', 'VALET_ERROR_TEMPLATE_PATH'],
95-
[Valet::homePath(), VALET_SERVER_PATH, VALET_STATIC_PREFIX, $_SERVER['HOME'], $defaultPhp['port'], $valetErrorTemplatePath],
97+
['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'HOME_PATH', 'VALET_PHP_PORT', 'VALET_ERROR_TEMPLATE_PATH', 'VALET_INTERCEPT_ERRORS'],
98+
[Valet::homePath(), VALET_SERVER_PATH, VALET_STATIC_PREFIX, $_SERVER['HOME'], $defaultPhp['port'], $valetErrorTemplatePath, $interceptErrors],
9699
$this->files->getStub('valet.conf')
97100
)
98101
);

cli/Valet/Site.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public function getPhpVersion($site, $symlink = false) {
309309
}
310310

311311
/**
312-
* Extract PHP version of exising nginx config.
312+
* Extract PHP version of existing nginx config.
313313
*
314314
* @param string $url
315315
*
@@ -1201,4 +1201,4 @@ public function certificatesPath($url = null, $extension = null) {
12011201
public function caPath($caFile = null) {
12021202
return $this->valetHomePath() . '/CA' . ($caFile ? '/' . $caFile : '');
12031203
}
1204-
}
1204+
}

cli/Valet/Upgrader.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function onEveryRun() {
4343
$this->upgradeDeprecatedNginxConfigDirectives();
4444
$this->fixOldSampleValetDriver();
4545
$this->upgradeNginxSitePhpPortOverrides();
46+
$this->addMissingConfigKeys();
4647
}
4748
}
4849

@@ -320,4 +321,11 @@ private function migrateSymlinksUpgradeKey() {
320321
// Remove the legacy upgrade key to clean up the config.
321322
$this->config->removeKey($legacyUpgradeKey);
322323
}
324+
325+
/**
326+
* Add any missing necessary default configuration keys.
327+
*/
328+
public function addMissingConfigKeys() {
329+
$this->config->addMissingDefaultConfigKeys();
330+
}
323331
}

cli/stubs/valet.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ server {
4545
fastcgi_param HOME "HOME_PATH";
4646
fastcgi_param PATH_INFO $fastcgi_path_info;
4747

48-
fastcgi_intercept_errors on;
48+
fastcgi_intercept_errors VALET_INTERCEPT_ERRORS;
4949
}
5050

5151
location ~ /\.ht {

cli/valet.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,52 @@
404404
info('Use <bg=magenta> start </> <bg=magenta> stop </> or <bg=magenta> restart </> commands to change the status, eg. <bg=magenta> valet restart nginx </>');
405405
})->descriptions('List the installed Valet services.');
406406

407+
/**
408+
* Display the current visibility of the nginx error pages, or optionally
409+
* update its visibility to enable or disable them.
410+
*
411+
* Valet's nginx error pages are shown when a site has internal server or PHP errors.
412+
* They are enabled by default, but can be disabled to allow any 3rd party error
413+
* reporting tools to show instead.
414+
*
415+
* @param string $visibility Optionally, set to `on` to enable Valet's nginx error pages, or `off` to disable them. Default is `on`.
416+
*/
417+
$app->command('nginx-error-page [visibility]', function ($visibility = null) {
418+
$key = 'nginx_error_page';
419+
$current = Configuration::get($key);
420+
421+
// If no visibility argument was passed, output the current status of the nginx error pages.
422+
if ($visibility == null) {
423+
return info("Valet's nginx error pages are $current.");
424+
}
425+
426+
// If the visibility argument is valid...
427+
if (in_array($visibility, ['on', 'off'])) {
428+
// If the visibility is the same as the current value, output a message and return early.
429+
// This avoids unnecessary nginx config rewrites when the user tries to set the visibility
430+
// to its current value.
431+
if ($visibility === $current) {
432+
return info("Valet's nginx error pages are already $visibility.");
433+
}
434+
435+
// Update the configuration value for the nginx error page visibility.
436+
Configuration::updateKey($key, $visibility);
437+
438+
// Re-install nginx server config and restart nginx to apply the change.
439+
Nginx::installServer();
440+
Nginx::restart();
441+
442+
return info("Valet's nginx error pages are now $visibility.");
443+
}
444+
// If the visibility argument is invalid, output an error message.
445+
else {
446+
return warning("Invalid visibility. Please set to either 'on' or 'off'.");
447+
}
448+
449+
})->descriptions("Display the current visibility of Valet's nginx error pages, or optionally update its visibility to enable or disable them.", [
450+
'visibility' => "<fg=green>[on = default]</> will show Valet's nginx error pages \n <fg=green>[off]</> will disable them."
451+
]);
452+
407453
/**
408454
* Most commands are available only if Valet is installed.
409455
*/
@@ -1482,4 +1528,4 @@
14821528
/**
14831529
* Run the application.
14841530
*/
1485-
$app->run();
1531+
$app->run();

phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<exclude-pattern>web/app/plugins</exclude-pattern>
1414
<exclude-pattern>web/app/mu-plugins</exclude-pattern>
1515
<exclude-pattern>vendor/</exclude-pattern>
16+
<exclude-pattern>node_modules/</exclude-pattern>
1617
<exclude-pattern>web/app/uploads/</exclude-pattern>
1718

1819
<!-- Show colors in console -->

0 commit comments

Comments
 (0)