Last active
May 8, 2018 04:37
-
-
Save fuminchao/90c96848b40fca7c6cf2e1fd50a0542e to your computer and use it in GitHub Desktop.
npm reverse proxy with cache by Nginx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Default server configuration | |
# | |
proxy_cache_path /var/cache/nginx/bb-npm-tarball levels=1:2 keys_zone=bb_npm_cache:100m max_size=10g | |
inactive=1000d use_temp_path=off; | |
log_format format-npm-proxy '$remote_addr|$upstream_cache_status|$status|$http_user_agent|' | |
'curl -X $request_method \'http://localhost:8899$request_uri\' ' | |
'-H "pacote-req-type: $http_pacote_req_type" ' | |
'-H "pacote-pkg-id: $http_pacote_pkg_id" ' | |
'-H "accept: $http_accept" ' | |
'-H "accept-encoding: $http_accept_encoding" ' | |
'-H "npm-in-ci: $http_npm_in_ci" ' | |
'-H "npm-session: $http_npm_session" ' | |
'-H "npm-scope: $http_npm_scope" ' | |
'-H "referer: $http_referer" ' | |
'-H "user-agent: npm/5.3.0 node/v8.11.0 darwin x64" ' | |
'-o /dev/null' | |
; | |
server { | |
resolver 10.75.106.4 10.102.3.40 10.75.106.10 1.1.1.1; | |
listen 8899; | |
root /var/www/html; | |
access_log /var/log/nginx/npm-proxy.log format-npm-proxy; | |
location ~ /repository/bb-npm/.*\.tgz$ { | |
proxy_cache bb_npm_cache; | |
proxy_cache_key tarball-$request_uri; | |
proxy_pass http://npm.bbpd.io$request_uri; | |
proxy_set_header Host "npm.bbpd.io"; | |
proxy_pass_header user-agent; | |
proxy_pass_header pacote-req-type; | |
proxy_pass_header pacote-pkg-id; | |
proxy_pass_header accept; | |
proxy_pass_header accept-encoding; | |
proxy_pass_header npm-in-ci; | |
proxy_pass_header npm-session; | |
proxy_pass_header npm-scope; | |
proxy_pass_header referer; | |
proxy_buffers 256 4k; | |
proxy_max_temp_file_size 0; | |
proxy_connect_timeout 30; | |
proxy_cache_valid 200 240h; | |
add_header X-Cache-Status "$upstream_cache_status 240h tarball-$request_uri$http_user_agent"; | |
} | |
location /repository/bb-npm/ { | |
proxy_cache bb_npm_cache; | |
proxy_cache_key packcument-$request_uri; | |
proxy_pass http://npm.bbpd.io$request_uri; | |
proxy_set_header Host "npm.bbpd.io"; | |
# critical for sub_filter | |
proxy_set_header Accept-Encoding "identity;q=1"; | |
proxy_pass_header user-agent; | |
proxy_pass_header pacote-req-type; | |
proxy_pass_header pacote-pkg-id; | |
proxy_pass_header accept; | |
proxy_pass_header accept-encoding; | |
proxy_pass_header npm-in-ci; | |
proxy_pass_header npm-session; | |
proxy_pass_header npm-scope; | |
proxy_pass_header referer; | |
proxy_buffers 256 4k; | |
proxy_max_temp_file_size 0; | |
proxy_connect_timeout 30; | |
proxy_cache_valid 200 2h; | |
add_header X-Cache-Status "$upstream_cache_status 2h packcument-$request_uri$http_user_agent"; | |
# rewrite hostname in response | |
sub_filter 'http://npm.bbpd.io/' 'http://learnshanghai-pd.int.bbpd.io:8899/'; | |
sub_filter_once off; | |
sub_filter_types application/json; | |
} | |
location / { | |
try_files /npm-proxy.html =404; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment