Ji Mun
Ji Mun

Reputation: 1830

Simple Reverse-proxy using nginx

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

Upvotes: 0

Views: 1128

Answers (2)

Andrey  Kopeyko
Andrey Kopeyko

Reputation: 1566

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.

Upvotes: 1

jmera
jmera

Reputation: 684

Change the default port: rails s -p 80

Upvotes: 0

Related Questions