The Nginx agent forwards the implementation code uploaded by ali cloud OSS

  • 2020-05-15 03:29:46
  • OfStack

preface

Because small program upload requires https, server https USES the certificate generated by letsencrypt, but aliyun oss does not do https (lazy to upload the certificate), so it wants to use Nginx agent to forward the upload request.

Nginx configuration


# HTTPS server
#
 server {
  listen  443 ssl;
  server_name your.domain.name;

  ...

  location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $Host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For 
  }

  location /oss {
    proxy_set_header Host drift-book-dev.oss-cn-shenzhen.aliyuncs.com;
    proxy_set_header Connection keep-alive;
    proxy_pass http://***.oss-cn-***.aliyuncs.com/;
    #proxy_set_header X-Real-IP $remote_addr;
    #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
 }

Here, the child path"/oss" is used as the forwarding path.

proxy_pass specify your aliyun domain name, remember to use the slash "/" in the following 1, otherwise the forwarding will fail;

nginx is configured with proxy_pass proxy forwarding

Assume that the following 4 kinds of circumstances respectively with http: / / 192.168.1.1 proxy/test html for a visit.

1 species:


location /proxy/ { 
  proxy_pass http://127.0.0.1/; 
} 

Agent to URL: http: / / 127.0.0.1 test html

Type 2 (1 less at the end than type 1)


location /proxy/ { 
  proxy_pass http://127.0.0.1; 
} 

Agent to URL: http: / / 127.0.0.1 proxy/test html

Third:


location /proxy/ { 
  proxy_pass http://127.0.0.1/aaa/; 
} 

Agent to URL: http: / / 127.0.0.1 aaa/test html

Type 4 (1 less than type 3)


location /proxy/ { 
  proxy_pass http://127.0.0.1/aaa; 
} 

Agent to URL: http: / / 127.0.0.1 aaatest html


Related articles: