doujiu8145 2017-09-06 12:49
浏览 44
已采纳

在EC2上的Nginx(Laravel 5.4)中设置自定义域名

I'm building a site builder where users have a subdomain username.myapp.com using wildcard subdomains to serve up their website (which works).

I want to allow users to use their own domain name so I setup dns.myapp.com which points to the server IP address.

In my domain name provider I've added www.customdomain.com CNAME to map to dns.myapp.com

It looks like www.customdomain.com is pointing to the server but it's not hitting the Laravel route and I get the Nginx 404 error page.

I've built a test method just to display the full domain path and check it's resolving;

RouteServiceProvider:

public function boot()
{
    \Route::pattern('domain', '[a-z0-9.\-]+');
    parent::boot();
}

routes/web.php

Route::group(['domain' => '{domain}'], function() {
    Route::get('/', 'PageController@domain');
});

Controllers/PageController.php

public function domain($domain)
{
    return $domain;
}

I can enter anything.myapp.com and it will return that path. I am trying to get www.customdomain.com to do the same.

This probably doesn't make a difference but myapp.com is in AWS Route 53 mapping to an EC2 server implemented through Laravel Forge.

EDIT: Nginx config:

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/myapp.com/before/*;

server {
    listen 80;
    listen [::]:80;
    server_name .myapp.com;
    root /home/forge/myapp.com/public;

    # FORGE SSL (DO NOT REMOVE!)
    # ssl_certificate;
    # ssl_certificate_key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php

    charset utf-8;

    # FORGE CONFIG (DOT NOT REMOVE!)
    include forge-conf/myapp.com/server/*;

    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/myapp.com-error.log error;

    error_page 404 /index.php;

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

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/myapp.com/after/*;                             
  • 写回答

1条回答 默认 最新

  • duanqiaoren9975 2017-09-06 14:59
    关注

    When you and setup multiple domains, you must config not only your DNS, but also your http servers to handle new domains.

    If you have only one server {} directive, it acts as default server. Even if your users uses IP or any other alias. Such servers receive much junk traffic, so good practice is create "dummy page" as default server and real server will handle only good queries.

    But sometime it's not working. You can do a hack:

    server_name ~.;
    

    It uses regular expression as "any string" and will work in anyway. Reasons, why don't work first server directive is unclear: possible somewhere included other config, patched nginx or even proxy at hoster side.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于react-hook组件用函数控制是否渲染的及时性问题。
  • ¥50 Linux下的软件,要做模块化拆分。进程间通信是否有开源框架可以借用?
  • ¥100 修改原有的MYSQL存储代码,在最右边添加多列数据
  • ¥20 Open Interpreter 使用时报错: still has pending operation at deallocation, the process may crash
  • ¥15 qt中链接动态链接库,调用其中的函数,该函数的参数需要传入回调函数,自己创建的回调函数无法作为参数传递进去
  • ¥15 如何把api接口返回的json数据自动计算分页自动执行并写入mysql数据库。
  • ¥15 matlab svm二分类代码问题
  • ¥40 求一款能支持ios15以上的屏蔽越狱插件。比较好用的
  • ¥15 C++ QT对比内存字符(形式不定)
  • ¥30 C++第三方库libiconv 远程安装协助