Create your own ngrok like server

11 Apr 2019

  1. Need a domain for your server. Currently, we use tunnel.example.com
  2. Config the nginx to proxy request to specific port.

     server {
         server_name tunnel.example.com;
    
         access_log /var/log/nginx/$host;
    
         location / {
           proxy_pass http://localhost:7689/;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header Host $host;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header X-Forwarded-Proto https;
           proxy_redirect off;
         }
    
         error_page 502 /50x.html;
         location = /50x.html {
           root /usr/share/nginx/html;
         }
    
         listen 443 ssl; # managed by Certbot
         ssl_certificate /etc/letsencrypt/live/tunnel.example.com/fullchain.pem; # managed by Certbot
         ssl_certificate_key /etc/letsencrypt/live/tunnel.example.com/privkey.pem; # managed by Certbot
         include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
         ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
     }
    
     server {
         if ($host = tunnel.example.com) {
             return 301 https://$host$request_uri;
         } # managed by Certbot
    
         listen 80;
         server_name tunnel.example.com;
         return 404; # managed by Certbot
     }
    
  3. Run below command on local, then you can visit our local rails app in browser through tunnel.example.com

     ssh -vnNT -o ServerAliveInterval=30 -R 7689:localhost:3000 ubuntu@example.com