AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 413148
Accepted
Arcticooling
Arcticooling
Asked: 2017-12-27 08:57:49 +0800 CST2017-12-27 08:57:49 +0800 CST 2017-12-27 08:57:49 +0800 CST

Nginx - 创建链接到另一个(本地)网站的子域

  • 772

我有一个带有纯 Nginx 的基于 Ubuntu 16.04 的 DigitalOcean VPS(我通过 DigitalOcean 的 DNS 管理工具管理 DNS)。在我的 Nginx 环境中,我在/var/www/html/. 一种是example.com(HTTPS),另一种是具有名为test(HTTP) 的目录的无域站点。

如何为example.com创建一个将导致test的子域?( test.example.com)?


1)我的nginx.conf。

2)我的example.com站点配置:

server {
    root /var/www/html/example.com;
    server_name example.com www.example.com;

    location ~ /\.ht {
        deny all;
    }

    location / {
        index index.php index.html index.htm fastcgi_index;
        try_files $uri $uri =404 $uri/ /index.php?$args;
    }

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {
        expires 365d;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    listen 80; # managed by Certbot

    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    # Redirect non-https traffic to https
    # if ($scheme != "https") {
    #     return 301 https://$host$request_uri;
    # } # managed by Certbot
}

3)我的test网站配置:

server {
    root /var/www/html/test;

    location ~ /\.ht {
        deny all;
    }

    location / {
        index index.php index.html index.htm fastcgi_index;
        try_files $uri $uri =404 $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    listen 80;
}

更新:

  • 我还没有更改我的 VPS 托管服务提供商的 DNS 管理工具中的任何内容。
  • 我希望世界上的每个人都能够test通过子域访问该站点。
dns ip
  • 1 1 个回答
  • 1917 Views

1 个回答

  • Voted
  1. Best Answer
    Thomas Ward
    2017-12-27T12:49:36+08:002017-12-27T12:49:36+08:00

    在任何 Nginx 和 DNS 环境中都需要两件事来正确创建具有不同文档根的新子域:

    1. 处理它的附加server { }块(除了您在第 3 项中已有的块)。

    2. 将另一个子域指向正确的网络服务器的 DNS 记录。

    根据提供的配置,您需要做两件事:

    1. 您的test.example.com站点配置缺少server_name test.example.com;指令。添加一个并重新启动您的nginx过程。

    2. 在您的主域的 DNS 中设置test.example.comDNS 记录(可能来自您的云的 DNS 管理工具)。

    总是告诉 NGINX 哪些站点要处理哪些服务器块。作为nginxUbuntu 中的包维护者,我熟悉人们遇到的大多数用户级陷阱,例如这个。


    你给了我们这个:

    server {
        root /var/www/html/test;
    
        location ~ /\.ht {
            deny all;
        }
    
        location / {
            index index.php index.html index.htm fastcgi_index;
            try_files $uri $uri =404 $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        listen 80;
    }
    

    因此,从字面上看,只需在您的行之前添加root这一行:

    server_name test.example.com;
    

    ...你得到这个配置文件:

    server {
        root /var/www/html/test;
        server_name test.example.com;
    
        location ~ /\.ht {
            deny all;
        }
    
        location / {
            index index.php index.html index.htm fastcgi_index;
            try_files $uri $uri =404 $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        listen 80;
    }
    
    • 2

相关问题

  • 如何使用不同的 DNS 服务器将主机名解析为 ip?

  • 将外部 IP 放入变量中 - grep 操作失败 [重复]

  • bind9反向解决问题

  • IP地址可以以255结尾而不是广播IP地址吗?

  • 如何验证我使用 9.9.9.9 作为 DNS?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    JSON数组使用jq来bash变量

    • 4 个回答
  • Marko Smith

    日期可以为 GMT 时区格式化当前时间吗?[复制]

    • 2 个回答
  • Marko Smith

    bash + 通过 bash 脚本从文件中读取变量和值

    • 4 个回答
  • Marko Smith

    如何复制目录并在同一命令中重命名它?

    • 4 个回答
  • Marko Smith

    ssh 连接。X11 连接因身份验证错误而被拒绝

    • 3 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Marko Smith

    systemctl 命令在 RHEL 6 中不起作用

    • 3 个回答
  • Marko Smith

    rsync 端口 22 和 873 使用

    • 2 个回答
  • Marko Smith

    以 100% 的利用率捕捉 /dev/loop -- 没有可用空间

    • 1 个回答
  • Marko Smith

    jq 打印子对象中所有的键和值

    • 2 个回答
  • Martin Hope
    EHerman JSON数组使用jq来bash变量 2017-12-31 14:50:58 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Drux 日期可以为 GMT 时区格式化当前时间吗?[复制] 2017-12-26 11:35:07 +0800 CST
  • Martin Hope
    AllisonC 如何复制目录并在同一命令中重命名它? 2017-12-22 05:28:06 +0800 CST
  • Martin Hope
    Steve “root”用户的文件权限如何工作? 2017-12-22 02:46:01 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST
  • Martin Hope
    Cbhihe 将默认编辑器更改为 vim for _ sudo systemctl edit [unit-file] _ 2017-12-03 10:11:38 +0800 CST
  • Martin Hope
    showkey 如何下载软件包而不是使用 apt-get 命令安装它? 2017-12-03 02:15:02 +0800 CST
  • Martin Hope
    youxiao 为什么目录 /home、/usr、/var 等都具有相同的 inode 编号 (2)? 2017-12-02 05:33:41 +0800 CST
  • Martin Hope
    user223600 gpg —list-keys 命令在将私钥导入全新安装后输出 uid [未知] 2017-11-26 18:26:02 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve