siwsonu
By:
siwsonu

Why I need include port with the domain/ip ?

July 5, 2016 1.4k views
Dokku Ruby on Rails DNS DigitalOcean Getting Started Ubuntu

Hi, I just create a droplet using dokku and deploy a RoR app, the problem is that only with the ip addres the web app doesn't open, the browser show this message "This webpage is not available", only if I specify the port it work (e.g. 100.201.202.203:50000). I add a domain and its the same, only work with the port (e.g www.something.com:50000). Someone know why is this happening and how can I fix it?

1 Answer

When you use a URL without a port included your browser assumes it is port 80 (or 443 for HTTPS). Using anything else requires it to be specified. Generally with modern apps using Rails, a JavaScript framework or something other than static HTML or PHP scripts you will use a reverse proxy web server in front of the app to process requests (this also automatically gives you nice standard access logs). What you can do is:

First install nginx

sudo apt-get update;
sudo apt-get install nginx;

Then once it's installed, edit the nginx configuration file in /etc/nginx/sites-enabled/ so it reads like:

server {
        listen   80; 
        server_name example.com; 

     location / {      
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:50000;

     }
}

Then restart nginx:

service nginx restart
  • Hi @ryanpq. dokku brings by default nginx, when I ssh the app and go to /etc/nginx there is no folder with the name "sites-enabled" but there's a file nginx.conf and this is what contains:

    user www-data;
    worker_processes 4;
    pid /run/nginx.pid;
    
    events {
            worker_connections 768;
            # multi_accept on;
    }
    
    http {
    
            ##
            # Basic Settings
            ##
    
            sendfile on;
            tcp_nopush on;
            tcp_nodelay on;
            keepalive_timeout 65;
            types_hash_max_size 2048;
            # server_tokens off;
    
            # server_names_hash_bucket_size 64;
            # server_name_in_redirect off;
    
            include /etc/nginx/mime.types;
            default_type application/octet-stream;
    
            ##
            # Logging Settings
            ##
    
            access_log /var/log/nginx/access.log;
            error_log /var/log/nginx/error.log;
    
            ##
            # Gzip Settings
            ##
            gzip_disable "msie6";
    
            # gzip_vary on;
            # gzip_proxied any;
            # gzip_comp_level 6;
            # gzip_buffers 16 8k;
            # gzip_http_version 1.1;
            # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascri$
    
            ##
            # nginx-naxsi config
            ##
            # Uncomment it if you installed nginx-naxsi
            ##
    
            #include /etc/nginx/naxsi_core.rules;
    
            ##
            # nginx-passenger config
            ##
            # Uncomment it if you installed nginx-passenger
            ##
    
            #passenger_root /usr;
            #passenger_ruby /usr/bin/ruby;
    
            ##
            # Virtual Host Configs
            ##
    
            include /etc/nginx/conf.d/*.conf;
            include /etc/nginx/sites-enabled/*;
    }
    #mail {
    #       # See sample authentication script at:
    #       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
    #
    #       # auth_http localhost/auth.php;
    #       # pop3_capabilities "TOP" "USER";
    #       # imap_capabilities "IMAP4rev1" "UIDPLUS";
    #
    #       server {
    #               listen     localhost:110;
    #               protocol   pop3;
    #               proxy      on;
    #       }
    #
    #       server {
    #               listen     localhost:143;
    #               protocol   imap;
    #               proxy      on;
    #       }
    #}        
    

    In the directory /home/dokku/my-app/ there is another nginx.conf:

    server {
     listen      [::]:22445;
     listen      22445;
    
     access_log  /var/log/nginx/<my-app>-access.log;
     error_log   /var/log/nginx/<my-app>-error.log;
    
     location    / {
    
       gzip on;
       gzip_min_length  1100;
       gzip_buffers  4 32k;
       gzip_types    text/css text/javascript text/xml text/plain text/x-component$
      gzip_vary on;
      gzip_comp_level  6;
    
      proxy_pass  http://<my-app>-5000;
      proxy_http_version 1.1;
      gzip on;
      gzip_min_length  1100;
      gzip_buffers  4 32k;
      gzip_types    text/css text/javascript text/xml text/plain text/x-component$
      gzip_vary on;
      gzip_comp_level  6;
    
      proxy_pass  http://<my-app>-5000;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header X-Request-Start $msec;
    }
     include /home/dokku/<my-app>/nginx.conf.d/*.conf;
    

    In this last file if I change the listen to 80 nothing happens, and when I restart or rebuild the app it changes back to 22445.

Have another answer? Share your knowledge.