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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵