0

I'm trying to redirect my domain url to my local server.

For some reason it work with the localhost (mapping from http://localhost/ to http://127.0.0.1:5000 works), but I can't make it working with my domain name (http://www.example.com)

Here's my nginx conf:

    ...
server {

        listen       80;
        server_name  www.example.com localhost;

        location / {
           proxy_pass http://127.0.0.1:5000;
           proxy_set_header Host $host;
        }
    }
...

Is there's something I am missing?

edit

It seems that the problem was about the reolution of 0.0.0.0, localhost and 127.0.0.1 . Rewriting my proxy_pass to http://0.0.0.0:5000 did the trick. Inversely with my server as long as it match exactly the nginx conf.

My error was because I thought 0.0.0.0 / 127.0.0.1 / was interchangeable. Thanks for your help.

Mr Bonjour
  • 121
  • 1
  • 1
  • 4
  • Do you have a DNS entry for `www.example.com` that goes to the IP of this server? – shinjijai Mar 14 '18 at 17:22
  • No I didn't configure my DNS. The only thing I did is use my router conf to point to the correct computer. So when my server point to port 80, I can access to my server through www.example.com. And if I start nginx with the default configuration, I can hit the nginx welcome page through my domain too. What I can't achieve is map www.example.com to my local server from Nginx. Do you think I have to configure my local DNS? – Mr Bonjour Mar 14 '18 at 17:42
  • Without the DNS entry to point www.example.com to the server you're running the nginx to, your request will never make it to your nginx server to begin with for your nginx to proxy. If you want to test this out, and not update your DNS, just edit your host file so www.example.com points to your server and see what happens. – shinjijai Mar 14 '18 at 17:45
  • I don't really understand what do you mean exactly by dns resolution. If I launch my server as 'sudo gunicorn -w 2 app:app -b 0.0.0.0:80' it will works from my domaine name. Same if I launch Nginx in standalone (I will get my Nginx welcome page). So the dns works. What is not working is asking Nginx to redirect to my server through my rule, even if it works with 'local'. When you're talking about DNS? Are you talking about my local one on my machine? – Mr Bonjour Mar 14 '18 at 17:55
  • If you ping www.example.com, where does it resolve to? Does it resolve to your server's IP? or nothing? or somewhere else? – shinjijai Mar 14 '18 at 17:57
  • What is the problem you are having? – Michael Hampton Mar 14 '18 at 18:03
  • yes. For detail, 'ping cryptodraco.hd.free.fr' (the name on my domain) works. And it resolves on my static ip, local machine – Mr Bonjour Mar 14 '18 at 18:06
  • And your nginx server lives on `82.237.110.182` that's listening for crytodraco.hd.free.fr? If that's the case, take a look at the access.log for nginx, and go from there. – shinjijai Mar 14 '18 at 18:08
  • No use, you can ping, but port 80 is closed. Nobody's home but us chickens. – Gerard H. Pille Mar 14 '18 at 18:09
  • It was closed. But now it is open. Now you have ever a 502 or Nginx welcom page – Mr Bonjour Mar 14 '18 at 18:14
  • From my access log, I can retrieve the request header 192.168.0.254 - - [14/Mar/2018:19:18:26 +0100] "GET / HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36" – Mr Bonjour Mar 14 '18 at 18:19
  • and from the error log: 2018/03/14 19:18:26 [error] 22425#0: *18 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 192.168.0.254, server: cryptodraco.hd.free.fr, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "cryptodraco.hd.free.fr" – Mr Bonjour Mar 14 '18 at 18:20
  • So now there's nobody listening on port 5000 of localhost. Do a "lsof -Pn -p XXXX" where XXXX is the pid of your server. – Gerard H. Pille Mar 14 '18 at 18:28
  • I got an answer now. Kiabi Pma Manager. But now port 80 is closed again. I got the answer with telnet and a "GET /" – Gerard H. Pille Mar 14 '18 at 18:30
  • 1
    I don't believe "proxy_pass to http://0.0.0.0:5000 did the trick". – Gerard H. Pille Mar 14 '18 at 20:25

1 Answers1

1

add a entry to /etc/hosts for your server_name www.example.com

127.0.0.1  www.example.com

its working for localhost because localhost is mapped by default in /etc/hosts/ file

Jenny D
  • 27,358
  • 21
  • 74
  • 110
Ravi Sevta
  • 111
  • 3