背景:
我们在项目中在这样一个场景,用户需要使用固定的软件资源,这些资源是以服务器或者以容器形式存在的。
资源以webAPI方式在内网向外提供接口,资源分类多种类型,每种类型的资源程序和Wapi参数都一样。这些资源部属完成后使用IP+端口进行区分。如下表所示
技术分析:
经过调研分析,发现Nginx 可以进行么向代理,代理后可以进行调度到内部的数据
技术验证:
1.使用静态方式在配置文件中直接写入
如下:
server {
listen 22280;
server_name localhost;
root "D:/dist";
location / {
index index.php index.html error/index.html;
error_page 400 /error/400.html;
error_page 403 /error/403.html;
error_page 404 /error/404.html;
error_page 500 /error/500.html;
error_page 501 /error/501.html;
error_page 502 /error/502.html;
error_page 503 /error/503.html;
error_page 504 /error/504.html;
error_page 505 /error/505.html;
error_page 506 /error/506.html;
error_page 507 /error/507.html;
error_page 509 /error/509.html;
error_page 510 /error/510.html;
#include D:/Git/111/nginx.htaccess;
autoindex off;
try_files $uri $uri/ @router; #需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
index index.html index.htm;
}
#反向代理
location /r1/ {
proxy_pass http://192.168.1.88:888/;
}
location /r2/ {
proxy_pass http://192.168.1.88:887/;
}
location /i1/ {
proxy_pass http://192.168.1.89:3333/;
}
location /i2/ {
proxy_pass http://192.168.1.90:3333/;
}
#对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
#因此需要rewrite到index.html中,然后交给路由在处理请求资源
location @router {
rewrite ^.*$ /index.html last;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
使用此方式可以达到所需效果,但如果资源变化需要手动调整Nginx的配置文件。因此考虑有没有一种办法可以做到动态转发呢?
问题难点:
1.资源需要一定的规律性
2.nginx动态转发功能技术上需要进行预研,明确是否可行
实现过程:
1.确定方案
方案里我们准备采用 固定内容+端口号的形式来进行处理,在此之前我们需要把服务用K8S映射到
同一IP或者同一规则的内容上。并且对URL进行了定义 /rs/(\d+)/实际API 的形式进行调用
2.方案实现
我们假定所有的 k8s 把所有的服务地址都映射到了 192.168.1.88上,仅端口不同,使用不同的端口就可以调度到不同的服务。
部分配置如下:
location /rs/(\d.+)/ {
proxy_pass http://localhost:$/;
}
运行报错 http 500
500 Internal Server Error
nginx/1.15.11
问题处理:
2024/03/22 17:07:35 [error] 5072#34752: *264 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:07:35 [error] 5072#34752: *265 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:08:15 [error] 5072#34752: *268 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:08:16 [error] 5072#34752: *269 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:08:16 [error] 5072#34752: *270 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:08:17 [error] 5072#34752: *271 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:09:24 [error] 5072#34752: *273 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:09:24 [error] 5072#34752: *274 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:09:25 [error] 5072#34752: *275 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:09:26 [error] 5072#34752: *276 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:09:27 [error] 5072#34752: *277 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:09:27 [error] 5072#34752: *278 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:09:35 [error] 5072#34752: *283 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:09:36 [error] 5072#34752: *284 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:09:36 [error] 5072#34752: *285 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:09:37 [error] 5072#34752: *286 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:09:38 [error] 5072#34752: *287 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:09:38 [error] 5072#34752: *288 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280"
2024/03/22 17:10:00 [error] 5072#34752: *279 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /uuu/60000/api/sysAuth/index.html HTTP/1.1", upstream: "http://192.168.27.51:60000/", host: "127.0.0.1:280"
2024/03/22 17:10:00 [error] 5072#34752: *279 rewrite or internal redirection cycle while processing "/index.html", client: 127.0.0.1, server: localhost, request: "GET /uuu/60000/api/sysAuth/index.html HTTP/1.1", upstream: "http://192.168.27.51:60000/", host: "127.0.0.1:280"
2024/03/22 17:10:01 [error] 5072#34752: *280 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280", referrer: "http://127.0.0.1:280/uuu/60000/api/sysAuth/index.html"
2024/03/22 17:11:05 [error] 5072#34752: *290 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /uuu/60000/api/sysAuth/index.html HTTP/1.1", upstream: "http://192.168.27.51:60000/", host: "127.0.0.1:280"
2024/03/22 17:11:05 [error] 5072#34752: *290 rewrite or internal redirection cycle while processing "/index.html", client: 127.0.0.1, server: localhost, request: "GET /uuu/60000/api/sysAuth/index.html HTTP/1.1", upstream: "http://192.168.27.51:60000/", host: "127.0.0.1:280"
2024/03/22 17:11:05 [error] 5072#34752: *293 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "127.0.0.1:280", referrer: "http://127.0.0.1:280/uuu/60000/api/sysAuth/index.html"
2024/03/22 17:14:18 [error] 8748#17732: *1 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /uuu/60000/index.html HTTP/1.1", upstream: "http://192.168.27.51:60000/uuu/60000/index.html", host: "localhost:280"
2024/03/22 17:14:18 [error] 8748#17732: *1 rewrite or internal redirection cycle while processing "/index.html", client: 127.0.0.1, server: localhost, request: "GET /uuu/60000/index.html HTTP/1.1", upstream: "http://192.168.27.51:60000/uuu/60000/index.html", host: "localhost:280"
2024/03/22 17:14:19 [error] 8748#17732: *5 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:280", referrer: "http://localhost:280/uuu/60000/index.html"
2024/03/22 17:15:59 [error] 12116#31400: *4 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /uuu/60000/aaaa HTTP/1.1", upstream: "http://192.168.27.51:60000/", host: "localhost:280"
2024/03/22 17:15:59 [error] 12116#31400: *4 rewrite or internal redirection cycle while processing "/index.html", client: 127.0.0.1, server: localhost, request: "GET /uuu/60000/aaaa HTTP/1.1", upstream: "http://192.168.27.51:60000/", host: "localhost:280"
2024/03/22 17:15:59 [error] 12116#31400: *7 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:280", referrer: "http://localhost:280/uuu/60000/aaaa"
2024/03/22 17:20:20 [error] 33904#20644: *1 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /uuu/60000/api/sysAuth/captcha HTTP/1.1", upstream: "http://192.168.27.51:60000/", host: "localhost:280"
2024/03/22 17:20:20 [error] 33904#20644: *1 rewrite or internal redirection cycle while processing "/index.html", client: 127.0.0.1, server: localhost, request: "GET /uuu/60000/api/sysAuth/captcha HTTP/1.1", upstream: "http://192.168.27.51:60000/", host: "localhost:280"
2024/03/22 17:20:20 [error] 33904#20644: *4 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:280", referrer: "http://localhost:280/uuu/60000/api/sysAuth/captcha"
2024/03/22 17:24:31 [error] 41040#27804: *2 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:280", referrer: "http://localhost:280/uuu/5257/api/sysAuth/captcha"
2024/03/22 17:24:35 [error] 41040#27804: *4 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /_framework/aspnetcore-browser-refresh.js HTTP/1.1", host: "localhost:280", referrer: "http://localhost:280/uuu/5257/index.html"
2024/03/22 17:24:35 [error] 41040#27804: *11 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /index-mini-profiler/includes.min.js?v=4.3.8+1120572909 HTTP/1.1", host: "localhost:280", referrer: "http://localhost:280/uuu/5257/index.html"
2024/03/22 17:24:35 [error] 41040#27804: *12 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /_vs/browserLink HTTP/1.1", host: "localhost:280", referrer: "http://localhost:280/uuu/5257/index.html"
2024/03/22 17:24:35 [error] 41040#27804: *13 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /swagger/%E5%85%AC%E5%85%B1%E6%9C%8D%E5%8A%A1/swagger.json HTTP/1.1", host: "localhost:280", referrer: "http://localhost:280/uuu/5257/index.html"
2024/03/22 17:35:40 [error] 41512#41416: *8 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /_framework/aspnetcore-browser-refresh.js HTTP/1.1", host: "localhost:280", referrer: "http://localhost:280/area/5257/index.html"
2024/03/22 17:35:40 [error] 41512#41416: *10 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /_vs/browserLink HTTP/1.1", host: "localhost:280", referrer: "http://localhost:280/area/5257/index.html"
2024/03/22 17:35:40 [error] 41512#41416: *9 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /index-mini-profiler/includes.min.js?v=4.3.8+1120572909 HTTP/1.1", host: "localhost:280", referrer: "http://localhost:280/area/5257/index.html"
2024/03/22 17:35:40 [error] 41512#41416: *2 rewrite or internal redirection cycle while redirect to named location "@router", client: 127.0.0.1, server: localhost, request: "GET /swagger/%E5%85%AC%E5%85%B1%E6%9C%8D%E5%8A%A1/swagger.json HTTP/1.1", host: "localhost:280", referrer: "http://localhost:280/area/5257/index.html"
通过日志发现存在循环引用之类的
考虑使用URL重写改变下,路径。再使用proxy_pass反向代理。尝试修改nginx并进行修改。
问题解决
location ~ ^/rs/(\d.+)/{
rewrite ^/rs/(\d+)/(.*)$ /$2 break;
proxy_pass http://192.168.1.88:$1;
}
最终结果能够正常转发到指定端口,并且nginx 也不用修改配置,实现了动态端口反向代理。
在这个过程中我发现要实现/dir/port/xxx 这种进行反向代理必须与url重写结合使用。不然会报404.
完整能够成功运行的配置nginx.conf如下
server {
listen 22280;
server_name localhost;
root "D:/dist";
location / {
index index.php index.html error/index.html;
error_page 400 /error/400.html;
error_page 403 /error/403.html;
error_page 404 /error/404.html;
error_page 500 /error/500.html;
error_page 501 /error/501.html;
error_page 502 /error/502.html;
error_page 503 /error/503.html;
error_page 504 /error/504.html;
error_page 505 /error/505.html;
error_page 506 /error/506.html;
error_page 507 /error/507.html;
error_page 509 /error/509.html;
error_page 510 /error/510.html;
#include D:/Git/111/nginx.htaccess;
autoindex off;
try_files $uri $uri/ @router; #需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
index index.html index.htm;
}
#反向代理
location ~ ^/rs/(\d.+)/{
rewrite ^/rs/(\d+)/(.*)$ /$2 break;
proxy_pass http://192.168.1.88:$1;
}
#对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
#因此需要rewrite到index.html中,然后交给路由在处理请求资源
location @router {
rewrite ^.*$ /index.html last;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}