Skip to content

Instantly share code, notes, and snippets.

@xqin
Created July 17, 2019 13:15
Show Gist options
  • Save xqin/26f250fc69c1e754e553443689b74397 to your computer and use it in GitHub Desktop.
Save xqin/26f250fc69c1e754e553443689b74397 to your computer and use it in GitHub Desktop.
nginx proxy_pass example

nginx.conf

user  nginx;
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
  worker_connections  1024;
}

http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;

  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

  access_log  /var/log/nginx/access.log  main;

  keepalive_timeout  30;

  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  gzip  on;

  server {
    listen 80;
    server_name _;

    root /data;

    location / {
      try_files /$uri @source;
    }

    location @source {
      proxy_set_header Host "cdn.npm.taobao.org";

      proxy_pass http://cdn.npm.taobao.org;

      proxy_store /data/$uri;

      proxy_store_access user:rw group:r all:r;
    }
  }
}
mkdir -p data

docker run -it --rm -v $(pwd)/data:/data \
  -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro \
  -p 8888:80 \
  nginx

example:

~/Desktop/Work/taobao
$ tree .
.
├── data
└── nginx.conf

1 directory, 1 file
~/Desktop/Work/taobao
$ time curl -s -o /dev/null http://127.0.0.1:8888/dist/sqlite3/v3.1.6/node-v48-win32-x64.tar.gz

real    0m1.250s
user    0m0.006s
sys     0m0.008s
~/Desktop/Work/taobao
$ time curl -s -o /dev/null http://127.0.0.1:8888/dist/sqlite3/v3.1.6/node-v47-linux-x64.tar.gz

real    0m1.641s
user    0m0.006s
sys     0m0.007s
~/Desktop/Work/taobao
$ tree .
.
├── data
│   └── dist
│       └── sqlite3
│           └── v3.1.6
│               ├── node-v47-linux-x64.tar.gz
│               └── node-v48-win32-x64.tar.gz
└── nginx.conf

4 directories, 3 files
~/Desktop/Work/taobao
$ time curl -s -o /dev/null http://127.0.0.1:8888/dist/sqlite3/v3.1.6/node-v47-linux-x64.tar.gz

real    0m0.039s
user    0m0.005s
sys     0m0.007s
~/Desktop/Work/taobao
$ time curl -s -o /dev/null http://127.0.0.1:8888/dist/sqlite3/v3.1.6/node-v47-linux-x64.tar.gz

real    0m0.036s
user    0m0.005s
sys     0m0.005s
@xqin
Copy link
Author

xqin commented Jul 17, 2019

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment