一聚教程网:一个值得你收藏的教程网站

热门教程

Nginx实现泛域名CDN节点配置

时间:2022-06-30 18:48:16 编辑:袖梨 来源:一聚教程网

如何使用Nginx泛域名解析+反向代理+静态资源缓存呢?

1.安装nginx,安装过程不再赘述,记得带上pcre、gzip、sub、status这几个模块,另外如果想开通在线清理缓存功能,需要安装ngx_cache_purge这个第三方模块。
2.删除nginx.conf中默认的server段,此操作不做你会让你抱憾终身。
3.将以下代码插入nginx.conf尾部,-t测试-s reload重启即可。

 代码如下 复制代码

#定义缓存的临时目录以及缓存存储目录
proxy_temp_path /data/temp;
proxy_cache_path /data/cache levels=1:2 keys_zone=cache_one:32m inactive=1d max_size=3g;
server
{
    listen 80;
    #接收泛域名解析,务必保证在这之前没有其他server段干扰。
    server_name _;
    root /tmp;
    access_log off;

    #匹配出对应域名
    if ( $host ~* (.*).(.*).(.*))
    {
        set $domain $1;
    }

    location /
        {
        #定义所使用的缓存以及缓存的目录结构。
        proxy_cache cache_one;
        proxy_cache_valid  200 304 12h;
        proxy_cache_key $host$uri$is_args$args;
        #下面这条灰常重要,告知upstream源服务器所要请求的域名是什么。
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #把请求扔给upstream,然后等着领赏吧。
        proxy_pass http://jp0129;
        proxy_set_header Accept-Encoding "";
        expires 1d;
        }
    location ~ .*.(php)?$
        {
        #动态请求不缓存
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://jp0129;
        proxy_set_header Accept-Encoding "";
        }
}
upstream  jp0129
{
        server 106.187.51.139;
}

大功告成,根据自身情况上机器,每台部署一个nginx即可,在域名管理中把vcs.xxx.com直接A记录几条轮询,配合一个小脚本来实现检测各个节点是否存活,节点宕掉就直接通过dnspod的api修改vcs.xxx.com的解析记录,剔除无效节点即可。

附一个Nginx下泛域名解析配置

在Nginx下支持泛域名解析其实很简单.只要在在编译 Nginx的时候加上以下代码即可

--with-http_sub_module

在配置nginx时:

 代码如下 复制代码

server {

        # Replace this port with the right one for your requirements

        listen      80;#could also be 1.2.3.4:80

 

        # Multiple hostnames seperated by spaces.  Replace these as well.

        server_name  www.111com.net *.你的域名.com;

        #Alternately: _ *

        root /PATH/TO/WEBROOT/$host;

        error_page  404              http://yourdomain.com/errors/404.html;

        access_log  logs/yourdomain.com.access.log;

        location / {

            root  /PATH/TO/WEBROOT/$host/;

            index  index.php;

        }

 

        # serve static files directly

        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {

            access_log        off;

            expires          30d;

        }

 

        location ~ .php$ {  

          # By all means use a different server for the fcgi processes if you need to  

          fastcgi_pass  127.0.0.1:YOURFCGIPORTHERE;

          fastcgi_index  index.php;

 

          fastcgi_param  SCRIPT_FILENAME  /PATH/TO/WEBROOT/$host/$fastcgi_script_name;

          fastcgi_param  QUERY_STRING    $query_string;

          fastcgi_param  REQUEST_METHOD  $request_method;

          fastcgi_param  CONTENT_TYPE    $content_type;

          fastcgi_param  CONTENT_LENGTH  $content_length;

          fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;

          fastcgi_param  REQUEST_URI        $request_uri;

          fastcgi_param  DOCUMENT_URI      $document_uri;

          fastcgi_param  DOCUMENT_ROOT      $document_root;

          fastcgi_param  SERVER_PROTOCOL    $server_protocol;

          fastcgi_param  REMOTE_ADDR        $remote_addr;

          fastcgi_param  REMOTE_PORT        $remote_port;

          fastcgi_param  SERVER_ADDR        $server_addr;

          fastcgi_param  SERVER_PORT        $server_port;

          fastcgi_param  SERVER_NAME        $server_name;

          fastcgi_intercept_errors on;

        }

 

        location ~ /.ht {

            deny  all;

        }

    }

然后启动nginx就可以实现泛域名解析了

热门栏目