如何 运行 在 localhost 中使用 nginx 的快速服务器?
How to run a express server using nginx in localhost?
我想 运行 在本地主机中使用 Nginx 的快速服务器。
可能吗?如果可能,我该如何实现?
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Server listening at http://localhost:${port}`))
如果您想使用 nginx 作为 express 的反向代理,您可以按如下方式配置您的服务器:
server {
listen 80 default_server;
server_name _;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:3000; #port where you are serving your express app.
}
}
您应该在 /etc/nginx/sites-available/default
中进行配置
修改配置文件后,检查脚本是否有错误:
$sudo nginx -t
预期结果应为:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
配置好后,你应该重启你的nginx实例,使用命令:
$sudo nginx -s reload
正确重新加载后断言您的 node/express 服务器是 运行。现在,如果您访问 http://localhost
,您应该会看到 http://localhost:3000
中正在监听什么
Nginx 应该为您的实例提供服务,当您停止节点/express 时,您应该会看到默认错误 502。
我想 运行 在本地主机中使用 Nginx 的快速服务器。 可能吗?如果可能,我该如何实现?
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Server listening at http://localhost:${port}`))
如果您想使用 nginx 作为 express 的反向代理,您可以按如下方式配置您的服务器:
server {
listen 80 default_server;
server_name _;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:3000; #port where you are serving your express app.
}
}
您应该在 /etc/nginx/sites-available/default
修改配置文件后,检查脚本是否有错误:
$sudo nginx -t
预期结果应为:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
配置好后,你应该重启你的nginx实例,使用命令:
$sudo nginx -s reload
正确重新加载后断言您的 node/express 服务器是 运行。现在,如果您访问 http://localhost
,您应该会看到 http://localhost:3000
Nginx 应该为您的实例提供服务,当您停止节点/express 时,您应该会看到默认错误 502。