先寫proxy不用憑證版的設定
a2a-proxy的nginx.conf如下:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 8443 ssl;
server_name _;
ssl_certificate /etc/nginx/certs/a2a-proxy.crt;
ssl_certificate_key /etc/nginx/certs/a2a-proxy.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location ^~ /a2a/ {
proxy_pass http://a2a-api:8081;
proxy_http_version 1.1;
# 在 /a2a/ 與相關 location 加入
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
# 改寫後端回傳的 Location(視後端回傳 host 調整)
proxy_redirect http://a2a-api:8081/ https://$host:8443/a2a/;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
location / {
proxy_pass http://a2a-web;
proxy_http_version 1.1;
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_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Port 8443;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
}
}
ssl設定過程雖然複雜,但網路也有不少相同主題的文章可參考。這個留到明天講。
主要有兩點:
1.proxy_pass:在location根目錄設定是:proxy_pass http://a2a-web,容器中透過同個NetworkName設定(即a2a-net)可以導向ContainerName:ContainerPort。如以下a2a-web.container的設定,導向預設80 port:
ContainerName=a2a-web
PublishPort=8080:80
2.proxy_redirect:在location /a2a/目錄設定是:
proxy_pass http://a2a-api:8081;
# 改寫後端回傳的 Location(視後端回傳 host 調整)
proxy_redirect http://a2a-api:8081/ https://$host:8443/a2a/;
這裡proxy_redirect的設定就符合昨天講的,以下我描述更詳細點:
F5 -> a2a-proxy:8443 -> a2a-web:80 -> a2a-proxy:8081/a2a -> a2a-api:8081/a2a
在前端的a2a-web呼叫後端的a2a-api時,URI都是/a2a,所以前端程式呼叫a2a-api,可以a2a-api:8081/a2a或a2a-proxy:8081/a2a ,這兩者是等值的。
從瀏覽器呼叫不經過F5:
https://RedHatIP:8443 會被a2a-proxy:8443 導向a2a-web:80
https://RedHatIP:8443/a2a 會被a2a-proxy:8443/a2a 導向a2a-api:8081