0%

nginx域名转发内网端口配置

例如转发到服务器3002端口,域名Nginx配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
location / {
allow all;
proxy_pass http://localhost:3002;
proxy_set_header Host $http_host;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
proxy_pass http://localhost:3002;
}
location ~ .*\.(js|css)?$
{
proxy_pass http://localhost:3002;
}

如果使用宝塔面板,在[伪静态]部分配置。

测试,通过node test.js来listen3002端口,访问域名后显示test.js返回值:Hello World

test.js:

1
2
3
4
5
6
7
8
9
10
11
const http = require('http');

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});

server.listen(3002, () => {
console.log('Server running at http://localhost:3002/');
});

欢迎关注我的其它发布渠道