Skip to content

Instantly share code, notes, and snippets.

@Ivlyth
Created November 18, 2016 17:11
Show Gist options
  • Save Ivlyth/e3fe9ab6009bcacdde33d3ac8e82c3a5 to your computer and use it in GitHub Desktop.
Save Ivlyth/e3fe9ab6009bcacdde33d3ac8e82c3a5 to your computer and use it in GitHub Desktop.
使用 SSH 隧道将内网服务暴露在公网上(主要用于临时调试等目的, 不推荐长时间的内网服务暴露)
# 使用了 lua 模块的 nginx 配置文件
# 推荐使用 openresty 或者自行编译 nginx with lua
lua_shared_dict proxy_ports 5m;
server {
listen 80;
server_name *.hproxy.yourdomain.com;
set $real_host $http_host;
if ($http_host ~* "(.*).hproxy.yourdomain.com") {
set $real_host "$1.yourdomain.com";
}
set $local_port "";
if ($query_string ~* "port=([0-9]+)") {
set $local_port $1;
}
if ($local_port != ""){
set_by_lua_block $res {
local proxy_ports = ngx.shared.proxy_ports;
proxy_ports:set(ngx.var.real_host, ngx.var.local_port);
return ngx.var.local_port;
}
}
if ($local_port = "") {
set_by_lua_block $local_port {
local proxy_ports = ngx.shared.proxy_ports;
return proxy_ports:get(ngx.var.real_host);
}
}
if ($local_port = ""){
set $local_port 59999;
}
set $proxy_address "http://127.0.0.1:${local_port}";
location / {
proxy_set_header Host $real_host;
proxy_pass $proxy_address;
}
}
# nginx 配置文件, 默认的nginx安装即可支持
server {
listen 80;
server_name *.pproxy.yourdomain.com;
set $local_port "";
if ($http_host ~* "([0-9]+).pproxy.yourdomain.com") {
set $local_port $1;
}
if ($local_port = ""){
set $local_port 59999;
}
set $proxy_address "http://127.0.0.1:${local_port}";
location / {
proxy_set_header Host $real_host;
proxy_pass $proxy_address;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment