Natu Myers
Natu Myers

Reputation: 496

Nginx Redirect Rules Not Working for Node App

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;
    }
 }

Upvotes: 0

Views: 184

Answers (1)

Caio Leonhardt
Caio Leonhardt

Reputation: 100

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");

Upvotes: 1

Related Questions