使用 NGINX 进行动态路由
Dynamic routing with NGINX
我是 NGINX 的业余爱好者,我想将 NGINX 设置为我的网络服务器的反向代理。
我想知道 NGINX 这些东西如下所列:
当浏览器发送带有 URL 的请求时:http://nginxproxy.com/client/1.2.3.4/, this request should be passed to the client with IP 1.2.3.4 http://1.2.3.4/,浏览器应该仍然显示 URL nginxproxy/client/1.2.3.4/
同样适用于:
- nginxproxy.com/client/2.3.4.5 --> //2.3.4.5
- nginxproxy.com/client/2.3.4.6 --> //2.3.4.6
所有其他不符合模式的请求都应该发送到我的默认服务器 myserver。
我可以使用 NGINX 来实现吗?
经过研究,我尝试了以下配置:
但不幸的是,它不起作用。地址在浏览器的地址栏上更改为 http:/1.2.3.4,而不是预期的 http:/nginxproxy.com/client/1.2.3.4.
server {
listen 80;
location ~ ^/client {
rewrite ^/client/?(.*) / break;
proxy_pass $scheme://;
}
location / {
proxy_pass http://myserver.com;
}
}
非常感谢任何帮助。
server {
listen 80;
location /client/ {
rewrite ^/client/(?<site>[^/]+)/? $scheme://$site;
}
location / {
proxy_pass $scheme://myserver.com;
}
}
根据@Cole 的输入进行更多研究,这是我的答案:
location ~ ^/client/(?<site>[^/]+)/? {
rewrite ^.*\/client\/(?<site>[^\/]+)\/?(.*) / break; #passing all the remaining request URIs after <site> group to client server
proxy_pass $scheme://$site;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host/client/$site; #this help to keep the address as it is on the browser's address bar
proxy_set_header X-Forwarded-Proto $scheme;
}
location / {
proxy_pass $scheme://myserver.com
}
我是 NGINX 的业余爱好者,我想将 NGINX 设置为我的网络服务器的反向代理。 我想知道 NGINX 这些东西如下所列:
当浏览器发送带有 URL 的请求时:http://nginxproxy.com/client/1.2.3.4/, this request should be passed to the client with IP 1.2.3.4 http://1.2.3.4/,浏览器应该仍然显示 URL nginxproxy/client/1.2.3.4/ 同样适用于:
- nginxproxy.com/client/2.3.4.5 --> //2.3.4.5
- nginxproxy.com/client/2.3.4.6 --> //2.3.4.6
所有其他不符合模式的请求都应该发送到我的默认服务器 myserver。
我可以使用 NGINX 来实现吗?
经过研究,我尝试了以下配置: 但不幸的是,它不起作用。地址在浏览器的地址栏上更改为 http:/1.2.3.4,而不是预期的 http:/nginxproxy.com/client/1.2.3.4.
server {
listen 80;
location ~ ^/client {
rewrite ^/client/?(.*) / break;
proxy_pass $scheme://;
}
location / {
proxy_pass http://myserver.com;
}
}
非常感谢任何帮助。
server {
listen 80;
location /client/ {
rewrite ^/client/(?<site>[^/]+)/? $scheme://$site;
}
location / {
proxy_pass $scheme://myserver.com;
}
}
根据@Cole 的输入进行更多研究,这是我的答案:
location ~ ^/client/(?<site>[^/]+)/? {
rewrite ^.*\/client\/(?<site>[^\/]+)\/?(.*) / break; #passing all the remaining request URIs after <site> group to client server
proxy_pass $scheme://$site;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host/client/$site; #this help to keep the address as it is on the browser's address bar
proxy_set_header X-Forwarded-Proto $scheme;
}
location / {
proxy_pass $scheme://myserver.com
}