There is a project that uses Baidu voice interface. Because of its small function, it doesn't want to write on the server. It plans to directly use js to tune Baidu interface, and then use nginx proxy to solve cross domain problems. Then, what can I do with token? Token must be cached and can't be cached in the browser. So I tried nginx proxy cache:
# The basic configuration of the proxy cache path cache needs to be placed outside the server. Here are the configuration items # Cache file path / data/nginx/cache (custom) # Keys > zone set cache name and shared memory size one cache: 50m # Levels sets the cache file directory level; levels=1:2 indicates two-level directory # inactive deletes the cache file that has not been accessed within the specified time # The maximum cache hard disk space is 200m. If the cache space is full, the resource with the longest cache time will be overwritten by default. # The following two are the parameters to load the cache when nginx starts # Loader'threshold loader performs up to 300 milliseconds per iteration # Loader ﹣ files loader loads up to 200 files in each iteration proxy_cache_path /data/nginx/cache keys_zone=one-cache:50m levels=1:2 inactive=7d loader_threshold=300 loader_files=200 max_size=200m; #Baidu voice recognition interface cross domain agent server { listen 80; server_name vop-baidu.proxy.abc.com; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers X-Requested-With,content-type; add_header Access-Control-Allow-Methods GET,POST; #,OPTIONS; add_header Access-Control-Max-Age 99999999; location / { proxy_pass http://vop.baidu.com; } #Baidu voice token location /token { proxy_cache one-cache; # Use a cache named one cache (required) proxy_cache_methods GET HEAD POST; #Configure the default GET|HEAD of the method to be cached proxy_cache_valid 200 302 10d; # Cache requests for 200 and 302 states for 10 days, any for all States proxy_cache_key $uri; # Defining cache key is the request URL by default #****Important * * * this is applicable to the response header entry of the proxy pass target server to be ignored, #Respond when the target server sets cache control or set cookie header #Will not be cached or affect the cache policy, so choose to ignore proxy_ignore_headers Cache-Control Set-Cookie; proxy_pass https://openapi.baidu.com/oauth/2.0/token?xxxxxxx; } }
After configuration, remember to create the cache file directory (/ data/nginx/cache, which can be automatically created in the cache layer), then restart nginx, request the relevant link to check whether the configuration is effective, and if it is effective, the cache file will appear in the cache directory:
There are also some advanced configuration items, proxy no cache, proxy cache bypass, proxy cache purge, proxy pass headers, proxy hide headers, proxy cache background update... When you use it, study it in detail.
Reference resources:
https://blog.csdn.net/dengjiexian123/article/details/53386586