Skip to content

JE-lee/nginx-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

如何使用 nginx 反向代理,并处理 302 重定向

proxy_pass + error_page + internal block

1.启动目标服务器

提供 '/redirect-to-github' 接口 该接口返回 302 重定向到 https://github.com

node server.js

2.启动 nginx

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

3.访问 'http://localhost:8888/proxy?url=http://host.docker.internal:7777/redirect-to-baidu'

njs module

  1. 启动目标服务器

提供 '/redirect-to-baidu' 接口 该接口返回 302 重定向到 https://www.baidu.com

node server.js
  1. 启动 nginx
docker run -it --rm --name nginx-njs -v $(pwd)/nginx-njs.conf:/etc/nginx/nginx.conf -v $(pwd)/http.js:/etc/nginx/http.js -p 8888:80 nginx:latest
  1. 访问 'http://localhost:8888/proxy?url=http://host.docker.internal:7777/redirect-to-baidu'

在 njs 中使用 ngx.fetch 请求 https 地址的时候,需要预先提供可信任的 CA 证书

location /proxy {
    js_content http.handle_proxy;
    js_fetch_trusted_certificate /etc/ssl/certs/GlobalSign_Root_CA.pem; # www.baidu.com 所使用的根证书
    add_header Access-Control-Allow-Origin *;
}

如上,我们需要 ngx.fetch https://www.baidu.com,那么我们就指定该域名的根证书为受信任。 也可以指定所有的系统的系统根证书

location /proxy {
    js_content http.handle_proxy;
    js_fetch_trusted_certificate /etc/ssl/certs/ca-certificates.crt; # 系统根证书
    add_header Access-Control-Allow-Origin *;
}

但是这样会导致性能问题,最好的做法是只信任我们需要的证书。pem 文件里面可以包含多个证书,nginx 会自动处理。

或者是禁用 fetch ssl verification (不推荐,仅调试用)

location /proxy {
    js_content http.handle_proxy;
    js_fetch_verify off;
    add_header Access-Control-Allow-Origin *;
}

About

如何使用 nginx 反向代理,并处理 302 重定向

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published