Configure a CDN node for a wildcard domain name using Nginx

Source: Internet
Author: User
Tags in domain nginx server

How can I use Nginx wildcard domain name resolution + reverse proxy + static resource cache?

1. Install nginx. Do not repeat the installation process. Remember to include the pcre, gzip, sub, and status modules. If you want to enable the online cache cleanup function, install the ngx_cache_purge third-party module.
2. Delete the default server segment in nginx. conf. If this operation is not performed, you will be sorry for your life.
3. Insert the following code to the end of nginx. conf and-t test-s reload to restart.

The code is as follows: Copy code

# Define the cache temporary directory and cache storage directory
Proxy_temp_path/data/temp;
Proxy_cache_path/data/cache levels = 1:2 keys_zone = cache_one: 32 m inactive = 1d max_size = 3g;
Server
{
Listen 80;
# When receiving wildcard domain name resolution, ensure that no other server segment interference exists before this.
Server_name _;
Root/tmp;
Access_log off;

# Matching the corresponding domain name
If ($ host ~ *(.*).(.*).(.*))
    {
Set $ domain $1;
    }

Location/
        {
# Define the cache used and the cache directory structure.
Proxy_cache cache_one;
Proxy_cache_valid 200 304 12 h;
Proxy_cache_key $ host $ uri $ is_args $ args;
# The following is often important to inform the upstream source server of the domain name to be requested.
Proxy_set_header Host $ host;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
# Throw the request to upstream and wait for the reward.
Proxy_pass http: // jp0129;
Proxy_set_header Accept-Encoding "";
Expires 1d;
        }
Location ~ . *. (Php )? $
        {
# Dynamic requests are not cached
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;
}

As A result, each server can be deployed with an nginx server based on its own situation. In domain name management, vcs.xxx.com is directly recorded with A few round-robin entries, and A small script is used to check whether each node is alive, when a node goes down, you can directly modify the DNS record of vcs.xxx.com through the dnspod api and remove invalid nodes.

A wildcard domain name resolution configuration under Nginx is provided.

It is actually very easy to support wildcard domain name resolution in Nginx. You only need to add the following code when compiling Nginx.

With-http_sub_module

When configuring nginx:

The code is as follows: Copy code

Server {

# Replace this port with the right one for your requirements

Listen 80; # cocould also be 1.2.3.4: 80

 

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

Server_name www.111cn.net *. Your domain name. 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. 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

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;

        }

    }

Then start nginx to implement wildcard domain name resolution.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.