前端analysis | 3w & 1h

《Docker》- nginx 本地代理调试线上api

2020-05-07

本地nginx 配置https访问产线api

安装nginx

1
2
$ brew search nginx
$ brew install nginx

自签证书

1
2
3
4
5
6
7
8
9
10
11
 # 其中,证书,不设置任何密码,便于nginx使用
openssl genrsa -des3 -passout pass:x -out http-ssl.pass.key 2048

openssl rsa -passin pass:x -in http-ssl.pass.key -out http-ssl.key

rm http-ssl.pass.key

openssl req -new -key http-ssl.key -out http-ssl.csr

openssl x509 -req -days 365 -in http-ssl.csr -signkey http-ssl.key -out http-ssl.crt

  • 推荐命令写入sh脚本
    1
    2
    #!/bin/bash 
    ....

本地host配置

1
127.0.0.1 api.test.com
  • 选择Finder → Go → GO TO Folder

  • 输入

    1
    /private/etc/hosts
  • copy hosts 到其余地方,进行编辑

  • 然后在copy 回来,替换即可

nginx.conf配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
server {
listen 443 ssl;
server_name *.test.com api.test.com localhost;

client_body_buffer_size 4k;
client_max_body_size 4M;
client_header_buffer_size 1k;
large_client_header_buffers 4 1k;

ssl_certificate ./ssl/http-ssl.crt;
ssl_certificate_key ./ssl/http-ssl.key;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8090;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

启动nginx

  • 启动
    1
    nginx 
  • 测试配置文件
    1
    nginx -t 
  • 重新加载配置
    1
    nginx -s reload

docker nginx配置

自签证书

  • 同上

本地host修改

  • 同上

nginx安装

1
$ docker pull nginx

nginx 配置

1

nginx 启动

1
2
3
4
5
6
7
8
9
10
11
$ docker run \
--name my-nginx \
--rm \
-d -p 8090:80 \
-v $PWD/html:/usr/share/nginx/html \
-v $PWD/ssl:/etc/nginx/ssl \
-v $PWD/nginx.conf:/etc/nginx/nginx.conf:ro \
-v $PWD/conf.d:/etc/nginx/conf.d \
-v $PWD/logs:/var/log/nginx \
nginx

  • 可以将上述命令放入sh脚本中
    #!/bin/bash 
    

docker stop my-nginx

…..

使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏