Nginx重定向规则不适用于节点应用程序(Nginx Redirect Rules Not Working for Node App)

编程入门 行业动态 更新时间:2024-10-28 18:33:08
Nginx重定向规则不适用于节点应用程序(Nginx Redirect Rules Not Working for Node App)

它重定向所有人,包括my.personal.ip.address,但它只应该将我以外的人重定向到博客。 当我把它放在默认的nginx文件中时,它正在工作,但现在我正在尝试让我的节点应用程序运行,我不确定在哪里放置if语句以及为什么它不起作用。

# the IP(s) on which your node server is running. I chose port 3000. upstream mywebsite { server 127.0.0.1:3000; keepalive 8; } # the nginx server instance server { listen 0.0.0.0:80; server_name mywebsite.ca mywebsite; access_log /usr/share/nginx/html/yourdomain.log; # pass the request to the node.js server with the correct headers # and much more can be added, see nginx config options location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_pass http://mywebsite/; if ($remote_addr != my.personal.ip.address){ rewrite ^ http://blog.mywebsite.ca; } proxy_redirect off; } }

It redirects everyone including my.personal.ip.address but it's only supposed to redirect people besides me to the blog. It was working when I put it in the default nginx file but now that I'm trying to get my node app to run, I'm unsure where to put the if statement and why it won't work.

# the IP(s) on which your node server is running. I chose port 3000. upstream mywebsite { server 127.0.0.1:3000; keepalive 8; } # the nginx server instance server { listen 0.0.0.0:80; server_name mywebsite.ca mywebsite; access_log /usr/share/nginx/html/yourdomain.log; # pass the request to the node.js server with the correct headers # and much more can be added, see nginx config options location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_pass http://mywebsite/; if ($remote_addr != my.personal.ip.address){ rewrite ^ http://blog.mywebsite.ca; } proxy_redirect off; } }

最满意答案

您的节点应用程序是否也在侦听127.0.0.1?

http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(3000, "127.0.0.1");

Is your node app listening on 127.0.0.1, too ?

http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(3000, "127.0.0.1");

更多推荐

本文发布于:2023-04-28 09:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   应用程序   重定向   不适用于   规则

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!