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 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥15 在现有系统基础上增加功能