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

在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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题