Solution: Gravatar profile pictures, Google fonts, and Google ajax cannot be used

Source: Internet
Author: User
Tags flock md5 php file ssl certificate nginx reverse proxy

Gravatar portraits are occasionally inaccessible in mainland China, and there are also a variety of google services that cannot be used. It is intolerable for "a programmer.
So he got a solution: Use nginx Reverse proxy Gravatar avatar, Google fonts, and Google ajax.
Before anti-proxy, make sure that nginx has installed the proxy_cache module and configured the path and parameters.


The following is the configuration code of the green onion in nginx. conf:


Http
{
# Other code ...#

Proxy_connect_timeout 5;
Proxy_read_timeout 60;
Proxy_send_timeout 5;
Proxy_buffer_size 16 k;
Proxy_buffers 4 64 k;
Proxy_busy_buffers_size 128 k;
Proxy_temp_file_write_size 128 k;
Proxy_temp_path/tmp/nginx_proxy_temp 1 2; # temporary file directory

# Other code ...#
}


Then there is a separate configuration file gravatar. conf, with # you need to modify it accordingly:


Proxy_cache_path/tmp/nginx_proxy_cache/gravatar levels = keys_zone = GRAVATAR: 32 m inactive = 7d max_size = 1g; # cache path

Server {
Listen 80;
Server_name gravatar.yuxiaoxi.com; # bound domain name

Location /{
Proxy_cache GRAVATAR;
Proxy_cache_key "$ scheme: // $ host $ request_uri ";
Proxy_cache_valid 200 304 7d;
Proxy_cache_valid 301 3d;
Proxy_cache_valid any 10 s;

Proxy_pass http://0.gravatar.com; # reverse domain name
Proxy_pass_header Server;
Proxy_redirect off;
Proxy_set_header Host 0.gravatar.com;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Scheme $ scheme;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
    }

Location ~ /Purge (/.*){
Allow 127.0.0.1;
Deny all;
Proxy_cache_purge GRAVATAR "$ scheme: // $ host $1 ";
    }

Location ~ * \. (Ftpquota | htaccess | htpasswd | asp | aspx | jsp | asa | mdb )? $ {
Deny all;
    }

Access_log/home/wwwroot/gravatar/log/access. log combined; # access_log end
Error_log/home/wwwroot/gravatar/log/error. log crit; # error_log end
}

Server
{
Listen 443 ssl spdy;
Server_name gravatar.yuxiaoxi.com; # bound domain name

Ssl on;
Ssl_certificate/usr/local/ssl/yuxiaoxi. crt; # ssl certificate
Ssl_certificate_key/usr/local/ssl/yuxiaoxi. pem; # ssl key
Ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
Ssl_ciphers 'ecdhe-RSA-AES128-GCM-SHA256: ECDHE-ECDSA-AES128-GCM-SHA256: ECDHE-RSA-AES256-GCM-SHA384: ECDHE-ECDSA-AES256-GCM-SHA384: DHE-RSA-AES128-GCM-SHA256: kEDH + AESGCM: DHE-DSS-AES128-GCM-SHA256: ECDHE-RSA-AES128-SHA256: ECDHE-ECDSA-AES128-SHA256: ECDHE-RSA-AES128-SHA: ECDHE-ECDSA-AES128-SHA: ECDHE-RSA-AES256-SHA384: ECDHE-ECDSA-AES256-SHA384: ECDHE-RSA-AES256-SHA: ECDHE-ECDSA-AES256-SHA: DHE-RSA-AES128-SHA25: 6: DHE-RSA-AES128-SHA: DHE-DSS-AES128-SHA256: DHE-RSA-AES256-SHA256: DHE-DSS-AES256-SHA: DHE-RSA-AES256-SHA: AES128-GCM-SHA256: AES128: AES256: AES :! ANULL :! ENULL :! EXPORT :! DES :! RC4 :! MD5 :! PSK ';
Ssl_prefer_server_ciphers on;
Ssl_session_timeout 5 m;
Ssl_session_cache shared: SSL: 10 m;
Ssl_trusted_certificate/usr/local/ssl/ca. crt; # CA root certificate

Location /{
Proxy_cache GRAVATAR;
Proxy_cache_key "$ scheme: // $ host $ request_uri ";
Proxy_cache_valid 200 304 7d;
Proxy_cache_valid 301 3d;
Proxy_cache_valid any 10 s;

Proxy_pass http://0.gravatar.com; # reverse domain name
Proxy_pass_header Server;
Proxy_redirect off;
Proxy_set_header Host 0.gravatar.com;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Scheme $ scheme;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
    }

Location ~ /Purge (/.*){
Allow 127.0.0.1;
Deny all;
Proxy_cache_purge GRAVATAR "$ scheme: // $ host $1 ";
    }

Location ~ * \. (Ftpquota | htaccess | htpasswd | asp | aspx | jsp | asa | mdb )? $ {
Deny all;
    }

Access_log/home/wwwroot/gravatar/log/access_ssl.log combined; # access_log end
Error_log/home/wwwroot/gravatar/log/error_ssl.log crit; # error_log end
}


Google fonts is similar to Google ajax configuration, so I will not post it. Please check the download link at the end of the article.
You can also use the green onion directly to support SSL ~ :


Replace www.gravatar.com, cn.gravatar.com, 0.gravatar.com, 1.gravatar.com, and 2.gravatar.com with gravatar.yuxiaoxi.com;
Replace fonts.gstatic.com with fonts.yuxiaoxi.com;
Replace ajax.googleapis.com with ajax.yuxiaoxi.com;


BYMT topic User modification method:


1. Open the functions. Php file in the/wp-content/themes/BYMT/directory.
2. Search for bymt_get_avatar and replace gravatar.duoshuo.com with gravatar.yuxiaoxi.com.
3. Search for bymt_avatar, start from // getting the avatar and cache, and replace it:
// Obtain the content through curl
Function bymt_curl_contents ($ url ){
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, TRUE );
Curl_setopt ($ ch, CURLOPT_URL, $ url );
$ Response = curl_exec ($ ch );
Curl_close ($ ch );

/** Request is blank **/
If (empty ($ response )){
$ Response = '';
    }

Return $ response;
}

// Write an object
Function bymt_write_file ($ path, $ data, $ mode = 'WB '){
If (! $ Fp = @ fopen ($ path, $ mode )){
Return false;
    }

Flock ($ fp, LOCK_EX );
Fwrite ($ fp, $ data );
Flock ($ fp, LOCK_UN );
Fclose ($ fp );

Return true;
}

// Get the avatar and cache
Function bymt_avatar ($ email, $ size = '50', $ time = '000000') {// Default size: 50px valid for 14 days
If (! Is_numeric ($ size) $ size = '50 ';
If (! Is_numeric ($ time) $ time = '000000 ';
If (bymt_c ('Avatar _ cache ')){
$ Mailhash = md5 (strtolower (trim ($ email )));
$ Default = urlencode (get_option ('Avatar _ default '));
$ Rating = get_option ('Avatar _ rating ');
$ Ava_path = ABSPATH. 'Avatar/'. $ mailhash.'-'. $ size. '.jpg ';
If (! File_exists ($ ava_path) | filesize ($ ava_path) <1 | (time ()-filemtime ($ ava_path)> $ time ){
$ Ava_url = 'http: // gravatar.yuxiaoxi.com/avatar/'.w.mailhash .'? S = '. $ size.' & d = '. $ default.' & r = '. $ rating;
$ Avadata = bymt_curl_contents ($ ava_url );
Bymt_write_file ($ ava_path, $ avadata );
} Else {
$ Ava_url = esc_url (home_url ('/avatar/'). $ mailhash. '-'. $ size. '.jpg ';
        }
Return ' ';
} Else {
Return get_avatar ($ email, $ size );
    }
}

4. Open/wp-shortdes/script-loader.php, search ajax.googleapis.com with ajax.yuxiaoxi.com, search fonts.gstatic.com with fonts.yuxiaoxi.com;
Work done!

Related Article

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.