【问题标题】:Link 2 domains to 1 Laravel project via Nginx通过 Nginx 将 2 个域链接到 1 个 Laravel 项目
【发布时间】:2019-03-30 05:52:28
【问题描述】:

我正在尝试将另一个域链接到我现有的项目

我正在使用 Laravel 5.1,我知道我们在 .env 中只有一个 APP_URL

有没有办法通过 Nginx 级别?

cat /etc/nginx/sites-available/default

server {                                                                                                                                                                           
    listen 80 default_server;                                                                                                                                                      
    server_name default;                                                                                                                                                           
    root /home/forge/bheng/public;                                                                                           
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;                                                                                                                                           

    index index.html index.htm index.php;                                                                                                                                          

    charset utf-8;                                                                                                                                                                 

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }


    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    access_log off;
    error_log  /var/log/nginx/default-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

如何配置这样的东西?

【问题讨论】:

  • 值得注意的是,APP_URL 环境变量仅由 Laravel 用于未提供 Host: 请求标头的情况,例如来自控制台命令或使用类似 route() 的排队作业或 asset()(在 HTTP 请求期间未启动)。

标签: laravel nginx virtualhost digital-ocean


【解决方案1】:

将两个域作为别名放在服务器名称配置中

server_name domain1.com www.domain1.com domain2.com www.domain2.com ;               

【讨论】:

  • 刚刚这样做并重新启动应该可以工作的服务器 - 对吧?这应该适用于任何项目堆栈?不用 Laravel 吧?
  • .env 文件中的域配置主要用于 CLI 命令(php artisan)。您也可以按域定义路由。
【解决方案2】:

您为两个域指定相同的 webroot。在 Laravel 代码中,您使用域组或url('/') 来检查您所在的域。

您的配置可能如下所示:

server {
     listen 80, 443;
     listen [::]:80, [::]:443;
     servername www.domain1.com www.domain2.com;

     root /home/kyo/laravel/public/;

     index index.php;

     location / {
          try_files $uri $uri/ =404;
     }
}

【讨论】:

  • 我不确定你在说什么,我很难想象。添加更多注释以支持您的观点?
【解决方案3】:

我使用了这样的东西,一个服务器名称别名:

server_name www.app1 www.app2;

然后让 DNS 指向同一个主机。如果您在自己的 linux 机器上工作,请更改 /etc/hosts 文件:

sudo vim /etc/hosts

并添加新主机:

127.0.0.1 www.app1
127.0.0.1 www.app2

【讨论】:

  • 我知道我们可以在cat /etc/nginx/sites-available/default 中做第一个,但是我们将在哪里配置您答案的第二部分?在 Linux 的什么文件中? /etc/hosts??
猜你喜欢
  • 2021-10-09
  • 2017-06-03
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 2021-07-27
  • 2020-01-06
  • 1970-01-01
相关资源
最近更新 更多