node.js - Nginx - 将所有内容重定向到特定端点

标签 node.js nginx

我在配置 Nginx 以重写对特定端点的所有请求时遇到一些麻烦。

我有一个 Nodejs 应用程序在端口 8080 上运行,我想要所有 http://SERVER_IP/ * 请求显示http://127.0.0.1:8080/statuscheck

我尝试过(在/etc/nginx/sites-available/default 文件中):

location / {
    rewrite ^.*$ /statuscheck last;
    proxy_pass http://127.0.0.1:8080;
}

但是我得到了 500。错误日志显示:

[error] 25756#0: *115803 rewrite or internal redirection cycle while processing "/statuscheck" 

感谢您的帮助。

最佳答案

rewrite 指令中的正则表达式也与 /statuscheck 匹配,因此任何导致表达式被多次求值的重写或重定向都会导致循环。其中包括标志参数:lastredirectpermanent

但是,break 标志参数会停止处理 rewrite 指令并继续处理当前位置内的 URL。例如:

location / {
    rewrite ^ /statuscheck break;
    proxy_pass http://127.0.0.1:8080;
}

参见this document了解详情。

关于node.js - Nginx - 将所有内容重定向到特定端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38420255/

相关文章:

javascript - 连接API的WebApp与后端渲染的区别

docker - docker中的nginx无法与localhost对话

nginx - 如何关闭内网 IP 的 gzip 内容编码?

node.js - 为什么我必须使用 "npm start x"启动某些脚本,而其他脚本仅使用 "npm x"启动?

javascript - node.js 文件写入位置参数不起作用

c - 使用 NGINX 运行 C FastCGI 脚本

sockets - 配置 NGINX 以使用 SSL 包装非 SSL 服务

codeigniter - nginx codeigniter 502 网关错误

android - Cordova 不是一个有效的任务

node.js - 使用 NodeJS 的 WooCommerce API 添加了不完整的产品变体