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.

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

报告相同问题?

悬赏问题

  • ¥50 深度学习运行代码直接中断
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥20 需要完整的共散射点成像代码
  • ¥15 编写vba代码实现数据录入工作
  • ¥15 做过TCL海信电视小米电视相关影视会员软件私我
  • ¥15 Mapreduce是正常的,在运行其他jar包时并没有任何问题,只是在做LogCount.jar 时出的问题。如图所示
  • ¥15 ImportError: DLL load failed while importing _iterative: 找不到指定的模块。
  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢