Skip to content

ngrok on nginx

Jaymz Rhime edited this page May 29, 2018 · 7 revisions

(Make sure you run the latest version of nginx.conf if you haven't updated since 5/25/2018.)

cd ~/ts_environment
./scripts/nginx.sh

Get ngrok hosting a local site

To serve up ~/Sites/[your-site-name] from [your-ngrok-id].ngrok.io

  1. Edit /etc/hosts with sudo, and add this line: 127.0.0.1 [your-ngrok-id].ngrok.io
  2. Start ngrok with ngrok http 80 --subdomain [your-ngrok-id]
  3. Replace [your-username], [your-ngrok-id], and [your-site-name] in the template below. (For sites with a different web root, you will need to set the rootpath like [your-site-name]/web)
  4. Put the file at /usr/local/etc/nginx/servers/[your-site-name].conf
  5. brew services restart nginx

[your-site-name].conf template

server {
  listen       8080;

  set $basepath "/Users/[your-username]/Sites";

  set $domain $host;

  client_max_body_size 100M;

  # handle localhost
  if ($domain ~ "^[your-ngrok-id].ngrok.io$") {
  	set $rootpath "[your-site-name]";
  	set $servername "[your-ngrok-id].ngrok.io";
  }

  server_name $servername;

  access_log "/usr/local/var/log/nginx/server.${servername}.access.log";
  error_log "/usr/local/var/log/nginx/server.localhost.error.log";

  root $basepath/$rootpath;


  # handle simplesaml
  location ~ /simplesaml {
      index index.php;
      location ~ ^(?<prefix>/simplesaml)(?<phpfile>.+?\.php)(?<pathinfo>/.*)?$ {
          include fastcgi.conf;
          fastcgi_param SERVER_NAME $servername;
          fastcgi_split_path_info ^(.+?\.php)(/.+)$;
          fastcgi_param PATH_INFO $pathinfo if_not_empty;
          fastcgi_pass php;
     }
  }

  location / {
    try_files $uri $uri/ /index.php?$args;
  }

  location ~ \.php$ {
    #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    include fastcgi.conf;

    fastcgi_param SERVER_NAME $servername;

    fastcgi_intercept_errors on;
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;

    fastcgi_pass php;
  }
}