nginx泛域名解析,实现多个二级域名

简介:

利用nginx泛域名解析配置二级域名和多域名,实现二级域名子站,用户个性独立子域名。

主要针对用户独立子域名这种情况,不可能在配置里面将用户子域名写完,因此需要通过nginx泛解析方式。

配置方法:

server_name  ~^(?<subdomain>.+)\.yourdomain\.com$;

通过匹配subdomain即可。而在下面的可以通过$subdomain这个变量获取当前子域名称。

情况一:绑定子域名到统一目录,作为用户个性域名

这种情况下,只需要直接匹配就可以了,目录都是指向同一个地方的(一般)。

配置实例:

server {

    listen   80;
    server_name yourdomain.com www.yourdomain.cpm ~^(?<subdomain>.+)\.m\.yourdomain\.com$;

    index index.php index.html index.htm;
    set $root_path '/var/www/yanue.net';
    root $root_path;

    try_files $uri $uri/ @rewrite;

    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }

    location ~ \.php {
            fastcgi_pass   127.0.0.1:9000;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }

    location ~ /\.ht {
        deny all;
    }
}

这样可以实现:

user.m.yourdomain.com 跳转到用户自己页面

当然跳转逻辑需要自己在程序里面去实现。

情况二:绑定子域名到不同目录(子站)

网站的目录结构为

html
├── bbs
└── www

html为nginx的安装目录下默认的存放源代码的路径。

bbs为论坛程序源代码路径

www为主页程序源代码路径

把相应程序放入上面的路径通过

http://www.youdomain.com 访问的就是主页

http://bbs.yourdomain.com 访问的就是论坛

其它二级域名类推。

配置实例:

server {
        listen       80;
        server_name  ~^(?<subdomain>.+)\.yourdomain\.com$;
        root   html/$subdomain; 
        index  index.html index.htm index.php;
        fastcgi_intercept_errors on;
        error_page  404      = /404.html;
        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't
                # break when using query string
                try_files $uri $uri/ =404;
       }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  domain $subdomain;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
}
目录
相关文章
|
21天前
|
缓存 JavaScript 安全
深度解析Nginx正向代理的原理与实现
深度解析Nginx正向代理的原理与实现
|
2月前
|
网络协议
阿里云DNS常见问题之opendn.com.cn域名云解析阿里云DNS付费后还是免费版如何解决
阿里云DNS(Domain Name System)服务是一个高可用和可扩展的云端DNS服务,用于将域名转换为IP地址,从而让用户能够通过域名访问云端资源。以下是一些关于阿里云DNS服务的常见问题合集:
|
2月前
|
应用服务中间件 nginx
Nginx中如何配置中文域名?
Nginx中如何配置中文域名?
62 0
|
3月前
|
应用服务中间件 PHP 开发工具
Nginx解析环境搭建及实战
Nginx解析环境搭建及实战
26 0
|
2天前
|
域名解析 弹性计算 应用服务中间件
基于nginx反向代理实现OSS固定域名IP访问
本文基于阿里云OSS手册:https://help.aliyun.com/zh/oss/use-cases/use-an-ecs-instance-that-runs-centos-to-configure-a-reverse-proxy-for-access-to-oss,继续深入讨论如何利用nginx反向代理,实现固定的IP/域名访问OSS bucket。官方文档能够解决大部分的反向代理固定IP访问oss bucket的场景,但是对于必须使用域名作为endpoint的系统,会出现signatrue鉴权问题。本文继续在官方文档的基础上,将反向代理需要域名作为endpoint的场景补齐方案。
|
11天前
|
网络协议 Windows
Windows Server 各版本搭建 DNS 服务器实现域名正反向解析
Windows Server 各版本搭建 DNS 服务器实现域名正反向解析
|
15天前
|
域名解析 网络协议 大数据
阿里云域名购买与域名解析使用教程(图文教程)
该文档是关于在阿里云注册账号、购买域名及进行DNS解析的步骤指南。首先,需注册阿里云账号并进行实名认证,建议选择企业认证以便获取企业发票。接着,通过阿里云域名注册页面检查并购买未被注册的域名,将域名添加至购物车并完成支付。购买后,在云解析DNS界面进行实名认证,然后对域名进行解析设置,选择A记录类型,填写主机记录(如www),记录值为云服务器的IP地址,设置TTL,并校验解析是否成功。最后,文中还提及了如何在云服务器上进行实例名称的修改和查看服务器状态等操作。
|
21天前
|
域名解析 缓存 负载均衡
Nginx正向代理域名的配置
Nginx正向代理域名的配置
|
22天前
|
Ubuntu 应用服务中间件 Linux
nginx 配置代理ip访问https的域名配置
nginx 配置代理ip访问https的域名配置
|
29天前
|
域名解析 弹性计算 安全
阿里云域名注册到备案再到解析详细流程
本文主要讲解域名的注册,备案和解析流程,帮小白轻松搞定域名全流程

相关产品

  • 云解析DNS
  • 推荐镜像

    更多