配置 nginx 将外部端口转发到同一内部端口

Ste*_*ett 2 nginx reverse-proxy port-forwarding

我正在运行 TileMill,它正在从本地主机监听端口 20008 和 20009。我希望 20009 只能通过端口 80 上的 nginx 访问(使用简单的身份验证)。我希望 20008 可以从外部“直接”访问。

server {
   listen 80;
   server_name localhost;
   location / {
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:20009;
        auth_basic "Restricted";
        auth_basic_user_file htpasswd;
    }
}
server {
   listen 20008;
   server_name localhost;
   location / {
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:20008;
    }
}
Run Code Online (Sandbox Code Playgroud)

明显的问题是 nginx 无法侦听端口 20008 - 它已被 TileMill 使用。

Restarting nginx: nginx: [emerg] bind() to 0.0.0.0:20008 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:20008 failed (98: Address already in use)
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以让 nginx 可以在外部侦听而不与内部侦听的其他服务发生冲突?(更改localhost为服务器的外部IP不起作用。)

小智 5

更改您的监听语句以包含您的外部 IP,例如:

听1.2.3.4:20008;