Skip to content

OpenResty

OpenResty: A.B.C.D
           │ │ │ └─ OpenResty patch
           │ │ └── OpenResty minor
           │ └──── Nginx minor
           └────── Nginx major

准备工作

sh
mkdir lua logs www conf.d

vim nginx.conf

nginx
worker_processes 1;

events {
    worker_connections 1024;
}

http {
    lua_shared_dict sessions 10m;  # session 需要
    lua_package_path "/usr/local/openresty/lualib/custom/?.lua;;";

    include /etc/nginx/conf.d/*.conf;
}
nginx
#user  nobody;
worker_processes  1;

error_log  logs/error.log;
pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;
}

在 conf.d 下编写一个最基础server,否则什么都看不到

vim conf.d/default.conf

nginx
server {
    listen 80;

    location / {
        return 200 "Hello OpenResty\n";
    }
}

docker-compose

vim docker-compose.yml

yml
version: "3"

services:
  openresty:
    image: openresty/openresty:1.21.4.3-alpine
    container_name: openresty
    ports:
      - "8080:80"
    volumes:
      # 主配置
      - ./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf

      # server / location
      - ./conf.d:/etc/nginx/conf.d

      # Lua 代码
      - ./lua:/usr/local/openresty/lualib/custom

      # 日志
      - ./logs:/usr/local/openresty/nginx/logs

      # 静态资源(如果有)
      - ./www:/data/www

    environment:
      - TZ=Asia/Shanghai
    restart: unless-stopped