因为最近在用valet,然后把MAMP PRO都干掉了(不能怪我啊,实在是MAMP PRO6的一些使用太反人类了。。。不知道作者是怎么想的),但现在想想反正也就是这样简单的使用,干脆就用valet了算了。其实也可以用laravel sail的。但因为4月份之前,M1版的mac上的docker都不正常。所以,就暂时放弃了,现在用用valet也算OK。80%的情况下都OK
创建个目录,valet link一下就完事了。因为现在都是laravel的项目,用起来也算方便,毕竟也是官方的工具。但因为我用frp,所以我设置valet tld 真实的域名(比如:xxx.com),这就造成,我访问demo.xxx.com,访问的是127.0.0.1,然后我映射在外面的server也是demo.xxx.com,其实这一切都OK了。然而问题是,有时候我要测试真实域名,比如就是 demo.xxx.com ,我就只能打开全局梯子。这样我访问demo.xxx.com才对应了 frp的域名。
听起来有点绕口令,说白了就是,因为用了真实域名,所以我默认访问都是本地,但外网访问我都是真实的。而我如果要测试真实的网址,反而还要爬个梯子。虽然影响不大,但还是想换换,于是我就想,valet 是不是可以允许绑定两个域名?这样的话,是不是就全解决了?本地测试一个域名,一个域名公开对外。
找了一些文章,发现默认是不支持,但有人通过hack的方法(说白了就是改模板,改配置改代码。。。。)支持了。原文在这里:https://medium.com/@simonhamp/supporting-multiple-tlds-in-laravel-valet-e3d2a963c613
我简单的COPY了一下,没排版,要看详细的,还是直接看原版吧:
- Valet is great.
- Occasionally though I need an app to respond on a different TLD to the default one. And there are even times when it makes sense to have an app respond on multiple TLDs (e.g. to prove some multi-domain functionality).
- You may have some secure assets hosted by a third-party that requires domain verification (FontAwesome Pro). In development, that may mean it’s easier to be using the same domain as other devs on the team than add loads of different dev domains to the service’s whitelist.
- In the current release of Valet, this kind of functionality isn’t natively available. Even in third-party packages like Valet+, this isn’t supported. But with a few quick changes it’s easily possible. Let me show you how.
- Dnsmasq
- First we’ll need to update the Dnsmasq config so that it will handle multiple TLDs.
- Turns out this is really easy. Valet holds some config in ~/.config/valet/dnsmasq.conf.
- In there we just need to add some config for each TLD we want to support. Add the following two lines for every TLD you want to support. Replace {tld} with the TLD, unsurprisingly.
- address=/{tld}/127.0.0.1
- listen-address=127.0.0.1
- Then restart Dnsmasq:
- sudo brew services restart dnsmasq
- Valet
- Now we just need to update the Valet code to support multiple domains.
- Firstly, we’re going to add some values to our ~/.config/valet/config.json. I’ve opted for simply adding keys rather than changing any existing one. I’ve added a tlds array. This will be used later.
- {
- ...
- "tlds": [
- "tld1",
- "tld2"
- ]
- }
- Then we just need to edit some Valet files and run some commands.
- All of these edits I’m making directly in my globally-installed Valet (~/.composer/vendor/laravel/valet/).
- // server.php, line 70
- $siteName = basename(
- // Filter host to support wildcard dns feature
- valet_support_wildcard_dns($_SERVER['HTTP_HOST']),
- '.'.$tld
- );
- // change to:
- foreach ($valetConfig['tlds'] as $tld) {
- if (strpos($_SERVER['HTTP_HOST'], '.'.$tld) === false) {
- continue;
- }
- $siteName = basename(
- // Filter host to support wildcard dns feature
- valet_support_wildcard_dns($_SERVER['HTTP_HOST']),
- '.'.$tld
- );
- }
- This just allows the server to respond to whichever one of the TLDs we want to support.
- Now let’s update the CLI so we can generate and remove SSL certs for all of the TLDs (valet secure and valet unsecure commands). Firstly, secure:
- // cli/valet.php, line 145, secure command
- $app->command('secure [domain]', function ($domain = null) {
- $url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld'];
- Site::secure($url);
- Nginx::restart();
- info('The ['.$url.'] site has been secured with a fresh TLS certificate.');
- })->descriptions('Secure the given domain with a trusted TLS certificate');
- // change to:
- $app->command('secure [domain]', function ($domain = null) {
- $urls = [];
- foreach (Configuration::read()['tlds'] as $tld) {
- $url = ($domain ?: Site::host(getcwd())).'.'.$tld;
- Site::secure($url);
- $urls[] = $url;
- }
- Nginx::restart();
- $urls = implode(', ', $urls);
- info('The ['.$urls.'] site has been secured with a fresh TLS certificate.');
- })->descriptions('Secure the given domain with a trusted TLS certificate');
- And now the unsecure command:
- // cli/valet.php, line 158, unsecure command
- $app->command('unsecure [domain]', function ($domain = null) {
- $url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld'];
- Site::unsecure($url);
- Nginx::restart();
- info('The ['.$url.'] site will now serve traffic over HTTP.');
- })->descriptions('Stop serving the given domain over HTTPS and remove the trusted TLS certificate');
- // change to:
- $app->command('secure [domain]', function ($domain = null) {
- $urls = [];
- foreach (Configuration::read()['tlds'] as $tld) {
- $url = ($domain ?: Site::host(getcwd())).'.'.$tld;
- Site::secure($url);
- $urls[] = $url;
- }
- Nginx::restart();
- $urls = implode(', ', $urls);
- info('The ['.$urls.'] site will now serve traffic over HTTP.');
- })->descriptions('Stop serving the given domain over HTTPS and remove the trusted TLS certificate');
- Finally, run the following command:
- valet restart
- Enjoy
以上是仅复制未排版,嫌丑的看原文:https://medium.com/@simonhamp/supporting-multiple-tlds-in-laravel-valet-e3d2a963c613
如果不用梯子看不了,就将就点吧