proxying websockets via nginx and https

This is the nginx configuration I use to put a Node.js WebSocket app behind HTTPS. The app listens on a local port (4050 here) and only speaks plain WebSocket. nginx handles the SSL certificate, so browsers can connect securely with wss://, and the app never has to deal with TLS itself. How it works The map block sets a $connection_upgrade variable. When a browser sends an Upgrade header, which is how every WebSocket connection starts, the value is upgrade. For ordinary requests it’s close. The upstream block names the app websocket and points it at 127.0.0.1:4050, so the app’s port is only reachable through nginx. The server block listens on port 443 with the site’s certificate and key. The log and certificate paths are the ones used by the control panel on that server, so adjust them for yours. location / is where the WebSocket proxying happens. proxy_http_version 1.1 is required because WebSockets need HTTP/1.1. The two proxy_set_header lines pass the Upgrade and Connection headers on to the app; without them nginx drops those headers and the handshake fails. Host passes the original domain through. The script-file location sends requests ending in .php, .py and similar to the same port, using the panel’s standard proxy settings from proxy.inc. The .ht, .git, .svn, .hg and .bzr locations block access to hidden config files and version-control folders. The /.well-known/acme-challenge location lets AutoSSL or Let’s Encrypt verify the domain, so the certificate keeps renewing. Using it Replace DOMAIN.com, the listen IP (172.31.83.59) and the port 4050 with your own values. Test the configuration with nginx -t and reload nginx. Your front end can then connect with new WebSocket("wss://DOMAIN.com/"). ...

December 9, 2020 · 2 min · Harshvardhan Malpani

Sample .htaccess for Laravel with PHP FPM

This is the .htaccess I used for a Laravel API on an Apache server with PHP running through PHP-FPM. It solves two problems at once. First, the domain’s document root pointed at the Laravel project folder rather than at Laravel’s public folder, so requests have to be passed into public without /public appearing in the URL. Second, with PHP-FPM, Apache doesn’t pass the Authorization header to PHP by default, so token logins to the API fail. ...

October 23, 2020 · 4 min · Harshvardhan Malpani

Gravityforms limit submission – once for user, unlimited for guest

I use Gravity Wiz’s GP Limit Submissions, one of the Gravity Perks add-ons for Gravity Forms, to control how many times a form can be submitted. On one site I needed this combination: each logged-in user could submit the form only once, while visitors who weren’t logged in could submit it as many times as they liked. I got it by adding a few lines to the plugin’s user rule. ...

October 22, 2020 · 3 min · Harshvardhan Malpani

How to add Buy Now button on Product page – WordPress/woocommerce?

A WooCommerce product page only has an Add to cart button. A Buy Now button that adds the product and takes the shopper straight to the checkout saves a step for people who only want that one thing. This snippet adds one next to Add to cart, without a plugin. Put the code in your child theme’s functions.php, or in a code snippets plugin. Change && to && If the if line shows up as && when you copy the code, that’s HTML encoding from the web page. PHP needs the plain &&. ...

October 7, 2020 · 3 min · Harshvardhan Malpani

How to get Gravity forms entry value by label?

Gravity Forms stores each entry as an array keyed by field ID, so in a hook such as gform_after_submission you normally read a value with something like $entry['3'], or $entry['1.3'] for the first-name part of a Name field. Those IDs are different on every form, and they’re easy to mix up. When I’d rather refer to fields by the label people see on the form, I use this helper to look the value up by label instead. ...

October 3, 2020 · 3 min · Harshvardhan Malpani

How to edit wordpress woocommerce order api responses (legacy + latest)

WooCommerce lets you change what its API sends back for an order without touching the order itself. On one store I needed three changes in the order data the API returns: hide the customer’s email address; send the shipping phone number in place of the billing phone, while keeping the original billing phone in an extra field; remove the meta data attached to each line item. The store saved the shipping phone in the order meta key _shipping_phone. The code below does this for both the legacy WooCommerce API and the current REST API. Put it in your child theme’s functions.php or in a small custom plugin. If you add it to functions.php, leave out the opening <?php, because that file already starts with one. ...

August 31, 2020 · 4 min · Harshvardhan Malpani

Standalone script to print all published post IDs in WordPress

Sometimes an outside script or another site needs to know which posts a WordPress blog has, for example to build a list of pages to check or to sync something. This small standalone PHP file returns the IDs of all published posts as a JSON array. It loads only the minimum of WordPress needed to reach the database, so it’s quick and doesn’t run your theme or plugins. Save it as a .php file in the WordPress root folder, next to wp-load.php, because it loads wp-load.php by a relative path. Then open that file’s address on your site in a browser, or call it from your script. ...

August 31, 2020 · 2 min · Harshvardhan Malpani

PHP caveats – int/float type conversion

Everything that comes from a form, a URL or a CSV file reaches PHP as a string, so sooner or later you have to decide whether a value like "1e3", "042" or "1.5n" is a number, and which number. PHP gives you several ways to do that, and they don’t agree with each other. The following code checks what built-in functions like intval, floatval, is_numeric, is_float and filter_var return for several different types of values. Feel free to run it locally, and leave a comment if I can make the results more interesting with some more functions. ...

July 4, 2020 · 11 min · Harshvardhan Malpani

Jenkins auto deploying flask in test env with last build process kill and pipenv

This is the shell script I used in Jenkins to deploy a Flask API to a test server on every build. Each build installs the dependencies with pipenv, runs the database migrations, stops the copy of the app that the previous build started, and starts the new code in the background. It goes into an “Execute shell” build step of a freestyle job. It assumes the repository contains a Pipfile, the killlastbuild.sh script described below, and a Flask app that uses Flask-Migrate for its database. ...

June 22, 2020 · 5 min · Harshvardhan Malpani

How to whitelist IPs in NGINX configuration?

When a site, or part of one, should only be reachable from a few known IP addresses, such as a staging site, an admin panel or an internal tool, nginx can refuse everyone else with 403 Forbidden. This is the version I used, based on a variable that is set when the visitor’s IP is on the list. Add this code in your .conf file: if ($remote_addr = "14.51.20.3") { set $block_me_now "B"; } if ($remote_addr = "104.1.20.29") { set $block_me_now "B"; } if ($block_me_now != "B") { return 403; break; } How it works $remote_addr is the IP address of the client connecting to nginx. Each if ($remote_addr = "...") block compares it with one allowed address, and on a match set $block_me_now "B"; stores the value B in a variable. Despite the name, B here means “allowed”. The last block returns 403 to every request where the variable isn’t B, which means every IP that isn’t on the list. return stops processing straight away, so the break; after it never runs; it’s harmless. To allow another address, copy one of the first two blocks and change the IP. ...

June 8, 2020 · 2 min · Harshvardhan Malpani