0

I am running a Rails app on http://localhost:3000, which I want to access as http://localhost. I am trying to use nginx to do reverse-proxy, and it seems like it should be simple but I haven't gotten it to work. I can access my app at http://localhost:3000 but I get no response at http://localhost. This is my first time toying with nginx so I am not sure what's wrong, where to look for more details. I also looked at 10+ Stack Overflow questions, nginx tutorials and questions but none of them have helped me so far.

Here's a snippet of /etc/nginx/nginx.conf

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
    # multi_accept on;
}

http {
    include       /etc/nginx/mime.types;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

The only .conf file in the conf.d directory: /etc/nginx/conf.d/default.conf

upstream backend {
    server 127.0.0.1:3000;
}

server {
    listen 80;

    server_name localhost;

    access_log  /var/log/nginx/localhost.access.log;
    location / {
        proxy_pass http://backend;
        proxy_redirect off;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto  http;
        proxy_set_header X-NginX-Proxy true;
    }
}

I followed this page in setting it up.

Also, Content of /etc/nginx/sites-enabled/default, also the only file in sites-enabled directory

server {
    listen   80 default;
    server_name  localhost;

    access_log  /var/log/nginx/localhost.access.log;

    location / {
            root   /var/www/nginx-default;
            index  index.html index.htm;
    }

    location /doc {
            root   /usr/share;
            autoindex on;
            allow 127.0.0.1;
            deny all;
    }

    location /images {
            root   /usr/share;
            autoindex on;
    }
}

And when I restart nginx... (the output)

$ sudo service nginx restart
Restarting nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
configuration file /etc/nginx/nginx.conf test is successful
nginx.

And I see this in the log:

2014/10/15 23:28:12 [warn] 5800#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
4
  • Is it required that you use nginx? There are other options if all you want to do is access your webapp on port 80 rather than the default 3000. Commented Oct 15, 2014 at 21:37
  • I can use anything that will do the port redirecting. What do you suggest? @jmera Commented Oct 15, 2014 at 21:43
  • 1
    I wasn't suggesting port redirecting. I was suggesting just running the rails app on port 80 on your dev machine: rails s -p 80. To achieve this you don't need nginx at all... usually, in a dev environment, you don't need an http server like nginx or apache. Commented Oct 15, 2014 at 21:45
  • That does address my need in a simple way! Thank you! Commented Oct 15, 2014 at 22:07

2 Answers 2

1

The problem is that server localhost:80 is defined twice :

  • in "conf.d/default.conf"
  • and in "sites-enabled/default"

and nginx gets confused when request to "localhost:80" arrives - which server should serve the request???

You should replace the server block in "conf.d/default.conf" file with content of the server block in your "sites-enabled/default" (removing it from there).

Because "conf.d/*.conf" files are intended for configuring modules\features, not for servers being hosted descriptions.

Sign up to request clarification or add additional context in comments.

Comments

0

Change the default port: rails s -p 80

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.