小编Ber*_*ier的帖子

nginx :根据域前缀重定向到端口(动态)

我正在尝试使用 nginx 根据域前缀重定向到端口(运行 NodeJS 应用程序)。到目前为止,我可以重定向

  • example.com:80 --> 端口 8502
  • 5555.example.com:80 --> 端口 5555
  • 6666.example.com:80 --> 端口 6666

有没有一种方法可以进行这种重定向,而不必一遍又一遍地复制粘贴?

server {
  listen 80;
  server_name 5555.example.com;
  location / {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_http_version 1.1;
    proxy_pass http://example.com:5555;
  }
}
Run Code Online (Sandbox Code Playgroud)

我想我应该用正则表达式来做到这一点,所以我尝试了以下方法,但没有成功:

~^(?<theport>.+)\.example\.com$   #then changed proxy_pass to http://example.com:$theport


~^([0-9]+)\.example\.com$  #then changed proxy_pass to http://example.com:$1


server_name "~^([0-9]{4})\.example\.com$";
set $theport $1;  #then changed proxy_pass to http://example.com:$theport
Run Code Online (Sandbox Code Playgroud)

在所有情况下,我都会收到“502 Bad Gateway”错误。

nginx node.js

4
推荐指数
1
解决办法
4030
查看次数

标签 统计

nginx ×1

node.js ×1