OpenResty Installation and Configuration Guide

OpenResty Versions and Variants

Several Nginx-based web server implementations are available:

  • OpenResty: An enhanced distribution of Nginx with LuaJIT integration (recommended)
  • Nginx Plus: Commercial version with additional enterprise features
  • Tengine: A fork of Nginx developed by Alibaba

Installing OpenResty on Linux

This guide uses CentOS 7.6.1810 as the base system.

1. Install Dependencies

OpenResty requires several libraries for proper compilation:

# Install dependencies on CentOS
yum -y install readline-devel pcre-devel openssl-devel gcc perl curl

2. Download Source Code

Download and extract the OpenResty source package:

wget https://openresty.org/download/openresty-1.11.2.5.tar.gz
tar zxvf openresty-1.11.2.5.tar.gz
cd openresty-1.11.2.5

3. Compile and Install

Configure with desired options and compile:

./configure --prefix=/usr/local/openresty \
            --with-luajit \
            --without-http_redis2_module \
            --with-http_iconv_module
make
make install

4. Start the Service

Manual Startup

# Start Nginx
/usr/local/openresty/nginx/sbin/nginx

# Stop Nginx
/usr/local/openresty/nginx/sbin/nginx -s stop

# Graceful shutdown
/usr/local/openresty/nginx/sbin/nginx -s quit

# Reload configuration
/usr/local/openresty/nginx/sbin/nginx -s reload

Systemd Service Integration

Create a systemd service file:

vim /usr/lib/systemd/system/nginx.service

With the following content:

[Unit]
Description=The OpenResty HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf
ExecStart=/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
ExecReload=/usr/local/openresty/nginx/sbin/nginx -s reload
ExecStop=/usr/local/openresty/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Enable and start the service:

systemctl enable nginx.service
systemctl start nginx.service

Docker Installation

For containerized deployments:

# Pull the latest Nginx image
docker pull nginx

# Copy configuration files from the image
docker cp nginx:/etc/nginx/nginx.conf /path/to/config/nginx.conf
docker cp nginx:/etc/nginx/conf.d /path/to/config/conf.d
docker cp nginx:/usr/share/nginx/html /path/to/html

# Run the container with volume mappings
docker run \
  -p 8000:80 \
  -p 8001:81 \
  -p 8002:82 \
  --name webserver \
  -v /path/to/config/nginx.conf:/etc/nginx/nginx.conf \
  -v /path/to/config/conf.d:/etc/nginx/conf.d \
  -v /path/to/logs:/var/log/nginx \
  -v /path/to/html:/usr/share/nginx/html \
  -d nginx:latest

Basic Configuration

Directory Structure

OpenResty's directory layout includes:

  • client_body_temp: Temporary files for client request bodies
  • conf: Configuraton files
  • html: Default web content
  • logs: Access and error logs
  • sbin: Nginx executable

Configuration File Structure

The main configuration file follows this structure:

# Global settings
user nobody;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
  use epoll;
  worker_connections 51200;
}

http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;
  tcp_nopush on;
  
  # Server configurations
  server {
    listen 80;
    server_name localhost;
    
    location / {
      root /usr/share/nginx/html;
      index index.html;
    }
  }
}

Location Directives

Location blocks define how request are processed:

location /api {
  proxy_pass http://backend-service:8080;
}

location /static {
  root /var/www/static;
  index index.html;
}

Load Balancing with Upstream

Distribute traffic across multiple backend servers:

upstream backend_cluster {
  server 192.168.1.10:8080;
  server 192.168.1.11:8080;
  server 192.168.1.12:8080;
}

server {
  listen 80;
  location / {
    proxy_pass http://backend_cluster;
  }
}

Weighted Load Balancing

Assign different traffic proportions to servers:

upstream weighted_cluster {
  server 192.168.1.10:8080 weight=3;
  server 192.168.1.11:8080 weight=1;
  server 192.168.1.12:8080 weight=2;
}

Server Health Management

Mark servers as unavailable or backup:

upstream health_managed {
  server 192.168.1.10:8080 down;  # Currently unavailable
  server 192.168.1.11:8080;       # Primary server
  server 192.168.1.12:8080 backup; # Backup server
}

Anti-Leeching Configuration

Prevent hotlinking of resources:

location ~* \.(png|jpg|gif)$ {
  valid_referers none blocked server_names ~\.google\. ~\.bing\.;
  
  if ($invalid_referer) {
    return 403;
  }
  
  root /usr/share/nginx/images;
}

Tags: OpenResty nginx load-balancing docker Linux

Posted on Fri, 16 Oct 2026 16:45:57 +0000 by Crowly