【问题标题】:Setting up multiple domains on nginx server在 nginx 服务器上设置多个域
【发布时间】:2018-02-10 12:24:34
【问题描述】:

我在运行单个应用程序的 ubuntu 上有一个 nginx 服务器。我想让多个域指向这个服务器。

目前我的 /etc/nginx/sites-available/default 看起来像这样:

(website1.com 只是一个例子,因为我不想透露我的真实域名)

server {
    listen 80 default_server;
    listen [::]:80 default_server;


    root /var/www/website/public;
    index index.php index.html index.htm;


    server_name website1.com;

    location / {

            try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME 
            $document_root$fastcgi_script_name;
            include fastcgi_params;

    }

}

这在有人访问 website1.com 时有效

现在,如果人们来自 website2.com,我是否只需再次准确地复制代码并更改 server_name?

或者还有什么我必须做的吗?

【问题讨论】:

    标签: nginx configuration


    【解决方案1】:

    您可以在服务器名称指令中列出尽可能多的域名

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
    
        root /var/www/website/public;
        index index.php index.html index.htm;
    
    
        server_name website1.com website2.com website3.com;
    ...
    

    请记住,您需要一个将 website2.com 解析为与 website2.com 相同的 ip 的 dns!

    【讨论】:

    • 谢谢老兄!当涉及到 ssl 证书时,我会怎么做?
    • 如果您使用子域,您可以只使用通配符证书,但如果您的网站类似于 website1.com 和 website2.com 而不是 web2.website.com 和 web1.website.com,则您需要每个站点都有一个几乎相同的服务器块,只需更改 server_name 和 ssl 部分。
    • 如果它符合您的期望,请接受答案:D
    猜你喜欢
    • 1970-01-01
    • 2022-11-23
    • 2021-10-15
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多